✅ Yes, Python does support multiple inheritance —
meaning a class can inherit attributes and methods from more than one parent class.
However, this flexibility can sometimes create ambiguity and conflicts, known as the “Diamond Problem.”
Python solves this intelligently using the Method Resolution Order (MRO) and C3 Linearization algorithm.
🔹 1. What is Multiple Inheritance?
In multiple inheritance, a class can derive from more than one parent class.
Syntax:
class Child(Parent1, Parent2):
# class body
pass🧩 Example:
class A:
def show(self):
print("Class A")
class B:
def display(self):
print("Class B")
class C(A, B): # Multiple inheritance
pass
obj = C()
obj.show() # From A
obj.display() # From BOutput:
Class A
Class B✅ The class C inherits both from A and B.
🔹 2. What is the Diamond Problem?
The Diamond Problem occurs when multiple parent classes inherit from a common base class,
and a child class inherits from those parent classes, creating a diamond-shaped hierarchy.
Example Structure:
A
/ \
B C
\ /
DCode Example:
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
class C(A):
def show(self):
print("Class C")
class D(B, C):
pass
obj = D()
obj.show()Output:
Class B🔹 3. Why the Diamond Problem Happens
- Both classes B and C inherit from A and override
show(). - Class D inherits from both B and C.
- When we call
obj.show(), Python must decide which version ofshow()to use.
This ambiguity (B or C?) is the Diamond Problem.
🔹 4. How Python Solves It — MRO (Method Resolution Order)
Python uses the C3 Linearization Algorithm to determine the order in which base classes are searched when executing a method.
To see this order, use:
print(D.mro())Output:
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]✅ So the search order is:
👉 D → B → C → A → object
Hence, when we called obj.show(), Python executed B.show() because B appears before C in the MRO.
🔹 5. Understanding MRO Rules
MRO follows C3 Linearization, ensuring:
- Subclass before superclass (child before parent).
- Left-to-right order of inheritance as declared in the class.
- No class appears twice in the final MRO.
Example:
class X: pass
class Y: pass
class Z(X, Y): pass
print(Z.mro())Output:
[<class '__main__.Z'>, <class '__main__.X'>, <class '__main__.Y'>, <class 'object'>]🔹 6. Example with super() in Multiple Inheritance
Using super() with multiple inheritance ensures all parent classes are called properly in MRO order.
🧩 Example:
class A:
def show(self):
print("A called")
class B(A):
def show(self):
print("B called")
super().show()
class C(A):
def show(self):
print("C called")
super().show()
class D(B, C):
def show(self):
print("D called")
super().show()
obj = D()
obj.show()Output:
D called
B called
C called
A called✅ Notice:
- All parent classes (
B,C,A) were called exactly once. - The order followed the MRO sequence:
D → B → C → A
This avoids the diamond problem entirely.
🔹 7. Checking MRO in Python
You can view the method resolution order in two ways:
print(D.__mro__)
# or
print(D.mro())Example Output:
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)🔹 8. Summary Table
| Feature | Description |
|---|---|
| Multiple Inheritance | A class inherits from more than one base class. |
| Diamond Problem | Ambiguity when multiple parent classes inherit from a common ancestor. |
| Python’s Solution | Uses MRO (C3 Linearization) to define a consistent lookup order. |
| MRO Order Rule | Left to right, depth-first, no duplicates. |
| Keyword Used | super() — ensures cooperative method calling. |
✅ In Summary
| Concept | Explanation |
|---|---|
| Supports Multiple Inheritance? | ✅ Yes, Python allows it. |
| Diamond Problem Exists? | ⚠️ Theoretically yes, but Python’s MRO solves it. |
| MRO Algorithm | C3 Linearization (resolves method lookup order). |
| How to View MRO | ClassName.mro() or ClassName.__mro__ |
| Best Practice | Always use super() when overriding methods in multiple inheritance. |
✅ In short:
Python supports multiple inheritance, and the diamond problem is solved through the Method Resolution Order (MRO) using C3 Linearization, ensuring each parent class is called exactly once in a predictable order.
