Hello Python programmer, In this guide, we will go through the Python super() function using examples.
super function in Python is a powerful function to give access to the method and properties of parent or base class.
the super function gives us authority to ignore the base class name to call its attributes and methods from the child class or derived class.
Table of Contents
Python super function
Python super() function is used to access all the methods and properties from the base class or parent class.
super() function return an object that represents the parent class.
In Python, super() has two major use cases:
- super() function us to avoid using the base class name explicitly
- super() working with Multiple Inheritance
Syntax
The syntax of super function in Python is:-
super()
Parameter
super function in Python does not support any parameter.
Python super function example
Here we will take some examples to understand super function in Python.
Using Base class name:
class Employee:
def __init__(self):
self.name = 'John'
self.role = 'Programmer'
class Doctor(Employee):
def __init__(self):
self.var = 'Instance variable in B'
self.name = 'Vishvajit'
self.role = 'Student'
Employee.__init__(self)
x = Doctor()
print(x.var)
print(x.name)
print(x.role)
Output
Instance variable in B
John
Programmer
Without using the super function:
class Employee:
def __init__(self):
self.name = 'John'
self.role = 'Programmer'
class Doctor(Employee):
def __init__(self):
self.var = 'Instance variable in B'
self.name = 'Vishvajit'
self.role = 'Student'
x = Doctor()
print(x.var)
print(x.name)
print(x.role)
Output
Instance variable in B
Vishvajit
Student
With using super() function:
class Employee:
def __init__(self):
self.name = 'John'
self.role = 'Programmer'
class Doctor(Employee):
def __init__(self):
self.var = 'Instance variable in B'
self.name = 'Vishvajit'
self.role = 'Student'
super().__init__()
x = Doctor()
print(x.var)
print(x.name)
print(x.role)
Output
Instance variable in B
John
Programmer
In the above example, we called the __init__() method of the Employee class from the Doctor class using super.__init__() instead of using Employee.__init__().
Since we don’t need to specify the base class name to call its methods and properties. We can easily call the base class methods and properties by using the super() function.
Multiple inheritance:
Python super() function can also used with multiple inheritance.
class Person:
def __init__(self, name1):
print(name1 , " is person")
class Doctor(Person):
def __init__(self, name2):
print(name2, "is doctor")
super().__init__(name2)
class Engineer(Doctor):
def __init__(self, name3):
print(name3, "is Engineer")
super().__init__(name3)
x = Engineer('Vishvajit')
Output
Vishvajit is Engineer
Vishvajit is doctor
Vishvajit is person
Conclusion
In this article, you have learned Python super function along with examples. The super function is the best built-in function to access the base class attributes and methods.
If you’ll work with Inheritance in Python, you can’t ignore Python super function.
Python built-in functions
For more information:- Click Here