Using and understanding Python metaclasses (an index)
Most explanations of Python's metaclasses I've seen start by trying to explain the background and perhaps how metaclasses work, and only then do they start into what you can do with them. This approach doesn't work for me and I kept bouncing off these explanations, so I wrote a series of entries from the other way around; I started with what you can use metaclasses for and only later explained everything else. This entry is an index to this effort.
(I feel one of the reasons metaclasses are such tricky and slippery things is that they can be used to do a number of different jobs that are not obviously related to each other. It turns out there is a reason for all of these things to be clumped together, but to understand it you need quite a lot of background first. So you might as well begin with what you can do with metaclasses and then later explore why they can do all of these things as you get interested.)
To start with is my four part series on what you can do with metaclasses (in order of increasing obscurity):
- changing a class as it's being created, which is the classical case and most of what people use metaclasses for in practice.
- doing something when a class is called to create class instances; many writeups fold this in with the first use, but it's actually kind of different in subtle ways.
- adding and controlling class-only attributes, where we are getting into obscure territory.
- and finally supplying a class's special methods.
For an example of using metaclasses, I've written a namespace metaclass for using (or abusing) classes as namespaces. This uses a metaclass for the first case, changing a new class as it's being created.
Then there's a series of entries on the background of metaclasses, at two levels. At the high level is my explanation of why metaclasses work, which covers the high level features of Python that allows it to have metaclasses. At the low level I have a series of entries on the actual mechanics:
- what (new-style) classes are in Python, with
a supplement on understanding
- understanding what types really do, which mentions in passing the important information that a class with a metaclass has that metaclass as its type.
- how Python looks up attributes when you do
obj.attr, which is necessary to understand how several parts of metaclasses work.
These entries all have links to earlier entries with even more low level details, delving right down into the guts of how the CPython interpreter implements things like representing classes in memory.
By the end of all of this, you should not only know what you can do with metaclasses (even if you have no use for a lot of the possibilities) but you should also see why all of the different things are all aspects of a general set of mechanisms that runs through much of new-style Python classes.
(Of course, the expected irony of writing an explanation of metaclasses that I could read and understand is that I don't need it any more.)