Here it is: Clever uses for metaclasses
SUMMARY
This talk introduces metaclasses and attempts to “defang” them, showing what they are good for, and when they are silly.
Metaclasses can reduce redundancy in code but can be very confusing, so in this talk, I will walk through several examples of how to use metaclasses to solve problems.
I plan to cover (at least) these examples:
- Add extra methods and attributes to a class after its definition.
- Verify a class correctly implements a specification.
- Give a subclass its own class-variables rather than sharing them with the parent class.
- Implement the basics of an object-relational mapper (ORM).
I published an article in the November 2008 issue of Python Magazine with the same title (Clever Uses for Metaclasses) and I’ll use some code from that article.
I want to give this talk in a friendly, informal manner, so that people that feel intimidated by metaclasses realize that there’s nothing to be scared of.
EXPERTISE LEVEL
This talk is aimed at the intermediate-level programmer, already familiar with object-oriented concepts and is really comfortable with Python.
I do not expect people in the audience to know ANYTHING about metaclasses before this talk.
AREAS OF PYTHON
- object-oriented programming
- metaprogramming
- dynamic language tomfoolery
- introspection
Matt,
Hey did you think of using class decorators instead of metaclasses? I think you can do all that with class decorations, and probably easier to understand.
Yeah, I agree about class decorators being a good substitute. The big
difference with class decorators is that they don't apply to
subclasses, so you have to keep applying them in each subclass.
For maximum confusion, maybe metaclasses could apply class decorators,
and class decorators might be made to depend on code built by the
metaclasses.
I like writing articles and exploring these wacky corners, but when it
comes to write, I try to keep my stuff as stupid as possible. All the
dynamic tomfoolery is awesome when it works, but it is hellish to
debug.
Matt
Meta classes really confuses me too. Ya. I have read the Clever Uses for Metaclasses. It is very nice. Nice explanations are given. Thanks for sharing.
by thoi trang