Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10964

Re: Calling super() in __init__ of a metaclass

References <CAF-Rda-4EFBj=AiuR10LAUPxoLaAX-j71TiwrabKSL40x19wQw@mail.gmail.com>
Date 2011-08-06 01:04 -0700
Subject Re: Calling super() in __init__ of a metaclass
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1967.1312617874.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky <eliben@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Calling super() in __init__ of a metaclass Chris Rebert <clp2@rebertia.com> - 2011-08-06 01:04 -0700

csiph-web