Calling super() in __init__ of a metaclass

Eli Bendersky eliben at gmail.com
Sat Aug 6 03:34:48 EDT 2011


Consider this standard metaclass definition:

class MyMetaclass(type):
    def __init__(cls, name, bases, dct):
        super(MyMetaclass, cls).__init__(name, bases, dct)
        # do meta-stuff

class Foo(object):
    __metaclass__ = MyMetaclass

The call "super(MyMetaclass, cls)" should returns the parent of
MyMetaclass here. But the 'cls' passed into this __init__ is *not*
MyMetaclass, but rather the created class - i.e. Foo. So how does
"super" get to the parent of MyMetaclass using this information? The
documentation of "super" says:

    If the second argument is a type, issubclass(type2, type) must be
true (this is useful for classmethods).

Yes, 'cls' is a type (it's "class Foo"), but no, it's not a subclass
of MyMetaclass, so this doesn't help.

Eli



More information about the Python-list mailing list