super() Function in Python π

The super() function is used in inheritance to call methods from the parent class. It allows a child class to use attributes and methods of its parent without explicitly naming the parent class.
1οΈβ£ Why Use super()?
β
Avoids repetitive code β You donβt need to rewrite parent methods.
β
Maintains code flexibility β If the parent class name changes, you donβt have to update references in child classes.
β
Supports multiple inheritance β Ensures the correct method is called following the Method Resolution Order (MRO).
2οΈβ£ Using super() in Method Overriding
Example: Calling Parent Method with super()
class Animal:
def speak(self):
return "Animal makes a sound"
class Dog(Animal):
def speak(self):
original_sound = super().speak() # Call parent method
return original_sound + " but Dog says Woof! Woof!"
dog = Dog()
print(dog.speak())
# Output: Animal makes a sound but Dog says Woof! Woof!
πΉ super().speak() calls the speak() method from Animal.
πΉ The overridden method in Dog extends the parent method.
3οΈβ£ Using super() in the __init__() Constructor
π Calling the Parentβs Constructor ensures attributes are inherited properly.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age) # Call Parent constructor
self.grade = grade # New attribute
student = Student("Alice", 20, "A")
print(student.name) # Output: Alice
print(student.age) # Output: 20
print(student.grade) # Output: A
πΉ super().__init__(name, age) calls the __init__() method from Person.
πΉ The child class Student adds a new attribute (grade) without rewriting existing logic.
4οΈβ£ super() in Multiple Inheritance
π Python follows the MRO (Method Resolution Order) when calling super().
class A:
def show(self):
return "A's show() method"
class B(A):
def show(self):
return super().show() + " β B's show() method"
class C(B):
def show(self):
return super().show() + " β C's show() method"
c = C()
print(c.show())
# Output: A's show() method β B's show() method β C's show() method
πΉ super().show() calls the next method in MRO instead of explicitly naming A.
To check MRO:
print(C.mro())
# Output: [
πΉ Python follows the chain: C β B β A β object.
5οΈβ£ Using super() in Diamond Inheritance (MRO)
π When a class inherits from two classes that share a common ancestor, super() ensures each parent is only called once.
class A:
def show(self):
return "A"
class B(A):
def show(self):
return super().show() + " β B"
class C(A):
def show(self):
return super().show() + " β C"
class D(B, C): # Inherits from both B and C
def show(self):
return super().show() + " β D"
d = D()
print(d.show())
# Output: A β C β B β D
πΉ super() follows C3 Linearization (MRO) to avoid duplicate calls.
πΉ Python resolves the method calls properly to prevent A.show() from running twice.
To see the MRO for D:
print(D.mro())
# Output: [
β Correct method execution order is: A β C β B β D.
6οΈβ£ When to Use super()?
β
When overriding methods β to keep the parentβs behavior while extending it.
β
When working with multiple inheritance β ensures correct MRO.
β
When calling __init__() of parent class β for proper attribute initialization.
π¨ Avoid calling super() without overriding a method, as it may lead to unexpected results.
7οΈβ£ Summary Table
Feature Description
super() Function Calls methods from a parent class.
Method Overriding Extends or modifies a parent method in the child class.
Calling Parent __init__() Ensures attributes are properly inherited.
MRO (Method Resolution Order) Python follows C3 Linearization when using super().
Diamond Inheritance Prevents duplicate parent method calls.
Date: 2025-03-24 00:00:00.000000