473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'super' to only be used for diamond inheritance problems?

I've seen a few discussion about the use of 'super' in Python, including
the opinion that 'super' should only be used to solve inheritance
diamond problem. (And that a constructor that wants to call the
superclass methods should just call them by name and forget about super.)
What is people's opinion on this? Does it make any sense?
Oct 31 '05 #1
4 2747
Alex Hunsley wrote:
I've seen a few discussion about the use of 'super' in Python, including
the opinion that 'super' should only be used to solve inheritance
diamond problem. (And that a constructor that wants to call the
superclass methods should just call them by name and forget about super.)
What is people's opinion on this? Does it make any sense?


<imho>
Since you'll never know how your class will be used, better to stick
with super... super(MyClass, self).dothis() is no more difficult to use
than MySuperClass.do this(self), and does not require much more typing,
so I don't see any reason not to use it.
</imho>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Oct 31 '05 #2
Alex Hunsley <la**@tardis.ed .ac.molar.uk> wrote:
I've seen a few discussion about the use of 'super' in Python, including
the opinion that 'super' should only be used to solve inheritance
diamond problem. (And that a constructor that wants to call the
superclass methods should just call them by name and forget about super.)
What is people's opinion on this? Does it make any sense?


No, it makes no sense whatsoever. Any situation where multiple
inheritance is possible, diamonds or no diamonds, can benefit from
cooperative superclass calling where needed (e.g., __init__). Consider:

class A(B, C): ...

should A know or care at all whether B and C share common bases? Of
course not -- why should A have to be burdened with that knowledge?! To
ensure __init__ is called on each base once and only once,
systematically using super(A, self) is the best and simplest way.

Another way to put it: there's ALWAYS "meshes" (diamonds or more
complicated forms), whenever multiple inheritance occurs, because (in
the newstyle object model, and the other one only exists for legacy
reasons and shouldn't be used in new code) object is always among the
ancestors of EVERY class. So, again: use super.
Alex
Oct 31 '05 #3
Alex Hunsley wrote:
I've seen a few discussion about the use of 'super' in Python,
including the opinion that 'super' should only be used to solve
inheritance diamond problem. (And that a constructor that wants to
call the superclass methods should just call them by name and forget
about super.) What is people's opinion on this? Does it make any
sense?

I personally consider super a half-failure in Python. Some of my reasons are
cited here:
http://fuhm.org/super-harmful/

--
Giovanni Bajo
Nov 1 '05 #4
Giovanni Bajo wrote:
Alex Hunsley wrote:

I've seen a few discussion about the use of 'super' in Python,
including the opinion that 'super' should only be used to solve
inheritance diamond problem. (And that a constructor that wants to
call the superclass methods should just call them by name and forget
about super.) What is people's opinion on this? Does it make any
sense?


I personally consider super a half-failure in Python. Some of my reasons are
cited here:
http://fuhm.org/super-harmful/

It would help to have some clearer guidelines on class (type) usage. I
like James Knight's (fuhm) suggestion with respect to consistent signatures.

Colin W.
Nov 2 '05 #5

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

Similar topics

4
2322
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
5026
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'
9
1706
by: Paul Rubin | last post by:
I'm trying the super() function as described in Python Cookbook, 1st ed, p. 172 (Recipe 5.4). class A(object): def f(self): print 'A' class B(object): def f(self):
7
5246
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
1924
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
2195
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
1716
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):
4
3266
by: Noah | last post by:
Am I the only one that finds the super function to be confusing? I have a base class that inherits from object. In other words new style class: class foo (object): def __init__ (self, arg_A, arg_B): self.a = arg_A self.b = arg_B # Do I need to call __init__ on "object" base class?
10
13290
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>"; print(self.text); def __del__(self): self.text = "</BODY></HTML>"; print(self.text);
0
8380
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
8710
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
8497
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
7310
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...
1
6162
isladogs
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...
0
4150
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...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
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.