473,785 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

super() not a panacea?


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
class also providing that method. This results in a situation where the
first base class is now required to use super in order to propogate the
call chain over to the new classes, in the case of the object being an
instance of the newly-added subclass, but still must not use super in
the case of the object being an instance of the original (bottom of
diamond) class.

So, are we asking too much of super? Is there any resolution to this
problem? The only one we can see is that a super object somehow effect a
no-op when the search for a method ends with the "object" class (and
"object", of course, doesn't implement the sought method). This seems
yucky, though.
print ' ',r'''Typical diamond using super.
A
/ \
B C
\ /
D
'''

class A(object):
def m(self):
print ' A'

class B(A):
def m(self):
super(B, self).m()
print ' B'

class C(A):
def m(self):
super(C, self).m()
print ' C'

class D(B,C):
def m(self):
super(D, self).m()
print ' D'

print '''create D instance and call m'''

D().m()

print r'''A C B D, looks good. Now introduce classes Z & X
A Z
/|\ /
/ | \ /
/ | \/
B C X
\ /
D
'''

class Z(object):
def m(self):
print ' Z'

class X(A, Z):
def m(self):
super(X, self).m()
print ' X'

print '''create X instance and call m'''

X().m()

print '''"A X", Oh-oh, that is not right. Z.m was not called.
That is because A is not calling super.
Change class A to call super.'''

class A(object):
def m(self):
super(A, self).m()
print ' A'

class B(A):
def m(self):
super(B, self).m()
print ' B'

class C(A):
def m(self):
super(C, self).m()
print ' C'

class D(B,C):
def m(self):
super(D, self).m()
print ' D'

class Z(object):
def m(self):
print ' Z'

class X(A, Z):
def m(self):
super(X, self).m()
print ' X'

X().m()

print '"Z A X", That is much better.'
print 'Now, make sure D still works as before.'

try:
D().m()
except AttributeError, e:
print ' Error:', e
print ' ',"super object has no attribute 'm'!, now what?"
Jul 18 '05 #1
2 1678
"Clarence Gardner" <cl******@netlo jix.net> wrote in message news:<pa******* *************** ******@netlojix .net>...
The super object is considered a solution to the "diamond problem".
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
class also providing that method.


The solution I see is to introduce a placeholder base class Y
with a dummy "m" method on top of the hierarchy:

class Y(object):
def m(self):
pass

class A(Y):
def m(self):
super(A, self).m()
print ' A'

class B(A):
def m(self):
super(B, self).m()
print ' B'

class C(A):
def m(self):
super(C, self).m()
print ' C'

class D(B,C):
def m(self):
super(D, self).m()
print ' D'

class Z(Y):
def m(self):
print ' Z'

class X(A, Z):
def m(self):
super(X, self).m()
print ' X'

X().m()

D().m()
print X.mro()
print D.mro()

This is the easy solution. If you don't like it, let me know and I
will show you other possibilities.

Michele Simionato
Jul 18 '05 #2
Here is another solution that you will probably like more, since it does
not require to change the hierarchy. The trick is to use a custom "super"
which ignores attribute errors:

class mysuper(super):
def __getattribute_ _(self,name):
try:
return super.__getattr ibute__(self,na me)
except AttributeError: # returns a do-nothing method
return lambda *args, **kw: None

class A(object):
def m(self):
mysuper(A, self).m()
print ' A'

class B(A):
def m(self):
mysuper(B, self).m()
print ' B'

class C(A):
def m(self):
mysuper(C, self).m()
print ' C'

class D(B,C):
def m(self):
mysuper(D, self).m()
print ' D'

class Z(object):
def m(self):
print ' Z'

class X(A, Z):
def m(self):
mysuper(X, self).m()
print ' X'

X().m()

D().m()
print X.mro()
print D.mro()
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2331
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() '''
11
5035
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'
0
1240
by: Michele Simionato | last post by:
Here is an idea for a nicer syntax in cooperative method calls, which is not based on Guido's "autosuper" example. This is just a hack, waiting for a nicer "super" built-in ... Here is example of usage: from cooperative import Cooperative class B(Cooperative): def print_(self):
0
1514
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 ...
6
2014
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' ....
7
5260
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
7
1932
by: Pupeno | last post by:
Hello, I have a class called MyConfig, it is based on Python's ConfigParser.ConfigParser. It implements add_section(self, section), which is also implemented on ConfigParser.ConfigParser, which I want to call. So, reducing the problem to the bare minimum, the class (with a useless add_section that shows the problem): .... def add_section(self, section): .... super(MyConfig, self).add_section(section)
9
2202
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"
4
1722
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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,...
0
10350
Oralloy
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...
0
10157
jinu1996
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...
1
10097
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,...
0
9957
tracyyun
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...
0
8983
agi2029
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...
0
5386
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...
3
2887
bsmnconsultancy
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...

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.