[Python-3000] PEP for Metaclasses in Python 3000

Josiah Carlson jcarlson at uci.edu
Sun Mar 11 06:55:02 CET 2007


Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Josiah Carlson wrote:
> > I did have a post that was going to show that they were related, but
> > then I remembered that because of __metaclass__ assignment semantics, it
> > needs to necessarily have access to the class body dictionary at the
> > point of assignment, so the compiler, etc., cannot be taught to extract
> > everything.  Syntax is necessary to offer an ordered dictionary
> > semantics.
> 
> I still think that extra arguments are unnecessary in
> any case, because anything you could do by passing args
> to the dict creation function could be done by using a
> function instead of a class for the "metaclass".

Generally, yes.  Also, I believe that the PEP offered as a requirement
that the newdict creation stuff be a callable method on the metaclass,
or at least a callable attribute on the metaclass function.

What I was talking about with regards to the metaclass needing access to
the class body is for cases like the following which are currently
allowed...

    class foo(...):
        a = ...
        __metaclass__ = callable(a, ...)

The above is syntactically and semantically kosher, and only by pulling
the metaclass definition outside of the class body can we 1) prevent it
from happening, and more importantly 2) be able to discover the
metaclass and the ordered dictionary *prior* to the execution of the
class body.

Yes, you lose the ability to inline metaclasses, but that can't really
be helped.

 - Josiah



More information about the Python-3000 mailing list