Python Super() Function: Usage & Examples
In Python, the super() function is used to call methods from a parent class. It is mainly used in a subclass to call methods from the parent class in order to extend or override them in the subclass.
A common use of the super() function is to call the parent class constructor in the constructor of a subclass, for example:
class ParentClass:
def __init__(self, name):
self.name = name
class ChildClass(ParentClass):
def __init__(self, name, age):
super().__init__(name)
self.age = age
In the example above, the ChildClass inherits from the ParentClass and calls the parent class’s constructor in its own constructor to initialize the parent class’s properties. This way, the child class can extend the functionality of the parent class without having to re-implement the parent class’s methods.