[Python-3000] PEP for Metaclasses in Python 3000

Steven Bethard steven.bethard at gmail.com
Fri Mar 9 22:23:39 CET 2007


On 3/9/07, Talin <talin at acm.org> wrote:
> PEP: xxx
> Title: Metaclasses in Python 3000

Thanks for writing this.

>      This attribute is a method named __metacreate__, which is invoked
>      before the evaluation of the class body, and which has the
>      following form:
>
>       classdict = metaclass.__metacreate__(name, bases, keywords)
>
[snip]
>      There were some objections in the discussion to the 'two-phase'
>      creation process, where the metaclass is invoked twice, once to
>      create the class dictionary and once to 'finish' the class. Some
>      people felt that these two phases should be completely separate, in
>      that there ought to be separate syntax for specifying the custom
>      dict as for specifying the metaclass. However, in most cases, the
>      two will be intimately tied together, and the metaclass will most
>      likely have an intimate knowledge of the internal details of the
>      class dict. Requiring the programmer to insure that the correct dict
>      type and the correct metaclass type are used together creates an
>      additional and unneeded burden on the programmer.

I think it would really help to give some example code so that people
can see why one way is easier than the other.  I gather for using an
ordered dict, the two alternatives would look something like::

    # using __metacreate__ class/static method
    class meta(type):
        @staticmethod
        def __metacreate__(name, bases, keywords):
            return ordereddict()
        def __init__(cls, name, bases, bodydict):
            for key, value in bodydict.items():     # I know this is in order
                ...
    class C(metaclass=meta):
        ...

    # using separate callables
    def meta(name, bases, bodydict):
        for key, value in bodydict.items():     # I know this is in order
            ...
    class C(metaclass=meta, bodydict=ordereddict):
        ...

But it would be nice to fill in those ellipses with code that does
something useful.

<bikeshed>
I'd rather __metacreate__ be called something like __createdict__,
__creationdict__, __metadict__, __getmetadict__, etc. where it's
clearer that the purpose is to create a dict object.
</bikeshed>

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list