Skip to the solution
HardDSA14 min read

What is the Method Resolution Order (MRO)?

684 views

The problem

Question presented to candidate: "When a class inherits from several parents and two of them define the same method, how does Python decide which one runs? Walk me through the MRO, how it is computed, and how super() uses it."

What a strong answer should cover:

  • The MRO is the single, ordered list of classes Python searches for an attribute; it is stored in Cls.__mro__ (also Cls.mro()), and the first class that defines the name wins.
  • It is computed with the C3 linearization: a class comes before its parents, the left-to-right order of the bases is kept, every class appears once, and object is last.
  • super() does not mean "my parent": it means "the next class after this one in the MRO of type(self)", which is what makes cooperative multiple inheritance (each method calling super()) run every class in a diamond exactly once.
  • Some hierarchies have no consistent order; Python refuses them at class-creation time with TypeError: Cannot create a consistent method resolution order (MRO).
  • Practical consequences: mixins go to the left of the main base, cooperative __init__ methods take **kwargs and pass the rest on, and C3 has been the rule since Python 2.3 (still the rule in Python 3).

Clarifying questions expected:

  • Are we talking about method lookup on instances only, or also about how super() chains calls?

Code / implementation expected: Yes โ€” a diamond hierarchy, its __mro__, and a super() chain that shows each class running once.

  • mro
  • inheritance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

14 min read ยท 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

PythonThe diamond, super() and C3 implemented by hand
PythonImpossible MROs, moving super() targets, cooperative __init__

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 128 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextHard

What is TypeVarTuple and Unpack for variadic generics?

Continue