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__(alsoCls.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
objectis last. super()does not mean "my parent": it means "the next class after this one in the MRO oftype(self)", which is what makes cooperative multiple inheritance (each method callingsuper()) 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**kwargsand 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.