mnot’s Web log

Design depends largely on constraints.” — Charles Eames

Monday, 29 March 2004

Python Just Got a Whole Lot Cooler

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.


Filed under: Python

add to the discussion

your details

name
e-mail address

Your e-mail address will not be shared.

your comment

Separate paragraphs with blank lines; HTML markup will be removed.

By submitting a comment, you agree to grant a limited license to reproduce it, under the same terms as the page being commented upon. If you have questions or prefer other terms, please contact me.

Creative Commons License