Here's some code from Python in a Nutshell. The comments are lines from
a previous example that the calls to super replace in the new example:
class A(object):
def met(self):
print 'A.met'
class B(A):
def met(self):
print 'B.met'
# A.met(self)
super(B, self).met()
class C(A):
def met(self):
print 'C.met'
# A.met(self)
super(C, self).met()
class D(B, C):
def met(self):
print 'D.met'
# B.met()
# C.met()
super(D, self).met()
Then you call D().met()
Now, I understand that the commented code would cause A.met to be called
twice. But why does the second version (with super) not also do this? I
guess my problem lies in not understanding exactly what the super
function returns.
super(D, self).met() seems like it would return something that has to do
with both B and C, which each in turn return a superobject having to do
with A, so why isn't A.met called twice still?
Thanks! 4 1563
John Salerno wrote:
Here's some code from Python in a Nutshell. The comments are lines from
a previous example that the calls to super replace in the new example:
class A(object):
def met(self):
print 'A.met'
class B(A):
def met(self):
print 'B.met'
# A.met(self)
super(B, self).met()
class C(A):
def met(self):
print 'C.met'
# A.met(self)
super(C, self).met()
class D(B, C):
def met(self):
print 'D.met'
# B.met()
# C.met()
super(D, self).met()
Then you call D().met()
Now, I understand that the commented code would cause A.met to be called
twice. But why does the second version (with super) not also do this? I
guess my problem lies in not understanding exactly what the super
function returns.
super(D, self).met() seems like it would return something that has to do
with both B and C, which each in turn return a superobject having to do
with A, so why isn't A.met called twice still?
Thanks!
Basically super(class_, self).method looks in self's mro (it's list of
base classes) for class class_, and then starts searching *after* class
class_ for the next class that implements the method.
In this case the object's (instance of D) mro will be (D, B, C, A,
object), so as super gets called in each class, it looks in that list
(tuple, whatever) for the class following it (actually the next class
following it that implements the method).
Since no class appears in that list more than once, each class's
implementation of the method will only be called once.
HTH,
~Simon
Also, if you haven't already, read: http://www.python.org/download/relea...o/#cooperation
Simon Forman wrote:
In this case the object's (instance of D) mro will be (D, B, C, A,
object), so as super gets called in each class, it looks in that list
(tuple, whatever) for the class following it (actually the next class
following it that implements the method).
Since no class appears in that list more than once, each class's
implementation of the method will only be called once.
But after super(D, self).met() is called, doesn't that then call both
super(B, self).met() and super(C, self).met()? If so, how does that
avoid calling A.met twice? Or is that not what's happening?
Here's what I think gets called, in order, after running D().met():
print 'D.met'
super(D, self).met()
print 'B.met'
super(B, self).met()
print 'A.met'
print 'C.met'
super(C, self).met()
print 'A.met'
John Salerno wrote:
But after super(D, self).met() is called, doesn't that then call both
super(B, self).met() and super(C, self).met()? If so, how does that
avoid calling A.met twice? Or is that not what's happening?
If you have an instance of a B then super(B,self).m et() will call A.met(),
but if self is actually an instance of a D, then super(B,self).m et()
actually calls C.met().
That is why super needs both the class and the instance: so it can jump
sideways across the inheritance diamond instead of always passing calls to
the base of the current class.
Duncan Booth wrote:
John Salerno wrote:
>But after super(D, self).met() is called, doesn't that then call both super(B, self).met() and super(C, self).met()? If so, how does that avoid calling A.met twice? Or is that not what's happening?
If you have an instance of a B then super(B,self).m et() will call A.met(),
but if self is actually an instance of a D, then super(B,self).m et()
actually calls C.met().
That is why super needs both the class and the instance: so it can jump
sideways across the inheritance diamond instead of always passing calls to
the base of the current class.
Oh, I think I get it! So what's happening is this:
1. the MRO in this case is always (D, B, C, A, object)
2. super(D, self).met() looks in B
3. super(B, self).met() looks in C
4. super(C, self).met() looks in A
Right? So it's like making a ladder? I guess my confusion came when I
thought that a call to super(B, self).met() also called A.met, but I
guess it stops at C and lets C call it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kerim Borchaev |
last post by:
Hello!
Always when I use "super" I create a code duplication because class
used as first arg to "super" is always the class where the method
containing "super" was defined in:
'''
class C:
def method(self):
super(C, self).method()
'''
|
by: Clarence Gardner |
last post by:
The super object is considered a solution to the "diamond problem".
However, it generally requires that the ultimate base class know that it
is last in the method resolution order, and hence it should not itself
use super (as well as supplying the ultimate implementation of an
overridden method.)
However, a problem comes up if that ultimate base class is also the base
class for another which inherits from it and another independent base...
|
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: Steven Bethard |
last post by:
When would you call super with only one argument? The only examples I
can find of doing this are in the test suite for super. Playing around
with it:
py> class A(object):
.... x = 'a'
....
py> class B(A):
.... x = 'b'
....
|
by: Brent |
last post by:
I'd like to subclass the built-in str type. For example:
--
class MyString(str):
def __init__(self, txt, data):
super(MyString,self).__init__(txt)
self.data = data
| |
by: Mike Krell |
last post by:
I'm reading Alex Martelli's "Nutshell" second edition. In the section
called "Cooperative superclass method calling", he presents a diamond
inheritance hierachy:
class A(object):
def met(self):
print "A.met"
class B(A):
def met(self):
print "B.met"
|
by: Pyenos |
last post by:
Approach 1:
class Class1:
class Class2:
def __init__(self):self.variable="variable"
class Class3:
def method():print Class1().Class2().variable #problem
Approach 1.1:
|
by: ashitpro |
last post by:
Understanding Ext-2 file system:chapter 1
The first block in each Ext2 partition is never managed by the Ext2 filesystem,
because it is reserved for the partition boot sector.
The rest of the Ext2 partition is split into block groups,
each of which has the layout shown in Image 2.
As you will notice from the figure, some data structures must fit in exactly one block,
while others may require more than one block.
All the block groups...
|
by: matheussousuke |
last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails.
The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg
instead of
http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg
As u see, there's the website URL before the image URL.
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
| |