Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Object-oriented PythonMiddleCommonTheory

What is the Method Resolution Order (MRO), and how does `super()` use it?

Answer

The MRO is the linear order in which Python searches a class and its ancestors when looking up an attribute or method, computed by the C3 linearization algorithm so that a subclass always comes before its parents and a consistent order is preserved across all of a class's base classes. You can inspect it with `ClassName.__mro__`. `super()` does not simply mean "my direct parent class"; it returns a proxy that continues the lookup at the next class in the *current instance's* MRO after the calling class, which is what makes cooperative multiple inheritance work correctly.

Strong answer includes

  • MRO is computed via C3 linearization, inspectable via __mro__
  • subclass always precedes its parents in the order
  • super() continues at the next class in the instance's MRO, not literally the parent

Practical example

In a diamond

Python example
class D(B, C):

where both `B` and `C` inherit from `A` , `D.__mro__` is

Python example
(D, B, C, A, object)

, and calling

Python example
super().__init__()

inside `B` moves on to `C` , not directly to `A` .

#inheritance#mro#super

Sources

Data modelPython Software Foundation · Dunder methods, object identity, attribute lookup and the descriptor and context manager protocolsThe Python Language ReferencePython Software Foundation · Precise scoping, evaluation order and expression semantics