Multiple Inheritance
Inheriting from more than one parent class at once.
One Child, Many Parents
In Python, a class can inherit from more than one parent class. This means the child gets methods and attributes from all of its parents. It is a powerful feature, but one that should be used thoughtfully.
Basic Example
class Flyable:
def fly(self):
print("Flying!")
class Swimmable:
def swim(self):
print("Swimming!")
class Duck(Flyable, Swimmable):
def quack(self):
print("Quack!")
d = Duck()
d.fly() # From Flyable
d.swim() # From Swimmable
d.quack() # Own method
The Duck class inherits from both Flyable and Swimmable, gaining abilities from each parent while also having its own methods.
The Mixin Pattern
A mixin is a small, focused class that adds a single capability. Mixins are not meant to stand alone -- they are designed to be combined with other classes. This is the safest way to use multiple inheritance.
class LogMixin:
def log(self, message):
print(f"[LOG] {message}")
class JsonMixin:
def to_json(self):
import json
return json.dumps(self.__dict__)
class User(LogMixin, JsonMixin):
def __init__(self, name, email):
self.name = name
self.email = email
u = User("Alice", "alice@mail.com")
u.log("User created") # [LOG] User created
print(u.to_json()) # {"name": "Alice", "email": "alice@mail.com"}
Tip: Name your mixins with the "Mixin" suffix so other developers know they are meant to be combined, not used alone.
Method Resolution Order (MRO)
When a class has multiple parents, Python needs a way to decide which method to call if parents have methods with the same name. Python uses C3 linearization to create a predictable order. You can inspect it yourself.
# See the method resolution order
print(Duck.__mro__)
# (Duck, Flyable, Swimmable, object)
# Python checks in this order:
# 1. Duck itself
# 2. Flyable (first parent)
# 3. Swimmable (second parent)
# 4. object (base of everything)
When to Use Multiple Inheritance
Safe to Use
- Small mixins with one purpose each
- Interface-like abstract classes
- Combining unrelated capabilities
Use with Caution
- Deep hierarchies with multiple parents
- Parents with overlapping method names
- Parents that share a common ancestor
Best Practices
- Prefer mixins over complex multiple inheritance trees.
- Keep each parent class focused on a single responsibility.
- Use
ClassName.__mro__to understand the resolution order.
Common Mistakes
Not considering method name conflicts between parent classes.
Creating deep inheritance chains with multiple parents -- this becomes very hard to debug.
Forgetting that parent order matters: class D(B, C) is different from class D(C, B).
Practice Exercises
Exercise 1: Robot
Create Walkable and Talkable classes. Then create a Robot class that inherits from both. Test all methods.
Exercise 2: Custom Mixins
Create a TimestampMixin (adds created_at) and ValidateMixin (adds validate()). Combine them in a Post class.
Exercise 3: Check MRO
Create a class that inherits from 3 parents. Print its __mro__ and predict which method gets called when parents share a method name.
Summary
- A class can inherit from multiple parents using
class Child(Parent1, Parent2). - Mixins are small, focused classes that add one capability -- the safest pattern.
- Python uses MRO (C3 linearization) to resolve method conflicts predictably.
- Parent order in the class definition matters for resolution.
- Next up: The Diamond Problem -- what happens when parents share a common ancestor.