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

7

7stud

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.
 
A

Alex Martelli

7stud said:
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
 
S

Steve Holden

7stud said:
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 -------------
 
E

Evan Klitzke

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top