Python Metaprogramming

Learning PathSkills: Python, Descriptors, Metaclasses, Abstract Base Classes, Protocols, Duck Typing, Introspection, Dynamic Code Execution, exec(), eval()

A person bringing object boxes to a metaclass machine

In this learning path, you’ll explore Python’s metaprogramming tools, the features that let your code inspect and change how other code behaves. You’ll start with descriptors and metaclasses, which hook into attribute access and class creation, then use those hooks to work through duck typing, abstract base classes, and protocols.

You’ll finish with introspection and with running code your program generates itself using eval() and exec(). Quizzes along the way let you check your understanding.

Python Metaprogramming

Learning Path ⋅ 8 Resources

Descriptors and Metaclasses

Descriptors control what happens when you get or set an attribute, and metaclasses hook into the moment Python creates a class. Both let you move behavior into the class itself instead of repeating it in every method. You’ll start with descriptors because they build directly on the attribute lookup you already know.

Title image for Python Descriptors (Python Descriptors: An Introduction)

Course

Python Descriptors

Learn what Python descriptors are, how the descriptor protocol works, and when descriptors are useful—with practical, hands-on examples.

Title image for Metaclasses in Python (Python Metaclasses and Metaprogramming)

Course

Metaclasses in Python

Metaclasses are an important but mysterious behind-the-scenes mechanism for instantiating classes in Python. In this video course, you'll learn how Python's metaclasses work in object-oriented programming.

Title image for Python Metaclasses (Python Metaclasses and Metaprogramming)

Interactive Quiz

Python Metaclasses

Interfaces, Protocols, and Duck Typing

You’ve seen how descriptors and metaclasses hook into attribute access and class creation. Abstract base classes are the most common thing people actually build with a metaclass, since ABCMeta is one. You’ll move from informal duck typing to explicit ABCs and then to static protocols, each a stricter answer to the question of what counts as the right type.

Title image for Getting to Know Duck Typing in Python (Duck Typing in Python: Writing Flexible and Decoupled Code)

Course

Getting to Know Duck Typing in Python

Learn about duck typing in Python---a type system based on an object's behavior rather than inheritance. By taking advantage of duck typing, you can create flexible and decoupled sets of Python classes that work together or independently.

Title image for Python Interfaces: Object-Oriented Design Principles (Implementing an Interface in Python)

Course

Python Interfaces: Object-Oriented Design Principles

In this video course, you'll explore how to use a Python interface. You'll come to understand why interfaces are so useful and learn how to implement formal and informal interfaces in Python. You'll also examine the differences between Python interfaces and those in other programming languages.

Title image for Exploring Protocols in Python (Python Protocols: Leveraging Structural Subtyping)

Course

Exploring Protocols in Python

Learn how Python's protocols improve your use of type hints and static type checkers in this practical video course.

Inspecting and Generating Code at Runtime

Duck typing, ABCs, and protocols all reason about objects from the outside. Now you’ll look inward, using .__dict__ to see what an object actually holds, then generate and run code that doesn’t exist until your program is already running. You’ll also see why turning strings into live code is risky.

Title image for Working With Python's .__dict__ Attribute (Using Python's .__dict__ to Work With Attributes)

Course

Working With Python's .__dict__ Attribute

Explore Python's .__dict__ attribute to manage class and instance attributes directly for more flexible, low-level control of your objects.

Title image for Evaluate Expressions Dynamically With Python eval() (Python's eval(): Evaluating Expressions Dynamically)

Course

Evaluate Expressions Dynamically With Python eval()

Learn how Python's eval() works and how to use it effectively in your programs. Additionally, you'll learn how to minimize the security risks associated to the use of eval().

Title image for Python's exec(): Execute Dynamically Generated Code (Python's exec(): Execute Dynamically Generated Code)

Tutorial

Python's exec(): Execute Dynamically Generated Code

Learn how to use Python's built-in exec() function to execute code that comes as either a string or a compiled code object.

Congratulations on completing this learning path! You’ve explored Python’s metaprogramming tools, from descriptors and metaclasses to interfaces and protocols, and on to runtime introspection and dynamic code execution.

Continue your advanced Python journey with the next learning path:

Learning Path

Network Programming and Security

5 Resources ⋅ Skills: CRUD Operations, REST APIs, HTTPS, Socket Programming, Network Security, Web Services

You might also be interested in these related learning paths:

Got feedback on this learning path?

Looking for real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

« Browse All Learning Paths