Super() function in Python

Introduction:-

If you want to understand the python super function, we need to know about Inheritance in Python language. In Python Inheritance, Inheritance is the concept in object-oriented programming in which a class derives (or inherits) attributes and behaviors from another class without needing to implement them again. Click here to read more about Inheritance.

While referring the superclass from the base class, we don’t need to write the name of superclass explicitly.

In the following sections, we will discuss the python super function.

super() with single Inheritance:-

Super – is a built in function that returns a proxy object that allows you to refer your super class .you can refer to super class explicitly using super() function in python. Super() function allows us to refer to super class implicitly.

 
 
>>> class Parent:
         def __init__(self, name):
              print('Parent __init__', name)
  
>>> class Child(Parent):
         def __init__(self):
              print('Child __init__')
              super().__init__('max')
  
>>> child = Child()
    
Output:-
    Child __init__
    Parent __init__ max 

As we can see that we were able to call super class successfully using super() .

MRO (Method resolution order):-

It’s the order in which method should be inherited in the presence of multiple inheritance. You can view the MRO by using __mro__ attribute.

__mro__ attribute would help you see the order in which methods have been executed inside your child class or the parent class.

>>> class Parent:
          def __init__(self, name):
              print('Parent init', name)
>>> class Child(Parent):
          def __init__(self):
              print('Child init')
              super().__init__('max')
>>> child = Child()
>>> print(Child.__mro__)
output:-
 (<class '__main__.Child'>, <class '__main__.Parent'>, <class 'object'>)          

So this is the order in which the methods are called inside our child class and the parent class. And this order means that all the methods

inside the child class will be executed first and then all the methods inside the parent class will be executed.

super() with Multiple Inheritance :-

Let’s try super with in case  of multiple inheritance.

>>> class Parent1:
        def __init__(self, name):
            print('Parent1 __init__', name)
>>> class Parent2:
        def __init__(self, name):
            print('Parent2 __init__', name)
>>> class Child(Parent2, Parent1):
        def __init__(self):
            print('Child __init__')
            super().__init__('max')
>>> child = Child()
>>> print(Child.__mro__)

output:-
 Child __init__
 Parent2 __init__ max
 <class '__main__.Child'>, <class '__main__.Parent2'>, <class '__main__.Parent'> , <class 'object'>)            

We said that this super function is used to refer to the super class and now we’ve 2 super classes that means multiple inheritance .But only 1 output is printed ,which is from init method inside Parent2 , method inside Parent1 has not bee executed.

So how to solve this problem?

If you’ve multiple inheritance then you need to manually call these init functions using the class names. So let’s say I want to first call the init function from parent2 class then init function from parent 1 .Then syntax would be something like below-

>>> class Parent1:
        def __init__(self, name):  
            print('Parent __init__', name) 
>>>  class Parent2: 
         def __init__(self, name): 
         print('Parent2 __init__', name) 
>>>  class Child(Parent2, Parent1): 
         def __init__(self): 
             print('Child __init__')
             Parent2.__init__(self,'max')
             Parent1.__init__(self,'tom')
>>>  child = Child()   
>>>  print(Child.__mro__) 
output:-
 Child __init__ 
 Parent2 __init__ max
 Parent __init__ tom  
 (<class '__main__.Child'>, <class '__main__.Parent2'>, <class '__main__.Parent1'>,<class 'object'>)  
     

So now , as we can see that all the methods from both the classes have been executed.

0

Leave a Reply