Skip to content

Module 11 · Object-Oriented Programming — topic 17 of 25

Multiple inheritance

A class with more than one parent: the method resolution order (MRO), cooperative super() calls, mixin classes, and the diamond problem.

Beginner 12 min read·Lesson 174 of 306 in Python Programming

Learning objectives

  • Read a class's MRO
  • Write cooperative methods that call super()
  • Use small mixin classes to add reusable behaviour

Definition

Multiple inheritance is when a class lists several parent classes: class C(A, B). Python searches for attributes using the method resolution order (MRO), a single linear ordering computed by the C3 algorithm.

Introduction

Sometimes a class naturally combines several independent capabilities: a model that can be saved to JSON and also printed nicely. Multiple inheritance lets one class pick up behaviour from several parents.

Why it matters

Multiple inheritance is powerful but confusing without the MRO. Understanding it explains how frameworks built on mixins work and how super() really behaves.

Detailed explanation

Syntax: class Child(Left, Right): ... Parents are searched left to right, but always after their own subclasses.

The diamond problem: Left and Right both inherit from Base. Python's MRO puts Base only once, after both Left and Right, so its methods run once.

super() doesn't mean 'my parent'; it means 'the next class in the MRO of the object's class'. That is why every class in a cooperative hierarchy must call super().

Cooperative __init__ methods accept **kwargs, take the arguments they need and pass the rest on with super().__init__(**kwargs).

Mixins are small classes that add one capability (to_json, logging, comparison) and are not meant to be used alone. They are the safest form of multiple inheritance.

If two parents define the same method and the MRO picks the wrong one, the design is probably too clever — prefer composition.

Syntax

Python
class JsonMixin:
def to_json(self): ...
class ReprMixin:
def __repr__(self): ...
class User(JsonMixin, ReprMixin):
def __init__(self, name):
self.name = name
print(User.__mro__)
  • List several parents in brackets.
  • Lookup follows __mro__.
  • super() goes to the next class in the MRO.

Code examples

The diamond and the MRO

Python
class Base:
def hello(self):
return ["Base"]
class Left(Base):
def hello(self):
return ["Left"] + super().hello()
class Right(Base):
def hello(self):
return ["Right"] + super().hello()
class Child(Left, Right):
def hello(self):
return ["Child"] + super().hello()
print(Child().hello())
print([c.__name__ for c in Child.__mro__])
Output
['Child', 'Left', 'Right', 'Base']
['Child', 'Left', 'Right', 'Base', 'object']

Mixins add capabilities

Python
import json
class JsonMixin:
def to_json(self):
return json.dumps(vars(self), sort_keys=True)
class ReprMixin:
def __repr__(self):
fields = ", ".join(f"{k}={v!r}" for k, v in vars(self).items())
return f"{type(self).__name__}({fields})"
class User(JsonMixin, ReprMixin):
def __init__(self, name, age):
self.name, self.age = name, age
u = User("Asha", 30)
print(u)
print(u.to_json())
Output
User(name='Asha', age=30)
{"age": 30, "name": "Asha"}

More examples

Cooperative __init__ with **kwargs

Python
class Named:
def __init__(self, name, **kwargs):
super().__init__(**kwargs)
self.name = name
class Timestamped:
def __init__(self, created, **kwargs):
super().__init__(**kwargs)
self.created = created
class Document(Named, Timestamped):
def __init__(self, title, **kwargs):
super().__init__(**kwargs)
self.title = title
d = Document(title="Report", name="Asha", created="2026-11-27")
print(d.title, d.name, d.created)
print([c.__name__ for c in Document.__mro__])
Output
Report Asha 2026-11-27
['Document', 'Named', 'Timestamped', 'object']

Step-by-step explanation

  • Each __init__ takes the keyword it needs and passes the rest along with super().__init__(**kwargs).
  • Following the MRO, Document calls Named, Named calls Timestamped, and Timestamped calls object.
  • By the time object.__init__ runs, every keyword has been consumed, so nothing is left over.

Expected output

Running “Cooperative __init__ with **kwargs” prints exactly:

TextOutput
Report Asha 2026-11-27
['Document', 'Named', 'Timestamped', 'object']

Practical applications

  • Django class-based views combine mixins such as LoginRequiredMixin.
  • socketserver.ThreadingMixIn adds threads to a server class.

Best practices

  • Prefer mixins over deep multiple-inheritance trees.
  • Always use super() in cooperative hierarchies.
  • Inspect Class.__mro__ when behaviour is surprising.

Common mistakes

Calling parents by name in a diamond

Avoid

Left.__init__(self)
Right.__init__(self) # Base.__init__ runs twice

Prefer

super().__init__()

Explicit parent calls ignore the MRO and can run shared code twice.

Mixins with their own state and constructors

Avoid

class LogMixin:
def __init__(self, path): ...

Prefer

class LogMixin:
def log(self, msg): ...

Mixins should add behaviour, not demand constructor arguments.

Important notes

  • If Python cannot build a consistent MRO, class creation fails with TypeError.

Key takeaways

  • A class can inherit from several parents.
  • The MRO decides lookup order; super() follows it.
  • Mixins are the cleanest use of multiple inheritance.

Try it yourself

  1. 1
    Write a ComparableMixin that implements __lt__ using a key() method, and use it in two classes.
  2. 2
    Build a diamond of four classes and predict the MRO before printing it.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • ModerateWhat is printed?
Practise this topic