Skip to Content
View all events

Python Metaprogramming in Practice

Published by O'Reilly Media, Inc.

Intermediate content levelIntermediate

Diving deep into decorators, descriptors, and metaclasses

Course outcomes

  • Make decorators to augment the behavior of functions and/or classes
  • Customize classes just as much as you can customize class instances
  • Start your own Python framework that deeply customizes Python’s classes

Course description

If you can imagine a Python feature that doesn’t exist, you might be able to invent it yourself using one of Python’s metaprogramming features.

Join expert Trey Hunner to dive into the low-level metaprogramming features that Python provides for library and framework authors. You'll explore decorators, descriptors, and metaclasses, and see how each of these Python features works and what power they hold. You’ll also understand the trade-offs behind these different features: some are marginally more powerful but considerably more complex and should be used with caution.

What you’ll learn and how you can apply it

  • Customize classes with metaclasses
  • Control attribute behaviors with descriptors
  • Implement a decorator
  • Learn when to use metaclasses and descriptors and when to opt for alternatives

This live event is for you because...

  • You’re an experienced software developer.
  • You work with Python classes.
  • You want to deeply customize your objects and classes in Python.

Prerequisites

  • Access to a Python interpreter and a system command prompt/terminal window (necessary to participate in the exercises)
  • An understanding of what dunder methods are
  • An understanding of how to use decorators

Recommended preparation:

Complete the following setup instructions:

  • Create a directory to use for this workshop in your home directory or desktop.
  • Download the ZIP file of course exercises from either https://trey.io/tnbbru-metaprogramming-exercises (Google Drive) or https://trey.io/em7984-metaprogramming-exercises (GitHub) and extract it into the new directory you just made.
  • When unzipped, that ZIP file will create a new folder called "exercises." Open up your system command prompt or terminal (not a Python prompt) and change directories to that exercises directory. On Windows you may need to type "C:" or another drive letter to change drives first. There are a couple shortcuts in Windows for opening a command prompt in the correct directory quickly. (See https://trey.io/cmd1 and https://trey.io/cmd2 for examples.)
  • Once in the exercises directory, open a command prompt or terminal and run "py -3 test.py" (Windows) or "python3 test.py" (Linux/macOS). You should see a success message that starts with "Welcome to the Test Framework!"

Recommended follow-up:

Schedule

The time frames are only estimates and may vary according to how the class is progressing.

Decorators: The basics (30 minutes)

  • Presentation: Passing functions around and dynamic function creation; the decorator syntax
  • Hands-on exercise: Explore decorators
  • Q&A

Deeper into decorators (40 minutes)

  • Presentation: Decorators—well-behaved, context, and class decorators; decorators that accept arguments
  • Hands-on exercise: Dive deeper into decorators
  • Q&A
  • Break

Descriptors (50 minutes)

  • Presentation: Nondata descriptors; data descriptors and how attribute lookups actually work; methods are descriptors
  • Hands-on exercises: Explore nondata and data descriptors
  • Q&A
  • Break

Metaclasses (60 minutes)

  • Presentation: Metaclasses intro; custom class dunder methods; class initialization; customizing class creation
  • Hands-on exercise: Explore metaclasses
  • Q&A

Your Instructor

  • Trey Hunner

    Trey Hunner helps Python programmers level up their skills through corporate training and through Python Morsels, a Python skill-building service for individuals and small teams. Trey also sends out a new Python tip every week through his email newsletter at pym.dev/newsletter.

    linkedinXlinksearch

Skill covered

Python