Wandering Thoughts archives

2012-02-21

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):

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:

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.)

python/MetaclassIndex written at 23:35:17; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.