The __prepare__method
The __prepare__ method is passed with the name of the new class and its list of parent classes as well as any keyword arguments supplied by the user. It can do whatever we want it to, but it should also return a dictionary or similar object that could be used to hold the class's attributes (refer to the previous example).
We can preassign values to the attribute dictionary from inside of __prepare__, so we can actually assign attributes to the class before it even exists. That brings us to the second thing that metaclasses can easily control-the class's namespace.
In our previous example, we returned a dict() instance from __prepare__, so instances of this metaclass use a normal dictionary to store their attributes while ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access