[Python-Dev] Re: super() harmful?

James Y Knight foom at fuhm.net
Wed Jan 5 17:55:54 CET 2005


I'm not sure why super got dragged into this, but...

On Jan 4, 2005, at 9:02 PM, Guido van Rossum wrote:
> I think that James Y Knight's page misrepresents the issue. Quoting:

> But __init__ *is* special, in that it is okay for a subclass __init__
> (or __new__) to have a different signature than the base class
> __init__; this is not true for other methods. If you change a regular
> method's signature, you would break Liskov substitutability (i.e.,
> your subclass instance wouldn't be acceptable where a base class
> instance would be acceptable).

You're right, some issues do apply to __init__ alone. However, two 
important ones do not:

The issue of mixing super() and explicit calls to the superclass's 
method occur with any method. (Thus making it difficult/impossible for 
a framework to convert to using super without breaking client code that 
subclasses).

Adding optional arguments to one branch of the inheritance tree, but 
not another, or adding different optional args in both branches. 
(breaks unless you always pass optional args as keywordargs, and all 
methods take **kwargs and pass that on to super).

> Super is intended for use that are designed with method cooperation in
> mind, so I agree with the best practices in James's Conclusion:
> [[omitted]]
> But that's not the same as calling it harmful. :-(

The 'harmfulness' comes from people being confused by, and misusing 
super, because it is so very very easy to do so, and so very hard to 
use correctly.

 From what I can tell, it is mostly used incorrectly. *Especially* uses 
in __init__ or __new__. Many people seem to use super in their __init__ 
methods thinking that it'll magically improve something (like perhaps 
making multiple inheritance trees that include their class work 
better), only to just cause a different set of problems for multiple 
inheritance trees, instead, because they don't realize they need to 
follow those recommendations.

Here's another page that says much the same thing, but from the 
viewpoint of recommending the use of super and showing you all the 
hoops to use it right:
http://wiki.osafoundation.org/bin/view/Chandler/UsingSuper

James

PS, I wrote that page last pycon but never got around to finishing it 
up and therefore never really publically announced it. But I told some 
people about it and then they kept asking me for the URL so I linked 
to it, and well, then google found it of course, so I guess it's public 
now. ;)


More information about the Python-Dev mailing list