Python's super() with Multiple Inheritance



Python supports a feature known as multiple inheritance, where a class (child class) can inherit attributes and methods from more than one parent class. This allows for a flexible and powerful way to design class hierarchies.

To manage this, Python provides the super() function, which facilitates the calling of methods from a parent class without explicitly naming it.

Understanding Multiple Inheritance

In multiple inheritance, a child class can derive attributes and methods from multiple parent classes. This can be particularly useful in scenarios where you want to compose behavior from different sources.

Example

In the following example, the Child class inherits from both 'Father' and 'Mother' classes. The 'parent' method in the 'Child' class accesses attributes from both parent classes and prints them.

class Father:
    fathername = ""
    
    def father(self):
        print(self.fathername)

class Mother:
    mothername = ""
    
    def mother(self):
        print(self.mothername)

class Child(Father, Mother):
    def parent(self):
        print("Father:", self.fathername)
        print("Mother:", self.mothername)

s1 = Child()
s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.parent()

Following is the output for the above code -

Father: Srinivas
Mother: Anjali

Using 'super()' in Multiple Inheritance

The super() function in Python is used to call methods from a parent class without explicitly mentioning the parent's name. This is especially useful in a multiple inheritance scenario where the method resolution order (MRO) can be complex.

Example

In the following example, we are going to invoke parent class methods using the super() function.

class Father:
    fathername = ""
    
    def display_father(self):
        print(self.fathername)

class Mother:
    mothername = ""
    
    def display_mother(self):
        print(self.mothername)

class Child(Father, Mother):
    def display_parents(self):
        super().display_father()  # Calls Father's method
        super().display_mother()   # Calls Mother's method

s1 = Child()
s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.display_parents()

Following is the output for the above code -

Srinivas
Anjali

Combining Attributes with 'super()'Function

We can also use the super() function to initialize attributes in the constructor of the parent classes. In a child class constructor, by using super().__init__() to initialize attributes defined in the parent class. It makes code reuse and reduces duplication when creating class hierarchies.

Example

The following example demonstrates how to initialize parent class attributes within the constructor of a child class using explicit calls (but we can also modify it to use `super()`).

class Father:
    def __init__(self, fathername):
        self.fathername = fathername

class Mother:
    def __init__(self, mothername):
        self.mothername = mothername

class Child(Father, Mother):
    def __init__(self, fathername, mothername):
        Father.__init__(self, fathername)
        Mother.__init__(self, mothername)
        
    def display_parents(self):
        print("Father:", self.fathername)
        print("Mother:", self.mothername)

s1 = Child("Srinivas", "Anjali")
s1.display_parents()

Following is the output for the above code -

Father: Srinivas
Mother: Anjali

Method Resolution Order with 'super()' Function

Python follows a specific order when resolving methods in multiple inheritance. By using super(), we can ensure the correct method from the parent's class gets called as per the defined MRO.

Example

In this example, the 'super()' function ensures that the show method of each class in the hierarchy is called in the correct order, based on the MRO.

class A:
    def show(self):
        print("A's show method")

class B(A):
    def show(self):
        print("B's show method")
        super().show()

class C(A):
    def show(self):
        print("C's show method")
        super().show()

class D(B, C):
    def show(self):
        print("D's show method")
        super().show()

d = D()
d.show()

Following is the output for the above code -

D's show method
B's show method
C's show method
A's show method
Updated on: 2025-02-27T19:36:45+05:30

879 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements