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


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

Re: Calling super() in __init__ of a metaclass

Started byChris Rebert <clp2@rebertia.com>
First post2011-08-06 01:04 -0700
Last post2011-08-06 01:04 -0700
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 Chris Rebert <clp2@rebertia.com> - 2011-08-06 01:04 -0700

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

FromChris Rebert <clp2@rebertia.com>
Date2011-08-06 01:04 -0700
SubjectRe: Calling super() in __init__ of a metaclass
Message-ID<mailman.1967.1312617874.1164.python-list@python.org>
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

[toc] | [standalone]


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


csiph-web