Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does Python\'s super() work 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. The super() function follows Python's Method Resolution Order (MRO) to determine which method to call in complex inheritance hierarchies.
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 ?
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()
The output of the above code is ?
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 ?
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()
The output of the above code is ?
Srinivas Anjali
Initializing Parent Classes with super()
We can also use the super() function to initialize attributes in the constructor of parent classes. However, with multiple inheritance, we need to be careful as super() only calls the next class in the MRO chain ?
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):
# Explicit calls to avoid MRO issues with multiple inheritance
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()
The output of the above code is ?
Father: Srinivas Mother: Anjali
Method Resolution Order (MRO) with super()
Python follows a specific order when resolving methods in multiple inheritance called the Method Resolution Order (MRO). The super() function ensures that methods are called in the correct order based on this 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()
# Check the MRO
print("MRO:", D.__mro__)
d = D()
d.show()
The output of the above code is ?
MRO: (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>) D's show method B's show method C's show method A's show method
Cooperative Inheritance Example
For proper cooperative inheritance with multiple inheritance, all classes should use super() and accept **kwargs to pass arguments along the MRO chain ?
class Person:
def __init__(self, name, **kwargs):
self.name = name
super().__init__(**kwargs)
print(f"Person initialized: {name}")
class Employee:
def __init__(self, emp_id, **kwargs):
self.emp_id = emp_id
super().__init__(**kwargs)
print(f"Employee initialized: {emp_id}")
class Manager(Person, Employee):
def __init__(self, name, emp_id, department, **kwargs):
self.department = department
super().__init__(name=name, emp_id=emp_id, **kwargs)
print(f"Manager initialized: {department}")
# Create a Manager instance
m = Manager("Alice", "E001", "IT")
print(f"Manager: {m.name}, ID: {m.emp_id}, Dept: {m.department}")
The output of the above code is ?
Employee initialized: E001 Person initialized: Alice Manager initialized: IT Manager: Alice, ID: E001, Dept: IT
Conclusion
Python's super() function works with multiple inheritance by following the Method Resolution Order (MRO). Use explicit parent class calls when you need to initialize multiple parents, and use cooperative inheritance with **kwargs for complex hierarchies. Understanding MRO is crucial for working effectively with multiple inheritance in Python.
