mark nottingham

Python Just Got a Whole Lot Cooler

Monday, 29 March 2004

OK, so I know they’ve been around for a while, but I haven’t really got into Python’s metaclasses until just now, because I’ve been… well… busy.

This excellent presentation about them from the most recent PyCon woke me up to what they could do — which is just about anything.

One of the goals is making more information in Python declarative. To me, this means things similar to (but more elegant than) JSR175; the only thing we’re missing now is a good syntax for function attributes ( grumble, grumble).

Another interesting tidbit:

Class Construction — More use cases
[…]
* Load/create bases/methods/descriptors from non-python definitions:
* XML DTDs, VRML PROTOs, DB Connections, IDL
[…]

Can we say XML data binding?

My favourite code sample:

mnot-laptop:~> python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def functionalMeta(name, bases, dict):
... print "egads, how evil!"
... return type(name, bases, dict)
... 
>>> class R:
... __metaclass__ = functionalMeta
... 
egads, how evil!
>>> type(R)
<type 'type'>

Wow! You can fit a lot of magic in that little metaclass hook. I’m still getting my head around most of this, but I have plans, oh, yes, I have plans for them…

More to read yet at OnLamp and DeveloperWorks.