← Python Code Modern OOP
Browse Python Concepts

Method Resolution Order (MRO) in Python

Mental Model

Imagine Python's MRO as trying to create a single, non-conflicting family tree branch that honors all parental lineages without ambiguity. If you list Base directly, then list AuditMixin (which also inherits Base), it creates a conflict because Base would appear twice in different positions, making the lineage path impossible to linearize consistently.

Rule: When designing multiple inheritance hierarchies, always specify derived classes or mixins before base classes in the parent inheritance list.

The Setup

You are designing a modular plugin system where mixins are used to inject auditing capabilities. You organize classes in an inheritance hierarchy, but minor changes to order structure prevent the interpreter from starting up.

What Does This Print?

Broken code
Python
class Base:
    pass

class AuditMixin(Base):
    pass

# Misordered inheritance chain declaration
class DatabaseConnection(Base, AuditMixin):
    pass
Predict what happens when you run or import this code. Will DatabaseConnection class compile or fail with an error?

The Output

What actually happens
TypeError: Cannot create a consistent method resolution order (MRO) for bases Base, AuditMixin

Python throws a runtime definition-time TypeError: Cannot create a consistent method resolution order (MRO). The interpreter refuses to define the DatabaseConnection class because its structure violates the fundamental rules of Python's inheritance engine.

Why Python Does This

Python uses the C3 Linearization algorithm to determine how methods are resolved across multiple inherited classes. The algorithm enforces two primary constraints: child classes must always be searched before parent classes, and the order of class declaration inside bases must be preserved. In class DatabaseConnection(Base, AuditMixin), the developer specifies that Base should be searched before AuditMixin. However, AuditMixin inherits from Base, meaning AuditMixin must be searched before Base. These constraints are contradictory, causing C3 Linearization to fail and raise an MRO generation exception.

The Fix

Corrected pattern
Python
class Base:
    pass

class AuditMixin(Base):
    pass

# Correct subclassing order: place specific subclasses/mixins before base classes
class DatabaseConnection(AuditMixin, Base):
    pass

Correctly ordering the base classes (derived/mixin first, then general base) allows Python's C3 Linearization algorithm to construct a consistent Method Resolution Order. This ensures a unique, unambiguous path for attribute and method lookups in the inheritance chain.

How This Fails in Real Systems

A large-scale telemetry collection framework updated class hierarchies across global analytics plugins. A developer introduced a Base class dependency out of order in a mixin wrapper. Upon package deployment, the dependency injection layer crashed during startup initialization, causing a 1-hour downtime for analytics tracking engines.

Key Takeaway

When designing multiple inheritance hierarchies, always specify derived classes or mixins before base classes in the parent inheritance list.
Common mistake: Developers define multiple inheritance relationships without considering the C3 Linearization algorithm, leading to inconsistent MROs when a base class appears both directly and as an ancestor of another parent.