SyntaxStudy
Sign Up
Python Multiple Inheritance & MRO
Python Intermediate 10 min read

Multiple Inheritance & MRO

Multiple Inheritance & MRO

Python allows a class to inherit from more than one parent. The Method Resolution Order (MRO) determines which parent's method is called first.

Multiple Inheritance

class Flyable:
    def move(self):
        return "I fly"
    def describe(self):
        return "I can fly"

class Swimmable:
    def move(self):
        return "I swim"
    def describe(self):
        return "I can swim"

class Duck(Flyable, Swimmable):
    def describe(self):
        fly  = Flyable.describe(self)
        swim = Swimmable.describe(self)
        return f"Duck: {fly} and {swim}"

d = Duck()
print(d.move())       # "I fly" (Flyable listed first)
print(d.describe())   # Duck: I can fly and I can swim

MRO with super()

print(Duck.__mro__)
# (Duck, Flyable, Swimmable, object)

# super() follows MRO — not just "the parent"

Diamond Problem

class A:
    def greet(self): return "A"

class B(A):
    def greet(self): return f"B -> {super().greet()}"

class C(A):
    def greet(self): return f"C -> {super().greet()}"

class D(B, C):
    pass

print(D().greet())   # B -> C -> A
print(D.__mro__)     # D, B, C, A, object
Example
class JSONMixin:
    def to_json(self):
        import json
        return json.dumps(self.__dict__, default=str)

class LogMixin:
    def log(self):
        print(f"[{type(self).__name__}] {self.__dict__}")

class User(JSONMixin, LogMixin):
    def __init__(self, name, email):
        self.name = name
        self.email = email

u = User("Alice", "alice@example.com")
u.log()
print(u.to_json())
print(User.__mro__)
Pro Tip

Use mixins (small, focused classes) to compose behavior via multiple inheritance. Avoid deep, complex hierarchies — prefer flat mixin chains.