Multiple inheritance lets one class inherit from more than one parent. When it does, Python needs a rule for deciding which version of a method to run when two parents define one with the same name. That rule is the method resolution order, which is the MRO, and you can see it directly on any class.
The previous lesson in this module covered "super() and Overriding Methods Correctly", and the next lesson in this module is about "Properties: Getters, Setters and Validation".
Multiple inheritance is Python's most powerful feature and also its most misused one. It is how the standard library implements mixins like socketserver.ThreadingMixIn, and how frameworks add an optional capability to a base class without changing it. It is also how you end up with a class whose behaviour depends on the order in which you wrote the parents.
How do you see the method resolution order?
Every class has a __mro__ attribute that holds a tuple of itself and all of its ancestors, in the order that Python searches them. It starts with the class itself and it always ends with object.
class Base:
pass
class Left(Base):
pass
class Right(Base):
pass
class Child(Left, Right):
pass
for cls in (Child, Left, Base):
names = []
for parent in cls.__mro__:
names.append(parent.__name__)
print(cls.__name__, '->', names)
Reading that tuple from left to right gives you the search order. Child comes first, then Left, then Right, then Base, and finally object. When you call a method that Child does not define, Python looks in Left first. Only if Left does not define it does the search move on to Right.
The super() function from the previous lesson is the mechanism that walks this list. Each super() call moves one position to the right, which is why a chain of super() calls visits every class in the MRO exactly once.
What happens when two parents define the same method?
The one that appears first in the MRO wins, and that is the one that you listed first in the class statement. Python does not merge the two implementations. The second one is simply not called, unless the first one deliberately delegates to it with super().
class LoggingMixin:
def handle(self, item):
return 'logged ' + item
class RetryingMixin:
def handle(self, item):
return 'retried ' + item
class Worker(LoggingMixin, RetryingMixin):
def handle(self, item):
return super().handle(item)
class OtherWorker(RetryingMixin, LoggingMixin):
def handle(self, item):
return super().handle(item)
print('logging first :', Worker().handle('job'))
print('retrying first:', OtherWorker().handle('job'))
That is the practical meaning of "the MRO decides". Write class Worker(LoggingMixin, RetryingMixin) and you get logging first. Swap the order and you get retrying first. The order of the parent list is a real design decision, and it has consequences that you can observe.
How do multiple parents cooperate?
The pattern that makes multiple inheritance manageable is called cooperative multiple inheritance. Every mixin's method delegates to super() and forwards what comes back, so the chain composes all of the behaviours instead of stopping at the first one.
class BaseProcessor:
def process(self, item):
return 'processed ' + item
class RetryMixin:
def process(self, item):
return 'retried(' + super().process(item) + ')'
class TimingMixin:
def process(self, item):
return 'timed(' + super().process(item) + ')'
class LoggingMixin:
def process(self, item):
return 'logged(' + super().process(item) + ')'
class Pipeline(LoggingMixin, TimingMixin, RetryMixin, BaseProcessor):
def process(self, item):
return super().process(item)
print(Pipeline().process('job-1'))
names = []
for parent in Pipeline.__mro__:
names.append(parent.__name__)
print(names)
Compare that output with the previous example. Here all three behaviours appear, because each class hands off to the next one. If any one of them left out the super() call, then everything after it in the chain would be skipped, and the output would quietly lose a step.
This is why the mixins in the standard library are written this way. ThreadingMixIn does not know which class it will be mixed into. It only knows that it should do its own work and then let the next class in the MRO continue.
What is the diamond problem?
It appears when a class inherits from two parents that share a common ancestor. The classic diagram has a D at the bottom, B and C in the middle, and A at the top, where A inherits from both B and C.
class D:
def handle(self, item):
return 'D handled ' + item
class B(D):
def handle(self, item):
return 'B -> ' + super().handle(item)
class C(D):
def handle(self, item):
return 'C -> ' + super().handle(item)
class A(B, C):
def handle(self, item):
return 'A -> ' + super().handle(item)
print(A().handle('x'))
names = []
for parent in A.__mro__:
names.append(parent.__name__)
print('D appears once in the MRO:', names.count('D'))
Python's MRO solves the ordering half of this problem. D appears exactly once in the tuple, at the end, so the cooperative chain does not visit it twice. What the MRO does not solve on its own is the behaviour. If B and C both call super() and the method in D has a side effect such as printing, then that side effect still happens once, but it happens at the point where the chain reaches D and not at the top.
The practical advice is to keep shared ancestors shallow and their behaviour free of side effects. A mixin that only adds a method and forwards everything upwards is safe. A base class with mutable state that two branches inherit from is where the confusion starts.
When should you actually use multiple inheritance?
Mostly for mixins, which are small and focused classes that add one capability and are combined at the end. A line like class MyHandler(LoggingMixin, TimeoutMixin, BaseHandler) reads clearly, and each mixin stays independently testable.
Avoid it when you are tempted to use it so that you do not have to duplicate a method. If two classes need the same behaviour, the answer is usually a shared base class or a module-level function, and not a second parent.
| What you want to express | How to express it |
|---|---|
| a shared capability with no state | a mixin, combined last in the parent list |
| a contract that several classes must satisfy | an abstract base class with @abstractmethod |
| shared implementation code | a base class, or a module-level helper |
| an optional second implementation | composition with an injected collaborator |
Also avoid it when the parents belong to unrelated areas. Inheriting from both File and Connection because you want both sets of methods produces a class with no coherent meaning. Composition handles that case better, and the last lesson in this module covers it in full.
In summary
Cls.__mro__shows the search order, with the class itself first andobjectlast.- When two parents define the same method, the one that is earlier in the MRO wins.
- Cooperative mixins call
super()and forward the result, so all of the behaviours compose. - Leaving out a
super()call quietly drops everything that comes later in the chain. - Use multiple inheritance for focused mixins, and not to avoid duplication.
Try it yourself: build a mixin stack
Write three mixins that have no state of their own. The first is LoggingMixin with a process(item) method, the second is TimingMixin with a process(item) method, and the third is RetryMixin with a process(item) method. Each one calls super().process(item) and wraps the result.
Combine them as class Pipeline(LoggingMixin, TimingMixin, RetryMixin): and check that the output contains all three contributions in the right order. Now swap two of the parents and confirm that the order of the output changes while all three contributions remain. Finally, print Pipeline.__mro__ and account for every name in the tuple.