473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
3 2639
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
0
by: Delaney, Timothy C (Timothy) | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195 This is a new version of super that automatically determines which method needs to be called based on the existing stack frames....
2
by: Michael P. Soulier | last post by:
Ok, this works in Python on Windows, but here on Linux, with Python 2.4.1, I'm getting an error. The docs say: A typical use for calling a cooperative superclass method is: class C(B):...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.