Learn how Python's method resolution order works with multiple inheritance: the C3 algorithm, super(), diamond problems, mixins, and common MRO errors.
Understanding Python's Method Resolution Order With Multiple Inheritance
Python lets a class inherit from more than one parent. That is powerful, and it raises a question every developer eventually meets: when two parents define the same method, which one runs?
The answer is the method resolution order, usually shortened to MRO. It is the rule Python uses to decide where to look for an attribute or method, and in what sequence. Once you understand it, multiple inheritance stops feeling unpredictable, super() stops feeling magical, and errors like Cannot create a consistent method resolution order become easy to diagnose.
This guide covers what the MRO is, how Python computes it with the C3 linearization algorithm, how super() follows it, how to build cooperative classes and mixins, and which mistakes to avoid. Every example runs on Python 3.
What Is Method Resolution Order in Python?
When you access obj.some_method, Python searches for the name in a specific sequence of classes. That sequence is the MRO. The first class in the sequence that defines the name wins.
With single inheritance the sequence is obvious: the class itself, its parent, its grandparent, and so on up to object. With multiple inheritance, several valid-looking orders exist, so Python needs a precise, deterministic rule.
The __mro__ attribute returns a tuple, while the mro() method returns a list. Both are described in the official documentation for class.__mro__ and class.mro(). The standard library also offers inspect.getmro() for the same purpose.
Two facts are worth remembering from the start:
object is always the last class in the MRO of a Python 3 class.
The MRO belongs to the class, not to the instance. Every instance of a class shares the same lookup order.
A First Look at Multiple Inheritance
Here is a small hierarchy where the answer is not obvious at a glance:
class A:
def hello(self):
return "A"
class B(A):
pass
class C(A):
def hello(self):
return "C"
class D(B, C):
pass
print(D().hello())
print([cls.__name__ for cls in D.__mro__])
Output:
C
['D', 'B', 'C', 'A', 'object']
Many developers expect A. The reasoning is that D inherits from B, B inherits from A, and A defines hello. A naive depth-first search would walk D, B, A and stop there.
Python does not do that. It places C before A because C is a more specific class that overrides A's behavior. It would be surprising if A's version beat C's override. The MRO respects that intuition.
Why Python Replaced Depth-First Lookup
Early Python versions used a depth-first, left-to-right search for classic classes. That approach has a well-known flaw in diamond-shaped hierarchies: a shared base class can be found before a subclass that overrides it.
Python 2.2 introduced new-style classes with a revised ordering, and Python 2.3 adopted the C3 linearization algorithm. The official write-up, The Python 2.3 Method Resolution Order, explains that C3 was chosen because the earlier ordering was not monotonic. In Python 3 every class is a new-style class, so C3 is the only algorithm you need to know.
C3 guarantees three properties:
Children before parents. A class always appears before any of its base classes.
Local precedence order. If you write class D(B, C), then B appears before C in the MRO of D.
Monotonicity. If class X appears before class Y in the MRO of a parent, it still appears before Y in the MRO of every subclass.
When no ordering can satisfy all three rules, Python refuses to create the class. That refusal is a feature: it turns an ambiguous design into an immediate, visible error.
How the C3 Linearization Algorithm Works
The algorithm is recursive. The linearization of a class C with parents B1, B2, ..., BN is:
The final MRO is Child, Left, Right, Base, object. Notice the key moment in step 2: Base is skipped because Right still has to come before it. That is exactly how C3 ensures that Base is visited only after all of its subclasses.
You can confirm it in code:
print([cls.__name__ for cls in Child.__mro__])
# ['Child', 'Left', 'Right', 'Base', 'object']
Attribute Lookup and the MRO
The MRO is used whenever Python resolves an attribute on a class. For an instance attribute lookup, the order is slightly richer: data descriptors found on the type take priority, then the instance's own __dict__, then non-data descriptors and plain class attributes found through the MRO.
This has a practical consequence. Attributes assigned in __init__ (for example self.name = "x") live on the instance and are not subject to MRO ordering. MRO ordering decides which class's method or class attribute is found, not which parent last wrote an instance attribute.
The same principle explains why special methods behave consistently. Python looks up methods such as __repr__ and __len__ on the type, which means the MRO decides which implementation is used. If you want a deeper look at those hooks, the TVerge guide to the Python data model and special methods covers how they fit together.
How super() Really Works
A very common misconception is that super() means "call my parent class." In reality, super() means "call the next class in the MRO after the current one."
The official documentation for super() describes it as returning a proxy object that delegates method calls to a parent or sibling class of the given type. The word sibling is the important part.
Here is an example that makes the behavior visible:
class A:
def greet(self):
print("A.greet")
class B(A):
def greet(self):
print("B.greet")
super().greet()
class C(A):
def greet(self):
print("C.greet")
super().greet()
class D(B, C):
def greet(self):
print("D.greet")
super().greet()
D().greet()
print([cls.__name__ for cls in D.__mro__])
Look closely at the second and third lines. B does not inherit from C, yet super().greet() inside B ends up in C. That happens because super() consults the MRO of type(self), which is D, and the class after B in that list is C.
Two benefits follow from this:
Each method runs exactly once.A.greet appears a single time, even though both B and C inherit from A.
The chain adapts to the subclass.B does not need to know who comes next. The final class determines the path.
If B had called A.greet(self) directly instead of using super(), C would have been skipped, and A.greet could run twice. Hard-coding parent class names is one of the most frequent causes of duplicated work in multiple inheritance.
Cooperative Multiple Inheritance
For super() chains to work, every class in the chain must cooperate. That means each method calls super() and passes along the arguments it does not handle itself.
The most common place this matters is __init__. Here is a pattern that is reliable in practice, using keyword-only arguments:
class Engine:
def __init__(self, *, horsepower, **kwargs):
super().__init__(**kwargs)
self.horsepower = horsepower
class Electric:
def __init__(self, *, battery_kwh, **kwargs):
super().__init__(**kwargs)
self.battery_kwh = battery_kwh
class HybridCar(Engine, Electric):
def __init__(self, *, model, **kwargs):
super().__init__(**kwargs)
self.model = model
car = HybridCar(model="H1", horsepower=180, battery_kwh=1.5)
print(car.model, car.horsepower, car.battery_kwh)
print([cls.__name__ for cls in HybridCar.__mro__])
Here is what happens. HybridCar.__init__ takes model and forwards the rest. Engine.__init__ takes horsepower and forwards the rest. Electric.__init__ takes battery_kwh and forwards what remains, which is nothing, to object.__init__().
Each class consumes the arguments it owns and hands the others onward. No class needs to know the full hierarchy. Using keyword-only arguments avoids positional mix-ups when the order of base classes changes.
Using Mixins Safely
A mixin is a small class that adds one focused behavior and is not meant to stand on its own. Mixins are the most legitimate everyday use of multiple inheritance in Python, and the MRO decides how they interact with the main base class.
import json
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 JSONMixin:
def to_json(self):
return json.dumps(vars(self))
class User(ReprMixin, JSONMixin):
def __init__(self, name, email):
self.name = name
self.email = email
user = User("Ada", "ada@example.com")
print(user)
print(user.to_json())
Put mixins to the left of the main base class. In class Page(LoggingMixin, BaseView), the mixin's methods are found first and can call super() to reach BaseView.
Keep each mixin small and single-purpose. The more state a mixin owns, the harder the MRO is to reason about.
Make mixins cooperative. If a mixin overrides a method that other classes also define, it should call super().
Avoid requiring a specific __init__ signature. Prefer **kwargs forwarding or no __init__ at all.
Common MRO Errors and How to Fix Them
Inconsistent Hierarchy
The most recognizable MRO error appears when the base class order contradicts the inheritance relationships:
class X:
pass
class Y(X):
pass
class Z(X, Y):
pass
Running this raises:
TypeError: Cannot create a consistent method resolution order (MRO) for bases X, Y
The declaration Z(X, Y) demands that X come before Y. But Y is a subclass of X, so children must come before parents. Both rules cannot hold at once.
The fix is to list the more specific class first:
class Z(Y, X):
pass
print([cls.__name__ for cls in Z.__mro__])
# ['Z', 'Y', 'X', 'object']
Instance Layout Conflicts
Some built-in types cannot be combined because their internal memory layouts clash. For example, inheriting from two unrelated built-ins such as int and str fails with a message about an instance lay-out conflict. This is a different problem from MRO ordering, but it often shows up in the same situations and is worth recognizing.
Broken super() Chains
If one class in the chain forgets to call super(), everything after it in the MRO is silently skipped. When a method seems to "not run" in a multiple inheritance setup, print the MRO and check each class along the path for a missing super() call.
Mismatched Method Signatures
super() chains pass the same arguments from class to class. If one class expects different parameters, you will get a TypeError at call time. Agree on a shared signature or use the **kwargs forwarding pattern shown earlier.
Debugging and Inspecting the MRO
A few habits make MRO problems quick to solve:
Print the MRO first.print([c.__name__ for c in MyClass.__mro__]) answers most questions in seconds.
Use inspect.getmro() if you prefer a function over an attribute.
Check where a name is defined. Loop through the MRO and test name in vars(cls) to find the first class that defines it:
def find_definer(cls, name):
for klass in cls.__mro__:
if name in vars(klass):
return klass
return None
print(find_definer(D, "greet").__name__)
Remember metaclasses. The mro() method can be overridden in a metaclass, which allows a custom ordering. This is an advanced and rarely needed feature, but it explains surprising results in codebases that use custom metaclasses.
Best Practices for Multiple Inheritance
Multiple inheritance is a tool, not a goal. These guidelines keep it manageable:
Prefer composition when behavior is not truly "is-a." Holding a helper object is often clearer than inheriting from it.
Keep hierarchies shallow. Every extra layer makes the MRO harder to reason about.
Use super() everywhere in cooperative classes. Avoid hard-coded calls such as Parent.method(self).
Use keyword arguments for constructors in classes that may be combined.
Document the intended base class order. A short comment explaining why Mixin comes before Base saves future confusion.
Test the MRO. For important hierarchies, add a test that asserts the expected __mro__ so accidental reordering is caught.
The method resolution order is the sequence of classes Python searches when looking up a method or class attribute. It is computed once per class using the C3 linearization algorithm and stored in the class's __mro__ attribute.
How do I see the MRO of a class?
Use ClassName.__mro__ for a tuple or ClassName.mro() for a list. Both show the exact search order, starting with the class itself and ending with object.
Is the MRO depth-first or breadth-first?
Neither strictly. C3 linearization produces an order that respects the declared base order and ensures a class appears before its parents. In a diamond, it visits the left branch, then the right branch, and only then the shared base.
Does super() call the parent class?
Not necessarily. It calls the next class in the MRO of the instance's type, which may be a sibling rather than a direct parent. This is what allows cooperative multiple inheritance to work.
Why do I get "Cannot create a consistent method resolution order"?
The order of your base classes contradicts the inheritance relationships between them. Reorder the bases so subclasses come before their parents, or simplify the hierarchy.
Does the order of base classes matter?
Yes. class D(B, C) and class D(C, B) can produce different MROs and therefore different behavior when both parents define the same method.
Is multiple inheritance bad practice in Python?
Not inherently. Mixins and cooperative designs are widely used and well supported. Problems usually come from deep or tangled hierarchies, non-cooperative classes, and inconsistent signatures.
Conclusion
Python's method resolution order is a precise, predictable rule rather than a mystery. C3 linearization guarantees that children come before parents, that the order you declare is respected, and that the result stays consistent across subclasses. super() builds on that order, which is why it follows the MRO instead of simply calling a parent.
To work confidently with multiple inheritance, remember the essentials: inspect __mro__ whenever behavior surprises you, use super() consistently, forward unknown arguments with **kwargs, keep mixins small, and list more specific classes first. For deeper study, the official Python 2.3 MRO document walks through the algorithm with additional worked examples.
3MutationObserver Guide: Detect DOM Changes in JS & React