59

I see everywhere examples that super-class methods should be called by:

super(SuperClass, instance).method(args)

Is there any disadvantage to doing:

SuperClass.method(instance, args)
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
IanSR
  • 1,415
  • 3
  • 16
  • 18

2 Answers2

115

Consider the following situation:

class A(object):
    def __init__(self):
        print('Running A.__init__')
        super(A,self).__init__()
class B(A):
    def __init__(self):
        print('Running B.__init__')        
        # super(B,self).__init__()
        A.__init__(self) 

class C(A):
    def __init__(self):
        print('Running C.__init__')
        super(C,self).__init__()
class D(B,C):
    def __init__(self):
        print('Running D.__init__')
        super(D,self).__init__()

foo=D()

So the classes form a so-called inheritance diamond:

    A
   / \
  B   C
   \ /
    D

Running the code yields

Running D.__init__
Running B.__init__
Running A.__init__

That's bad because C's __init__ is skipped. The reason for that is because B's __init__ calls A's __init__ directly.

The purpose of super is to resolve inheritance diamonds. If you un-comment

# super(B,self).__init__()

and comment-out

A.__init__(self) 

the code yields the more desireable result:

Running D.__init__
Running B.__init__
Running C.__init__
Running A.__init__

Now all the __init__ methods get called. Notice that at the time you define B.__init__ you might think that super(B,self).__init__() is the same as calling A.__init__(self), but you'd be wrong. In the above situation, super(B,self).__init__() actually calls C.__init__(self).

Holy smokes, B knows nothing about C, and yet super(B,self) knows to call C's __init__? The reason is because self.__class__.mro() contains C. In other words, self (or in the above, foo) knows about C.

So be careful -- the two are not fungible. They can yield vastly different results.

Using super has pitfalls. It takes a considerable level of coordination between all the classes in the inheritance diagram. (They must, for example, either have the same call signature for __init__, since any particular __init__ would not know which other __init__ super might call next, or else use **kwargs.) Furthermore, you must be consistent about using super everywhere. Skip it once (as in the above example) and you defeat the entire purpose of super. See the link for more pitfalls.

If you have full control over your class hierarchy, or you avoid inheritance diamonds, then there is no need for super.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    +1 for the super-harmful link. I was about to post it myself. – Sven Marnach Feb 17 '11 at 20:47
  • 4
    [Python's super() considered super!](http://rhettinger.wordpress.com/2011/05/26/super-considered-super/) shows why and how to use `super()`. btw, with new-style classes and multiple inheritance there is always an inheritance diamond due to common `object` parent. – jfs Oct 05 '12 at 20:52
  • +1 for "(They must, for example, all have the same call signature for __init__, since any particular __init__ does not know which other __init__ super might call next.)". As I see it, this signature coordination puts the "harmful" in super() ... of course one can have something like __init__(self, *args, **kwargs) but if you can externalize your method/constructor meaning via its very signature why making it obscure ? – Shmil The Cat Mar 21 '13 at 11:00
  • 1
    But since the call signature of object.__ini___ is no arguments, then none of the inits can have multiple arguments. You can't as far as I can see use the *args, **kwargs method because at some point A is called and it calls object.__init__ with arguments and everything goes wrong. Or am I missing something (I surely am)? I can't find good examples which use __init__'s with arguments. – Francis Davey Jan 15 '14 at 21:12
  • 1
    @FrancisDavey: You can have a base class which does not call `super`, and/or you have to rely on the subclasses reducing `args` and `kwargs` so as to be empty by the time `object.__init__` is called. See [Raymond Hettinger's article](http://rhettinger.wordpress.com/2011/05/26/super-considered-super/) for a great exposition including examples. – unutbu Jan 15 '14 at 21:33
  • Thanks. What it turns out is that I have a diamond A, B, C, D which ought to be just like this but for some reason only D, B and A are being called. C doesn't get a look in and I can't work out why. That is the more fundamental problem rather than the problem I thought was there. Are there any examples with more complex use of __init__ that pass arguments around a diamond? That would be useful. – Francis Davey Jan 15 '14 at 21:36
  • Yes, there are examples in the article I linked to above. (Note the use of mandatory positional arguments, followed by `**kwargs`. The mandatory arguments are removed from `kwargs` when you call `super(...).__init__(**kwargs)`. So each time you call `super` the `kwargs` is getting smaller....) – unutbu Jan 15 '14 at 21:42
  • Thanks. That is very helpful and has helped me put together a working diamond multiple inheritance. – Francis Davey Jan 16 '14 at 14:59
9

There's no penalty as-is, though your example is somewhat misguided. In the first example, it should be

super(SubClass, instance).method(args)  # Sub, not SuperClass

and that leads me to quote the Python docs:

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).

Basically, by using the first method you don't have to hard-code your parent class in there for single-class hierarchies, and you simply can't really do what you want (efficiently/effectively) using the second method when using multiple inheritance.

Community
  • 1
  • 1
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • 1
    Do I understand it right, there are no cons to using `super` in single inheritance situations? – Wolf Feb 02 '15 at 10:08