Updated April 14, 2023
Introduction to super() in Python
The built-in function super() is a mechanism to refer parent class without naming it in Python. Super() creates a temporary object that helps in accessing the parent class’s methods and attributes. It’s like super() delegates accessing functionality it to an object during run time.
Rather than naming parent class explicitly, just the “super()” function helps you do so in a simple way. From single inheritance to multiple inheritances, it has great significance. We will see it through examples, going further. Along with this, super() uses the concept of MRO while dealing with Multiple inheritances, which will also be showcased as part of this.
Syntax in Python 3:
super().methodName(arguments)
Need:
The function super() helps you modify/enhance any module without even getting to know the base class’s details, which has been extended.
Examples of super() in Python
Here are the following examples mention below
Example #1
Code:
class ParentClass(object):
def __init__(self):
print("This is Base class")
pass
class ChildClass(ParentClass):
def __init__(self):
ParentClass.__init__(self)
c1 = ChildClass()
Output:
As one can notice, the above showcased is the case of single inheritance. “ParentClass” is the base class, or one can say parent class; however “, ChildClass” is the child class.
Here we initialized “ParentClass” within the child class to access the parent class.
But to make this process more abstract and easier, the super() function is developed.
Code:
class ParentClass(object):
def __init__(self):
print("This is Base class")
pass
class ChildClass(ParentClass):
def __init__(self):
super(ChildClass, self).__init__()
c1 = ChildClass()
Output:
As one notices, the parent class is not accessed directly by name. The function super() does it easily for us.
Example #2
The functionality of super() in multiple inheritances.
Multiple Inheritance:
Fig 1: Multiple Inheritance
Multiple inheritance is a type of inheritance where a child class inherits characteristics and features from more than one parent class.
There is a function issubclass() to check if the child class is a derivative of the parent class.
For the code shown below, if you do:
"issubclass(ChildClass,ParentClass1)" will return "True".
"issubclass(ParentClass1,ChildClass)" will return "False".
Code:
class ParentClass1(object):
def __init__(self):
print("This is Base class 1")
pass
class ParentClass2(object):
def __init__(self):
print("This is Base class 2")
pass
class ChildClass(ParentClass1,ParentClass2):
def __init__(self):
super(ChildClass, self).__init__()
c1 = ChildClass()
Output:
One might be wondering over the result; we are getting here.
Let’s understand: In the case of multiple inheritances, Python looks for attribute left to right.
So here, “ParentClass1” will be looked at first. And it will look for ParentClass1.__init()__, and print.
Example #3
Now let’s see parent and child class to attributes and methods.
Code:
class ParentClass:
def __init__(self):
self.parent_attribute = 'I am a parent'
def parent_method(self):
print('Good old days')
class ChildClass(ParentClass):
def __init__(self):
super().__init__()
self.child_attribute = 'I am a child'
def child_method(self):
print('New is awesom')
# Create instance of child class i.e. "ChildClass"
child = ChildClass()
# Print attributes and methods of child class
print(child.child_attribute)
print(child.parent_attribute)
child.parent_method()
child.child_method()
Output:
One can notice how super has helped accessing attributes and methods of the parent class, and forward compatibility becomes easier.
There are few points to be taken care of, to use super():
- The method referred to by super must exist in the parent class.
- Caller and callee class must have the same argument signature.
Example #4
Now let’s understand the very important concept of Multiple resolution orders in python.
Code:
class BB:
def check(self):
print('This is b')
class CC:
def check(self):
print('This is c')
class DD(BB, CC):
def check(self):
print('This is d')
d_inst = DD()
d_inst.check()
Output:
This is the obvious output.
Now lets comments check method in DD:
Code:
class BB:
def check(self):
print('This is b')
class CC:
def check(self):
print('This is c')
class DD(BB, CC):
#def check(self):
# print('This is d')
pass
d_inst = DD()
d_inst.check()
Output:
As one can notice, the “BB” parent class will be referred. And as the function is also present, so it printed “This is b”.
Now, let’s comment method of BB as well.
Code:
class BB:
#def check(self):
# print('This is b')
pass
class CC:
def check(self):
print('This is c')
class DD(BB, CC):
#def check(self):
# print('This is d')
pass
d_inst = DD()
d_inst.check()
Output:
Now, because python refers to parent class from left to right. And first parent class “BB” doesn’t have a method in it; it will refer to “CC”, and its method will be accessed. Hence, “This is c” printed.
MRO is a simple algorithm.
When there is a case of multiple inheritances, to resolve the accessibility of the method from the parent class, it is used. It creates routing series of processing classes and then follows the structure. This is like tree routing, which works like:
- At first, it will look for the method in instance class.
- If the method not found in the instance class, it will look for the first parent class method. Else in the second parent class.
Hence, in our case, it refers to classes in order: DD, BB, CC.
Conclusions
As we saw above, how important is the concept of super in inheritance? It helps you replace Parentname.method in single inheritance. Multiple inheritances is a very widely used concept in a real-life scenario, and henceforth knowing function super() makes it even more prominent. MRO is the algorithm which is used to resolve the accessibility of the method when multiple parent class exists.
Recommended Articles
We hope that this EDUCBA information on “super() in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.