How to Invoke the Super Constructor of the Parent Class in Python
Inheritance is a powerful concept of object-oriented programming in computer science that helps us avoid writing redundant code and goes well with the golden DRY rule of programming (Don’t repeat yourself). Using inheritance, a class can inherit all the properties and functions of another class into it. The class from which the functionality is inherited is the parent class, and the classes that inherit the functionality are child classes.
Inheritance help programmers and developers write scalable, reusable, and maintainable code, which improves productivity and efficiency.
To inherit all the attributes and methods, we must execute the parent class’s constructor from the child classes. In this article, we will learn how to invoke the super constructor of the parent class in Python.
Invoke the Super Constructor of the Parent Class in Python
The constructor of the parent class or the super constructor is invoked in the constructors of the child classes. The syntax for the same is as follows.
super().__init__(*args, **kwargs)
All the arguments required by the parent class are passed inside the __init__()
method. In the above syntax, *args, **kwargs
covers all the arguments, but one can also individually mention all the arguments as follows.
super().__init__(argument1, argument2, argument3, ...)
Now that we are done with the theory, let us understand the concept practically. Refer to the following Python code to understand the concept of inheritance and how to invoke a super constructor.
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def get_name(self):
return self.name
def set_name(self, name):
if isinstance(name, str):
self.name = name
def get_age(self):
return self.age
def set_age(self, age):
if isinstance(age, int):
self.age = age
def get_gender(self):
return self.gender
def set_gender(self, gender):
if isinstance(gender, str):
self.gender = gender
class Student(Person):
def __init__(self, name, age, gender, roll_number):
super().__init__(name, age, gender) # Important
self.roll_number = roll_number
def get_roll_number(self):
return self.roll_number
def set_roll_number(self, roll_number):
if isinstance(roll_number, int):
self.roll_number = roll_number
adam = Student("Adam", 21, "M", 5)
emma = Student("Emma", 23, "F", 18)
print("Name:", adam.get_name())
print("Age:", adam.get_age())
print("Gender:", adam.get_gender())
print("Roll Number:", adam.get_roll_number())
print()
print("Name:", emma.get_name())
print("Age:", emma.get_age())
print("Gender:", emma.get_gender())
print("Roll Number:", emma.get_roll_number())
Output:
Name: Adam
Age: 21
Gender: M
Roll Number: 5
Name: Emma
Age: 23
Gender: F
Roll Number: 18
The Person
class is the parent class, and the Student
class is the child class. The parent and child classes have setters and getters for all its attributes. The child class inherits all the attributes and the methods of the parent class. This statement super().__init__(name, age, gender)
invoked the Person
class’s constructor. Note that it is necessary to invoke the parent class’s constructor; otherwise, the code will break.