Python Multiple Inheritance – OOP Explained with Examples
Learn Python multiple inheritance in object-oriented programming. Understand how classes inherit from multiple parents with clear syntax and real-life examples.
1. What is Multiple Inheritance?
- Definition: A class can inherit attributes and methods from multiple parent classes.
- Comparison:
- Single Inheritance: A class inherits from one parent.
- Multiple Inheritance: A class inherits from two or more parents.
# Single Inheritance
class Parent:
pass
class Child(Parent):
pass
# Multiple Inheritance
class Parent1:
pass
class Parent2:
pass
class Child(Parent1, Parent2):
pass2. Why Use Multiple Inheritance?
- Reuse Code: Combine features from multiple classes.
- Flexibility: Create complex objects by mixing behaviors.
- Example: A
Smartphoneclass can inherit fromPhone,Camera, andComputer.
3. Method Resolution Order (MRO)
- What is MRO?
The order in which Python searches for methods in parent classes. - Rule: Follows the C3 Linearization Algorithm (left-to-right, depth-first, but avoids duplicates).
- Check MRO:
print(Child.__mro__) # Output: Tuple showing the order print(Child.mro()) # Output: List showing the order
Example 1: Simple MRO
class A:
def greet(self):
print("A")
class B(A):
def greet(self):
print("B")
class C(A):
def greet(self):
print("C")
class D(B, C):
pass
d = D()
d.greet() # Output: "B" (because B is first in MRO)
print(D.mro()) # Output: [D, B, C, A, object]4. The Diamond Problem
- What is it?
A class inherits from two classes that share a common ancestor.
# Diamond Structure: D -> B -> A and D -> C -> A
class A:
def process(self):
print("A")
class B(A):
def process(self):
print("B")
super().process()
class C(A):
def process(self):
print("C")
super().process()
class D(B, C):
pass
d = D()
d.process()Output:
B
C
A
Explanation:
- MRO for
D:D -> B -> C -> A -> object. super()inBcallsC, notA, because of the MRO.
5. Using super() in Multiple Inheritance
- Purpose: Call a method from the next class in the MRO.
- Critical for Constructors: Ensures all parent
__init__methods are called.
Example 2: super() in Constructors
class Parent1:
def __init__(self):
print("Parent1 initialized")
super().__init__() # Calls Parent2's __init__
class Parent2:
def __init__(self):
print("Parent2 initialized")
super().__init__() # Calls object's __init__
class Child(Parent1, Parent2):
def __init__(self):
print("Child initialized")
super().__init__()
c = Child()Output:
Child initialized
Parent1 initialized
Parent2 initialized
MRO Flow: Child -> Parent1 -> Parent2 -> object.
6. Mixins: A Practical Use Case
- Mixins: Small classes that add specific features (e.g., logging, serialization).
- Key Rule: Mixins should not stand alone.
Example 3: Mixin for JSON Serialization
class JSONMixin:
def to_json(self):
import json
return json.dumps(self.__dict__)
class Person:
def __init__(self, name):
self.name = name
class Employee(JSONMixin, Person):
def __init__(self, name, salary):
super().__init__(name)
self.salary = salary
emp = Employee("Alice", 50000)
print(emp.to_json()) # Output: {"name": "Alice", "salary": 50000}7. Common Pitfalls & Solutions
-
Name Clashes:
- Problem: Two parents have methods/attributes with the same name.
- Solution: Explicitly call the method from a specific parent.
class Parent1: def greet(self): print("Parent1") class Parent2: def greet(self): print("Parent2") class Child(Parent1, Parent2): def greet(self): Parent2.greet(self) # Explicitly call Parent2's method c = Child() c.greet() # Output: "Parent2" -
Incomplete Initialization:
- Problem: Forgetting
super()in__init__. - Fix: Always use
super().__init__()in parent classes.
- Problem: Forgetting
8. Best Practices
- Avoid Deep Inheritance: Prefer composition over complex hierarchies.
- Use Mixins: Add features without creating monolithic classes.
- Test MRO: Use
Child.mro()to verify the order. - Document Hierarchies: Clarify why multiple inheritance is used.
9. Real-World Example: Game Characters
class Walker:
def walk(self):
print("Walking")
class Swimmer:
def swim(self):
print("Swimming")
class Flying:
def fly(self):
print("Flying")
class SuperHero(Walker, Swimmer, Flying):
pass
hero = SuperHero()
hero.walk() # Output: Walking
hero.swim() # Output: Swimming
hero.fly() # Output: Flying10. When to Avoid Multiple Inheritance
- Complexity: If the code becomes hard to debug.
- Ambiguity: When parent classes have conflicting behaviors.
Key Takeaways
- Use multiple inheritance to combine reusable components.
- Always check the MRO to resolve method conflicts.
super()ensures cooperative inheritance.- Mixins are your friend for adding features cleanly.
By mastering these concepts, you’ll write flexible and powerful Python code! 🚀