By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,629 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,629 IT Pros & Developers. It's quick & easy.

using super() to call two parent classes __init__() method

P: n/a
When I run the following code and call super() in the Base class's
__init__ () method, only one Parent's __init__() method is called.
class Parent1(object):
def __init__(self):
print "Parent1 init called."
self.x = 10

class Parent2(object):
def __init__(self):
print "Parent2 init called."
self.y = 15

class Base(Parent1, Parent2):
def __init__(self):
super(Base, self).__init__()
self.z = 20

b = Base()

--output:--
Parent1 init called.

Aug 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
7stud <bb**********@yahoo.comwrote:
When I run the following code and call super() in the Base class's
__init__ () method, only one Parent's __init__() method is called.
class Parent1(object):
def __init__(self):
print "Parent1 init called."
self.x = 10

class Parent2(object):
def __init__(self):
print "Parent2 init called."
self.y = 15

class Base(Parent1, Parent2):
def __init__(self):
super(Base, self).__init__()
self.z = 20

b = Base()

--output:--
Parent1 init called.
Yep -- Parent1.__init__ doesn't call its own super's __init__, so it
doesn't participate in cooperative superclass delegation and "the buck
stops there".
Alex
Aug 17 '07 #2

P: n/a
7stud wrote:
When I run the following code and call super() in the Base class's
__init__ () method, only one Parent's __init__() method is called.
class Parent1(object):
def __init__(self):
print "Parent1 init called."
self.x = 10

class Parent2(object):
def __init__(self):
print "Parent2 init called."
self.y = 15

class Base(Parent1, Parent2):
def __init__(self):
super(Base, self).__init__()
self.z = 20

b = Base()

--output:--
Parent1 init called.
If you want super() to work for you then all your classes have to use
it. There is only one call to an __init__() method in your definitions -
Parent1 and Parent2 should also be calling their super().__init__().

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 17 '07 #3

P: n/a
On 8/16/07, 7stud <bb**********@yahoo.comwrote:
When I run the following code and call super() in the Base class's
__init__ () method, only one Parent's __init__() method is called.
As the other posters have mentioned, each class needs to make a call
to super. This is because the super call doesn't really call the
parent class' method, it actually calls the next method in the MRO.
The MRO in this case will be Base -Parent1 -Parent2 -object. You
can read the details of Python's MRO here
http://www.python.org/download/releases/2.3/mro/

--
Evan Klitzke <ev**@yelp.com>
Aug 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.