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.
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
- List several parents in brackets.
- Lookup follows __mro__.
- super() goes to the next class in the MRO.
Code examples
The diamond and the MRO
['Child', 'Left', 'Right', 'Base'] ['Child', 'Left', 'Right', 'Base', 'object']
Mixins add capabilities
User(name='Asha', age=30)
{"age": 30, "name": "Asha"}More examples
Cooperative __init__ with **kwargs
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:
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
Prefer
Explicit parent calls ignore the MRO and can run shared code twice.
Mixins with their own state and constructors
Avoid
Prefer
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
- 1Write a ComparableMixin that implements __lt__ using a key() method, and use it in two classes.
- 2Build 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?