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


Groups > comp.lang.python > #10966

Re: Calling super() in __init__ of a metaclass

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <eliben@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '*not*': 0.05; 'instance': 0.05; 'parameter': 0.05; 'convention.': 0.07; 'type,': 0.07; "(it's": 0.09; 'object;': 0.09; 'rename': 0.09; 'subclass': 0.09; 'type)': 0.09; 'am,': 0.12; 'things.': 0.13; 'argument': 0.15; '*instance*': 0.16; '__init__': 0.16; 'bases,': 0.16; 'definition,': 0.16; 'foo.': 0.16; 'invoked.': 0.16; 'metaclass': 0.16; 'subject:() ': 0.16; '\xa0def': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'thanks,': 0.18; 'help.': 0.19; 'cc:no real name:2**0': 0.20; "doesn't": 0.22; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; '\xa0if': 0.23; 'aug': 0.24; 'received:209.85.220': 0.27; '(this': 0.28; 'sat,': 0.28; 'bound': 0.29; 'message-id:@mail.gmail.com': 0.29; 'second': 0.29; 'cc:addr:python.org': 0.30; 'information?': 0.30; 'parent': 0.30; 'class': 0.30; 'chris': 0.32; 'earlier': 0.32; 'typically': 0.32; 'does': 0.32; 'calling': 0.33; 'typical': 0.34; 'rather': 0.35; 'created': 0.36; 'useful': 0.36; 'skip:" 10': 0.36; 'class.': 0.37; 'passed': 0.37; 'using': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'i.e.': 0.39; 'skip:\xa0 10': 0.39; "it's": 0.40; 'your': 0.61; 'super': 0.64; 'here': 0.65; 'here:': 0.65; 'here.': 0.66; 'flash': 0.71; '11:04,': 0.84; 'definition:': 0.84; 'remember:': 0.84; 'subject:Calling': 0.84; 'sudden': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=x3AV5LPs6Tsw5pRL8ihKX1DaSRkvAuNbxibx4XFKmiE=; b=gIavlQNjG8AENfV3lmSGaDE2WgiJqKfOuYVOzpuGgWJylFPJ/IblRji4V8lJoErStW xi9ZdLN0OlznmtZDMSIyYmbtw/75++sTOeK/GE+8xcy7y7doczC/rD7BEbH/RPbgslVS zRM9MdXOljC+h4/KS3bB2ZP7/pAPv4fqyDdOI=
MIME-Version 1.0
In-Reply-To <CAMZYqRQW-CVHC-k1BP2Ocqgc+TA_eu1CiqgBVGNMGNbiXGaSog@mail.gmail.com>
References <CAF-Rda-4EFBj=AiuR10LAUPxoLaAX-j71TiwrabKSL40x19wQw@mail.gmail.com> <CAMZYqRQW-CVHC-k1BP2Ocqgc+TA_eu1CiqgBVGNMGNbiXGaSog@mail.gmail.com>
From Eli Bendersky <eliben@gmail.com>
Date Sat, 6 Aug 2011 11:18:28 +0300
Subject Re: Calling super() in __init__ of a metaclass
To Chris Rebert <clp2@rebertia.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1968.1312618730.1164.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312618730 news.xs4all.nl 23841 [2001:888:2000:d::a6]:33146
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10966

Show key headers only | View raw


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


Thread

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

csiph-web