[Python-3000] Metaclasses in Py3K

Talin talin at acm.org
Fri Dec 8 18:47:30 CET 2006

How about this: Email responses/ideas to me directly, I'll summarize 
them and post the result.

Guido van Rossum wrote:
> I've been thinking about this too, and I think it's reasonable to let
> the metaclass provide the dict to be used as locals. This is easier
> when the metaclass is given in the class header, e.g. by way of a base
> class, or through the default metaclass. Although the default default
> metaclass could of course have a hook to pass things on to another
> metaclass specified using a __metaclass__ assignment, I'd rather
> innovate a bit more and add keyword arguments to the class header,
> e.g.
> class C(B1, B2, metaclass=Foo):
>  ...
> How can we design this feature without yet another gigantic
> contentious thread? I'm losing my appetite for those. I wonder if the
> right thing wouldn't be if you could do some serious research into the
> best possible semantics, syntax and implementation, and then post a
> fully-formed proposal (not quite a PEP). There is of course always the
> danger that you might go off into the deep end with that, so I'll
> leave it up to your judgement to decide when to post again. (Also, I'm
> still quite restricted in my internet access, at least until Monday.)
> --Guido
> On 12/8/06, Talin <talin at acm.org> wrote:
>> I'd like to see metaclasses have additional capabilities in Py3K, and
>> one thing I'd like is for metaclasses to be able to have access to the
>> order of declarations within a class.
>> If I understand correctly, in the current Python a class definition is
>> essentially a suite with ordinary local variables; once that suite
>> finishes executing, the locals() dict becomes the class's __dict__. The
>> metaclass slips in between these two phases of construction, in that it
>> can examine or modify the dict before it becomes a class.
>> Suppose, however, that the metaclass could hook into the get/set
>> operations of the locals() dict itself. In other words, the
>> __metaclass__ statement would have a side-effect of replacing the
>> locals() dict with a mapping object supplied by the metaclass.
>> This would allow the metaclass to intercept individual variable
>> assignments as they occur, rather than taking the whole dict in at once.
>> I can think of a ton of applications for this. For example, you could
>> implement the functionality of __slots__ as a metaclass, allowing you to
>> define the slots using regular Python syntax rather than having to pass
>> in a list of name strings. You could define C structures using regular
>> Python syntax. You could declare CORBA or other IDL interfaces using
>> Python syntax and appropriate decorators. You might even be able to do
>> things like enumerations using metaclasses.
>> -- Talin
>> _______________________________________________
>> Python-3000 mailing list
>> Python-3000 at python.org
>> http://mail.python.org/mailman/listinfo/python-3000
>> Unsubscribe: 
>> http://mail.python.org/mailman/options/python-3000/guido%40python.org

More information about the Python-3000 mailing list