Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'received:209.85.160.174': 0.09; 'received:mail-gy0-f174.google.com': 0.09; 'rename': 0.09; 'subclass': 0.09; 'type)': 0.09; 'am,': 0.12; '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; '\xc2\xa0if': 0.16; 'cc:addr:python- list': 0.16; 'wrote:': 0.16; 'cheers,': 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; 'aug': 0.24; '(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; 'skip:\xc2 20': 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; 'received:209.85.160': 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; "it's": 0.40; 'your': 0.61; 'super': 0.64; 'here': 0.65; 'here:': 0.65; 'here.': 0.66; 'flash': 0.71; 'skip:\xc2 10': 0.74; 'definition:': 0.84; 'remember:': 0.84; 'sender:addr:chris': 0.84; 'subject:Calling': 0.84; 'sudden': 0.84; 'url:rebertia': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=jr833iTuDaFJmyirfa/GqmSvma/036Mms0SVytFUvNY=; b=AEcHB/Hoocr1sXRFH0079740bA5Yj3lFPdx46A+ke3QY39zscbLAQCFtdHEponDb06 T/1NE4+t5i+XQppcvZU3AFQJ9CWwmiSqq1+ZtqAWDlo795brv7Qc1g3uf3O7aKmCaTFW r+CPsJKyzrxBCJ7TPgMqIFfeFl4niA+j8C1Yo= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Sat, 6 Aug 2011 01:04:30 -0700 X-Google-Sender-Auth: LjT3ySmRh7b7cizsP-4XS2cQBF0 Subject: Re: Calling super() in __init__ of a metaclass From: Chris Rebert To: Eli Bendersky Content-Type: text/plain; charset=UTF-8 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312617874 news.xs4all.nl 23907 [2001:888:2000:d::a6]:40329 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10964 On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky wrote: > Consider this standard metaclass definition: > > class MyMetaclass(type): > =C2=A0 =C2=A0def __init__(cls, name, bases, dct): > =C2=A0 =C2=A0 =C2=A0 =C2=A0super(MyMetaclass, cls).__init__(name, bases, = dct) > =C2=A0 =C2=A0 =C2=A0 =C2=A0# do meta-stuff > > class Foo(object): > =C2=A0 =C2=A0__metaclass__ =3D 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: > > =C2=A0 =C2=A0If the second argument is a type, issubclass(type2, type) mu= st 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