Calling super() in __init__ of a metaclass
Chris Rebert
clp2 at rebertia.com
Sat Aug 6 04:04:30 EDT 2011
On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky <eliben at gmail.com> wrote:
> 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.
...which is an instance of the first argument to super(), which is how
super is typically invoked. Remember: a class is an instance of its
metaclass.
Perhaps if you rename `cls` to `self` and then re-read your snippet,
you'll have a flash of sudden understanding. Calling the parameter
`cls` is merely convention.
> 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.
The typical form of super(), mentioned earlier in the documentation,
is being used here:
super(type, obj) -> bound super object; requires isinstance(obj, type)
`type` here is the metaclass, `obj` here is the class. By definition,
a class is an *instance* of its metaclass, so the precondition is
satisfied.
Cheers,
Chris
--
http://rebertia.com
More information about the Python-list
mailing list