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


Groups > comp.lang.python > #21390

Re: newb __init__ inheritance

From Peter Otten <__peter__@web.de>
Subject Re: newb __init__ inheritance
Date 2012-03-08 18:03 +0100
Organization None
References <1c6db866-6fa3-4de5-96de-51d6720a1300@x17g2000yqj.googlegroups.com> <13988849.1044.1331224217273.JavaMail.geo-discussion-forums@ynnk21>
Newsgroups comp.lang.python
Message-ID <mailman.514.1331226222.3037.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-08 07:25 -0800
  Re: newb __init__ inheritance Maarten <maarten.sneep@knmi.nl> - 2012-03-08 08:30 -0800
    Re: newb __init__ inheritance Peter Otten <__peter__@web.de> - 2012-03-08 18:03 +0100
  Re: newb __init__ inheritance Ethan Furman <ethan@stoneleaf.us> - 2012-03-08 09:34 -0800
  Re: newb __init__ inheritance "Colin J. Williams" <cjw@ncf.ca> - 2012-03-10 12:58 -0500
    Re: newb __init__ inheritance "Colin J. Williams" <cjw@ncf.ca> - 2012-03-10 17:47 -0500
      Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:18 -0700
        Re: newb __init__ inheritance Chris Rebert <clp2@rebertia.com> - 2012-03-11 03:38 -0700
          Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:56 -0700
            Re: newb __init__ inheritance Chris Rebert <clp2@rebertia.com> - 2012-03-11 04:37 -0700
            Re: newb __init__ inheritance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-11 05:40 -0600
            Re: newb __init__ inheritance Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-11 05:52 -0600
            Re: newb __init__ inheritance Peter Otten <__peter__@web.de> - 2012-03-11 13:12 +0100
          Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-11 03:56 -0700
  Re: newb __init__ inheritance hyperboogie <hyperboogie@gmail.com> - 2012-03-12 02:09 -0700

csiph-web