If you’ve used Python for object oriented programming, you might have encountered some inheritance going on. There is often a parent class and a child class that inherits from the parent.
According to online resources (references below), super()
function returns a temporary object that allows reference to the parent class so that the child class can have access to the parent’s attributes and methods. This helps reducing the amount of the code to write and also, more importantly, allows inheritance as a child class will have attributes and methods that its parent has.
super().method_a()
will look for method_a()
in the parent class of the current class.
Here is some example:
class Person:
name = ""
age = 0
def __init__(self, name, age):
self.name = name
self.age = age
def show_name(self):
print(self.name)
class Student(Person):
studentId = ""
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.studentId = student_id