In Python, super() function is used to call methods from a parent (superclass) inside a child (subclass). It allows to extend or override inherited methods while still reusing the parent's functionality.
Let's understand how super() works with an example where a child class calls a method from its parent class.
class Parent:
def show(self):
print("This is Parent class method")
class Child(Parent):
def show(self):
super().show() # Calling parent method
print("This is Child class method")
obj = Child()
obj.show()
Output
This is Parent class method This is Child class method
Explanation:
- Parent class has a method show() and child class overrides the same method.
- super().show() calls the parent class method inside the child class.
- This allows the child class to reuse the parent’s functionality and then add its own behavior.
Syntax
super()
Return: Return a proxy object which represents the parent's class.
Why Use super()?
- No need to hardcode parent class names, useful when class hierarchies change.
- Works with single, multiple, and multilevel inheritance.
- Improves code reusability and maintainability.
- Prevents duplicate initialization in complex hierarchies.
Initialize Parent Attributes with super()
In this example, child class uses super() to call the parent class constructor, allowing it to initialize inherited attributes while also adding its own new attribute.
class Emp:
def __init__(self, id, name):
self.id = id
self.name = name
class fun(Emp):
def __init__(self, id, name, email):
super().__init__(id, name) #Calls Emp’s __init__
self.email = email
obj = fun(101, "Olivia", "olivia@email.com")
print(obj.id, obj.name, obj.email)
Output
101 Olivia olivia@email.com
Explanation:
- Emp is the parent class with attributes id and name.
- fun inherits from Emp and adds an email attribute.
- super().__init__(id, name) initializes the parent attributes without rewriting the constructor.
Note: The position of super().__init__() affects the order in which the parent and child class code executes. Placing it before or after other statements changes whether the parent initialization runs first or later.
Effect of Using and Not Using super()
Example 1: In this example, child class overrides the constructor but does not call the parent constructor, which results in missing initialization of inherited attributes.
class Person:
def __init__(self, name, id):
self.name = name
self.id = id
class Emp(Person):
def __init__(self, name, id):
self.name_ = name # Forgot to call Person’s __init__
emp = Emp("Jack", 103)
print(emp.name)
Output
AttributeError: 'Emp' object has no attribute 'name'
Example 2: In this example, super() is used inside the child class constructor to properly call the parent constructor, ensuring that inherited attributes are initialized correctly.
class Person:
def __init__(self, name, id):
self.name = name
self.id = id
class Emp(Person):
def __init__(self, name, id):
super().__init__(name, id)
emp = Emp("James", 103)
print(emp.name, emp.id)
Output
James 103
Explanation:
- Emp overrides the constructor of Person.
- super().__init__(name, id) calls the parent constructor to initialize inherited attributes.
- Creating emp = Emp("James", 103) sets both parent and child attributes correctly.
Using super() in Multilevel Inheritance
In a multilevel inheritance setup, super() ensures that each parent class constructor is executed in the correct sequence. It follows Python’s Method Resolution Order (MRO) to maintain a consistent and predictable flow of initialization.
class Mammal:
def __init__(self, name):
print(name, "is a mammal")
class CanFly(Mammal):
def __init__(self, name):
print(name, "cannot fly")
super().__init__(name)
class CanSwim(CanFly):
def __init__(self, name):
print(name, "cannot swim")
super().__init__(name)
class Animal(CanSwim):
def __init__(self, name):
super().__init__(name)
dog = Animal("Dog")
Output
Dog cannot swim Dog cannot fly Dog is a mammal
Explanation:
- Each class calls super().__init__() to ensure parent constructors run.
- Execution follows the hierarchy: Animal → CanSwim → CanFly → Mammal.
- This ensures each parent constructor runs once in the correct order.