[Python-3000] PEP for Metaclasses in Python 3000

Josiah Carlson jcarlson at uci.edu
Sat Mar 10 20:18:31 CET 2007


Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> 
> Talin wrote:
> 
> >        class Foo(base1, base2, metaclass=mymeta):
> >          ...
> 
> -1 on this syntax, I think it's ugly.
> 
> Alternative proposals:
> 
>    class Foo(base1, base2) as MyMeta:
>      ...


While I don't particularly like the foo(base, metaclass=...) syntax,
(__metaclass__ also doesn't seem broken to me), the whole 'x as y'
approach to syntax is starting to wear on me.


>    class Foo(base1, base2) = MyMeta:

This particular variant make me want to claw out my eyes. Please don't
mention it again - I like my eyes!


> >      This attribute is a method named __metacreate__
> 
> Very bad choice of name -- it's not creating a meta-anything,
> and gives no clue what it is creating.

Agreed.


> >      It would be possible to leave the existing __metaclass__ syntax in
> >      place. Alternatively, it would not be too difficult to modify the
> >      syntax rules of the Py3K translation tool to convert from the old to
> >      the new syntax.
> 
> The existing syntax actually has some advantages, e.g.
> you can do things like
> 
>    class Foo:
>      class __metaclass__:
>        # Real class methods defined here
> 
> which I think is rather elegant, so I'm not sure I'd
> like the new syntax to completely replace the old one.

I agree; while I've never used the inline ability of metaclasses (I
rarely use metaclasses in the first place), losing this ability would
seem to be premature optimization.

Generally, about the only need I forsee for arbitrary keyword arguments
in the class 'foo(...)' signature is to pass arguments to the 'metadict'
factory (method, function, etc.).  Otherwise it seems more like a syntax
looking for a purpose than a purpose looking for a syntax.

There are two semantics that I would be happy with.  Either force
metaclasses to have a .metadict() or equivalent method (or a callable
attribute in the case of arbitrary callables), or even allow
__metaclass__ to be a tuple:

    class foo(...):
        __metaclass__ = mytype, newdict

Newdict needs to be callable, and if you want to pass certain
semantic-altering arguments, make newdict a factory can call it directly...

    class foo(...):
        __metaclass__ = mytype, newdict(no_dupes=True)

One wouldn't even need any of the default arguments only stuff to make
the two 'newdict' examples above use the same callable.


 - Josiah



More information about the Python-3000 mailing list