NEXT TUTORIAL: Method Overloading in Python
super() statement in Python is a built-in method which is used to call the constructor or method of super class from the subclass.
Since we know that if a subclass is having its own constructor, then in such case, super class constructor becomes inaccessible from the sub class. Let’s see,
class Programming: # base class
def __init__(self):
self.lang = 'Python'
class Python(Programming):
def __init__(self): # defining constructor for this subclass
self.no_lang = 'Python'
def display(self):
print('Language of Mars: ', self.lang) # trying to access self.lang from Programming class
p = Python()
p.display()
O/P
AttributeError: ‘Python’ object has no attribute ‘lang’
Solution to above problem is to make use of super keyword in Python.
1. We can call super class constructor using the super() method inside the __init__() of subclass.
Let’s see how,
class Programming: # base class
def __init__(self):
self.lang = 'Python'
class Python(Programming):
def __init__(self): # defining constructor for this subclass
super().__init__() # calling constructor of super class
self.no_lang = 'Python'
def display(self):
print('Language of Mars: ', self.lang) # trying to access self.lang from Programming class
p = Python()
p.display()
O/P
Language of Mars: Python
Above code also illustrates the working of super() method with Single inheritance.
2. We can also use super() method in a scenario where we have to call the method of super class that has same name as method in subclass.
Example,
class Rectangle:
def __init__(self, x, y):
print('I came to Rectangle`s init')
self.x = x
self.y = y
def find_area(self):
return self.x * self.y
class Square(Rectangle): # inheriting class Rectangle
def __init__(self, a):
print('I am at Square`s init')
self.a = a
super().__init__(self.a, self.a) # calling super class constructor
def find_area(self):
print('Area of Square is : ',super().find_area()) # calling super class method
s = Square(10)
s.find_area() # calling Square class method
O/P
I am at Square’s init
I came to Rectangle’s init
Area of Square is : 100
3. Using super() to solve the problem of Multiple Inheritance
Not aware of Multiple Inheritance ?
Please refer our tutorial : Inheritance in Python
Problem in Multiple Inheritance:
Suppose a program where we have defined 3 classes like,
class A:
def __init__(self):
print('In A')
class B:
def __init__(self):
print('In B')
class C(A, B):
def __init__(self):
print('In C')
super().__init__()
c = C()
O/P
In C
In A
With the current setup, class C cannot access the constructors of both the super classes
In class C, we have used super().init() statement which will only call the constructor of the class which is at the left side ie. class A, skipping class B.
Similarly, if we have defined class C in this way : class C(B, A)
Then, output would be :
In C
In B
So, this is indeed true that with the current setup, class C cannot access the constructors of both the super classes.
Solution
If class C wants to access all the members of both of its super class, then, the solution is to use super().init() in every class. Like,
class A:
def __init__(self):
print('In A')
super().__init__()
class B:
def __init__(self):
print('In B')
super().__init__()
class C(A, B):
def __init__(self):
print('In C')
super().__init__()
c = C()
O/P
In C
In A
In B
That’s all about super() method we know.
NEXT TUTORIAL: Method Overloading in Python
