QamoosTech
Programming FundamentalsObject-oriented programmingIntermediatePython

Method Resolution Order (MRO)

PronunciationMETH-ud rez-uh-LOO-shun OR-der

Definition

The method resolution order (MRO) is the order in which Python searches a class and its parents for a method. super() follows this order.

Where you hear it

In Python inheritance discussions, interviews about the diamond problem, and debugging which method actually ran.

Examples

  • Print D.__mro__ to see the lookup order.
  • super() calls the next class in the MRO, not necessarily the direct parent.
  • The MRO places the mixin before the base class, so its method runs first.

Common mistake

Assuming super() always means the direct parent. With multiple inheritance it means the next class in the MRO.

Don't confuse with

Method overriding, which is replacing an inherited method. The MRO decides which version is found first.

Say it at work

  • In a meeting or chatCheck the MRO before adding another base class.
  • In an email or ticketThe method resolution order explains why the mixin's version runs first.