Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10966
| References | <CAF-Rda-4EFBj=AiuR10LAUPxoLaAX-j71TiwrabKSL40x19wQw@mail.gmail.com> <CAMZYqRQW-CVHC-k1BP2Ocqgc+TA_eu1CiqgBVGNMGNbiXGaSog@mail.gmail.com> |
|---|---|
| From | Eli Bendersky <eliben@gmail.com> |
| Date | 2011-08-06 11:18 +0300 |
| Subject | Re: Calling super() in __init__ of a metaclass |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1968.1312618730.1164.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Calling super() in __init__ of a metaclass Eli Bendersky <eliben@gmail.com> - 2011-08-06 11:18 +0300
csiph-web