Multiple inheritance — the ability of a class to inherit from multiple parent classes — appeared in Python inspired by other OOP languages (for example, C++), where it helps to create reusable mixins. The development of the method resolution order (MRO) mechanism took a lot of time to ensure predictability in method conflicts.
A class may receive methods with the same name from different base classes. How to determine which method to use when called? Without a clear scheme, this leads to subtle bugs.
Python implements the MRO algorithm (C3 linearization), which defines a strict order of method searching when resolving conflicts in a hierarchy. Everything becomes transparent when using the super() function and the __mro__ method of classes to analyze the inheritance chain.
Example code:
class A: def foo(self): print("A") class B(A): def foo(self): print("B") super().foo() class C(A): def foo(self): print("C") super().foo() class D(B, C): def foo(self): print("D") super().foo() D().foo() print(D.__mro__)
Key features:
__mro__;What happens if parent method names collide?
Answer: The method call will occur in the order defined by the MRO. The first found method with the required name in the chain will be the one that executes.
Can you use super() without explicitly passing self?
Answer: In Python 3 — yes, the call super().method() in the body of a class method is equivalent to the explicit super(Class, self).method(), but only inside the class.
Can the MRO order of an existing class be changed?
Answer: The MRO order is static after the class declaration, but it can be inspected (or a new class can be built with a different inheritance order).
In the project, two mixins with similar methods were created, and the inheriting class uses both mixins but calls methods directly by class name, breaking the sequence of events.
Advantages:
Disadvantages:
Using super() in every method of the mixins ensures calls follow the chain, rather than skipping necessary behavior.
Advantages:
Disadvantages: