Multiple Inheritance in Python 🔗🐍

Multiple inheritance is a feature in Object-Oriented Programming (OOP) where a class can inherit from more than one parent class. This allows a child class to combine attributes and methods from multiple sources.
1️⃣ What is Multiple Inheritance?
📌 A child class inherits from multiple parent classes.
📌 The child class gains attributes and methods from all parent classes.
📌 Python uses Method Resolution Order (MRO) to determine which method to execute first.
2️⃣ Basic Example of Multiple Inheritance
# Parent Class 1
class Animal:
def speak(self):
return "Animal makes a sound"
# Parent Class 2
class Walker:
def walk(self):
return "I can walk"
# Child Class (Inheriting from both Animal and Walker)
class Dog(Animal, Walker):
def speak(self): # Overriding the speak method
return "Woof! Woof!"
# Creating an object
dog = Dog()
print(dog.speak()) # Output: Woof! Woof! (from Dog)
print(dog.walk()) # Output: I can walk (from Walker)
🔹 Dog inherits speak() from Animal but overrides it.
🔹 Dog also inherits walk() from Walker.
3️⃣ Method Resolution Order (MRO) 🔄
📌 When multiple parent classes have the same method name, Python follows MRO (Method Resolution Order) to decide which one to call first.
📌 You can check the MRO using .mro() or help(ClassName).
class A:
def show(self):
return "A"
class B:
def show(self):
return "B"
class C(A, B): # Inherits from A first, then B
pass
c = C()
print(c.show()) # Output: A (because A comes first)
print(C.mro()) # Output: [
🔹 Since A is listed before B in class C(A, B), Python calls A’s show() method first.
🔹 MRO follows the "C3 Linearization Algorithm" (Depth-First + Left-to-Right).
4️⃣ Using super() in Multiple Inheritance
📌 super() calls the next method in MRO order, ensuring that all parent classes get initialized properly.
class A:
def __init__(self):
print("Initializing A")
class B:
def __init__(self):
print("Initializing B")
class C(A, B):
def __init__(self):
super().__init__() # Calls A's constructor first (based on MRO)
print("Initializing C")
c = C()
# Output:
# Initializing A
# Initializing C
🔹 super().__init__() only calls A’s constructor, skipping B.
🔹 To call all parent constructors, use super() in each class:
class A:
def __init__(self):
print("Initializing A")
super().__init__()
class B:
def __init__(self):
print("Initializing B")
super().__init__()
class C(A, B):
def __init__(self):
print("Initializing C")
super().__init__()
c = C()
# Output:
# Initializing C
# Initializing A
# Initializing B
🔹 Now super() ensures all parent classes are initialized in the correct MRO order.
5️⃣ When to Use Multiple Inheritance?
✅ When a class needs features from multiple independent classes.
✅ When you want to reuse code from different sources.
✅ When designing mixin classes (small, reusable functionalities).
🚨 Avoid deep inheritance trees, as they can make debugging difficult.
6️⃣ Summary Table
Feature Description
Multiple Inheritance A class inherits from multiple parent classes.
Method Resolution Order (MRO) Determines which method is executed first when multiple parents have the same method.
super() in Multiple Inheritance Ensures all parent classes are properly initialized.
Use Cases Combining functionality from multiple sources, mixins.
Date: 2025-03-24 00:00:00.000000