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


Groups > comp.lang.python > #10966 > unrolled thread

Re: Calling super() in __init__ of a metaclass

Started byEli Bendersky <eliben@gmail.com>
First post2011-08-06 11:18 +0300
Last post2011-08-06 11:18 +0300
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Calling super() in __init__ of a metaclass Eli Bendersky <eliben@gmail.com> - 2011-08-06 11:18 +0300

#10966 — Re: Calling super() in __init__ of a metaclass

FromEli Bendersky <eliben@gmail.com>
Date2011-08-06 11:18 +0300
SubjectRe: Calling super() in __init__ of a metaclass
Message-ID<mailman.1968.1312618730.1164.python-list@python.org>
On Sat, Aug 6, 2011 at 11:04, Chris Rebert <clp2@rebertia.com> wrote:
> 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.

Thanks, Chris. This clarifies things.

Eli

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web