Introduction π
The **Diamond Problem** occurs in multiple inheritance when a class inherits from two parent classes that both inherit from the same base class. This creates ambiguity:Which parentβs method should be called?
Note
1. What Does the Diamond Look Like? π·
diamond_structure.txt
A
/ \
B C
\ /
Dβ B and C both inherit from A
β D inherits from both B and C
β D should call A's method only once β Python handles this via MRO
2. Diamond Problem Example π§±
diamond_problem.py
class A:
def show(self):
print("A show")
class B(A):
def show(self):
print("B show")
class C(A):
def show(self):
print("C show")
class D(B, C):
pass
d = D()
d.show()β Output: B show
β Because Python checks classes in **MRO order**
3. How Python Resolves This: MRO π
check_mro.py
print(D.mro())Sample Output:
[D, B, C, A, object]
Note
4. Diamond Problem with super() π
The super() function does NOT call the immediate parent. Instead, it follows **MRO order**, ensuring each class executes only once.
super_diamond.py
class A:
def show(self):
print("A")
super().show()
class B(A):
def show(self):
print("B")
super().show()
class C(A):
def show(self):
print("C")
super().show()
class D(B, C):
def show(self):
print("D")
super().show()
d = D()
d.show()β Output (MRO order):D β B β C β A
β Each class runs only once β avoids duplication
β No ambiguity or repeated outputs
5. Why MRO Is Important in Diamond Inheritance π§
- β Prevents calling the same method multiple times
- β Ensures consistent method lookup
- β Avoids ambiguity when two parents share methods
- β Makes super() work safely in complex inheritance
6. Real-World Example β Device System π₯οΈ
device_diamond.py
class Device:
def start(self):
print("Device started")
class Computer(Device):
def start(self):
print("Computer started")
super().start()
class Phone(Device):
def start(self):
print("Phone started")
super().start()
class SmartDevice(Computer, Phone):
def start(self):
print("SmartDevice starting")
super().start()
sd = SmartDevice()
sd.start()β Output follows MRO (SmartDevice β Computer β Phone β Device)
β Each class executes exactly once
7. MRO Rules (C3 Linearization) π
Pythonβs MRO follows these principles:
- β Child class is always checked first
- β Parents are checked left-to-right
- β A class only appears once in the final order
- β Method resolution respects inheritance hierarchy
8. Incorrect Diamond Handling (What Not to Do) β
wrong_diamond.py
class B(A):
def show(self):
print("B")
A.show(self) # β manually calling A breaks MRONote
9. Best Practices π‘
- β Always use super() in shared methods
- β Check MRO with Class.mro() when unsure
- β Avoid overwriting methods unless necessary
- β Keep class hierarchies clean and logical
- β Use single inheritance where possible; use multiple only when needed
Conclusion π
super() working together.β β¨You now fully understand **Diamond Problem Handling** in Python! Want the next topic? Try MRO, super(), Magic Methods, Method Overriding, or Cooperative Multiple Inheritance. Just tell me! π