2024-04-22 –, A03-A04
Metaclasses. What are they? Where do they live? How do they reproduce?
Did you know that you can make your classes receive keyword arguments, just like functions? And that they can be decorated as well?
Do you want to understand how classes, metaclasses and decorators work and what are they good for?
In this hands-on coding session we will inspect the inner workings of how Python creates classes, and how decorators, meta-classes and methods from superclasses can influence this process.
We'll explore:
- normal and special methods
- how attribute lookup works between instances and classes
- what are descriptors, and how they fit into attribute lookup process
- what is the relationship between instances, classes and metaclasses
- what are metaclasses for
- and some other metaprogramming odds and ends
All that is required for you to enjoy this session is that you have written a class in Python. If you've done the original Python Tutorial, that should be more than enough.
Class outline:
- 10 min.: Intro and Setup
- 15 min.: Every time is "runtime":
- Function, Classes and Methods are created at runtime
- The dual responsibility of
class
- Attribute lookup and method resolution order
- The role of
.__dict__
and.__slots__
- Special methods, giving instances superpowers
- 10 min.: Everything is an object:
- Functions, methods and classes are also objects
- Descriptors, properties and method binding
- The two functionalities of
type
- And how to create a class without the
class
keyword
- And how to create a class without the
- 10 min.: Metaclass is the class of the class:
- Calling a class creates an instance, calling a metaclass creates a class
type
&object
: class relations- Creating and using metaclasses
- 15 min.: What are metaclasses for?
- Giving classes special methods
- Intercepting class creation
- Keyword arguments in class declarations
- Preparing the class namespace
- The role of the methods:
__call__
,__new__
&__init__
- What are metaclasses not for
- 5 min.: complete debugging walkthrough
- class creation
- instance creation
- instance use
- 5 min.: You're unlikely to ever need to create a metaclass
__init_subclass__
- Class decorators
__class_getitem__
- Capturing descriptor names and ordering
- 5 min.: Examples
- 5 min.: conclusion and questions
None
Expected audience expertise: Python:Novice
Abstract as a tweet (X) or toot (Mastodon):The secret life of Metaclasses, a tour of Python metaprogramming concepts for novices
Public link to supporting material, e.g. videos, Github, etc.:https://github.com/leorochael/2024-04-22-Talk-PyConDE-Metaclasses
Luciano Ramalho is the author of Fluent Python, published in 9 languages and 2 editions since 2015. He was a pioneering organizer of the Python Brasil association, which supports the Brazilian national PyCon. He is now a writer, teacher, and model railroader.
Python developer with over 22 years of experience, Leonardo is a technical reviewer for Luciano Ramalho's "Fluent Python" book for both editions.