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. 3 2672
7stud <bb**********@y ahoo.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
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 -------------
On 8/16/07, 7stud <bb**********@y ahoo.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.co m> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 function on pages 89-90. Based on the
example on page 90, I wrote this test code :
class A(object):
def test(self):
print 'A'
|
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. It's much
nicer for writing cooperative classes. It does have more overhead, but
at this stage I'm not so concerned about that - the important thing is
it actually works.
Note that this uses sys._getframe() magic ...
|
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):
def meth(self, arg):
super(C, self).meth(arg)
|
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 inheritance when the
base classes are written to expect it, so "Always use super()" seems
like bad advice. OTOH sometimes you need super() to get correct
behaviour. ISTM "Only use super() when you know you need it" might be
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |