Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between super() and calling superclass directly

In Python 2.7 and 3, I use the following method to call a super-class's function:

class C(B):     def __init__(self):         B.__init__(self) 

I see it's also possible to replace B.__init__(self) with super(B, self).__init__() and in python3 super().__init__().

Are there any advantages or disadvantages to doing this either way? It makes more sense to call it from B directly for me at least, but maybe there's a good reason where super() can only be used when using metaclasses (which I generally avoid).

like image 296
johannestaas Avatar asked Feb 07 '14 23:02

johannestaas


People also ask

What's the difference between super () and super?

The super keyword in Java is a reference variable that is used to refer parent class objects. The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.

What are two differences between a super class method call and a superclass constructor call?

Both are used in a subclass as a way to invoke or refer to its superclass. super() is a method call you make inside a constructor or inside an overridden method to invoke the superclass's constructor or the superclass's overridden method. The super() call can also take arguments in these cases.

What does super () do in Python?

The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What is the difference between class and super class?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).


1 Answers

For single inheritance, super() is just a fancier way to refer to the base type. That way, you make the code more maintainable, for example in case you want to change the base type’s name. When you are using super everywhere, you just need to change it in the class line.

The real benefit comes with multiple inheritance though. When using super, a single call will not only automatically call the method of all base types (in the correct inheritance order), but it will also make sure that each method is only called once.

This essentially allows types to have a diamond property, e.g. you have a single base type A, and two types B and C which both derive from A. And then you have a type D which inherits from both B and C (making it implicitely inherit from A too—twice). If you call the base types’ methods explicitely now, you will end up calling A’s method twice. But using super, it will only call it once:

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

When we now instantiate D, we get the following output:

>>> D() A B C D <__main__.D object at 0x000000000371DD30> 

Now, let’s do all that again with manually calling the base type’s method:

class A2 (object):     def __init__ (self):         print('A2')  class B2 (A2):     def __init__ (self):         A2.__init__(self)         print('B2')  class C2 (A2):     def __init__ (self):         A2.__init__(self)         print('C2')  class D2 (C2, B2):     def __init__ (self):         B2.__init__(self)         C2.__init__(self)         print('D2') 

And this is the output:

>>> D2() A2 B2 A2 C2 D2 <__main__.D2 object at 0x0000000003734E48> 

As you can see, A2 occurs twice. This is usually not what you want. It gets even messier when you manually call method of one of your base types that uses super. So instead, you should just use super() to make sure everything works, and also so you don’t have to worry about it too much.

like image 155
poke Avatar answered Sep 29 '22 04:09

poke