Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'explicitly.': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'from:addr:web.de': 0.23; 'figure': 0.26; 'there.': 0.27; 'pass': 0.29; 'class': 0.29; 'print': 0.29; '"in': 0.30; 'parent': 0.30; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'skip:_ 10': 0.38; 'to:addr:python.org': 0.40; 'maarten': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: newb __init__ inheritance Date: Thu, 08 Mar 2012 18:03:25 +0100 Organization: None References: <1c6db866-6fa3-4de5-96de-51d6720a1300@x17g2000yqj.googlegroups.com> <13988849.1044.1331224217273.JavaMail.geo-discussion-forums@ynnk21> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ac8a.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331226223 news.xs4all.nl 6858 [2001:888:2000:d::a6]:45091 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21390 Maarten wrote: > Alternatively you can figure out the parent class with a call to super: This is WRONG: > super(self.__class__, self).__init__() You have to name the current class explicitly. Consider: >> class A(object): ... def __init__(self): ... print "in a" ... >>> class B(A): ... def __init__(self): ... print "in b" ... super(self.__class__, self).__init__() # wrong ... >>> class C(B): pass ... >>> Can you figure out what C() will print? Try it out if you can't. The corrected code: >>> class B(A): ... def __init__(self): ... print "in b" ... super(B, self).__init__() ... >>> class C(B): pass ... >>> C() in b in a <__main__.C object at 0x7fcfafd52b10> In Python 3 you can call super() with no args; super().__init__() do the right thing there.