Python · The language, not just syntax
How does inheritance and the MRO work in Python?
Short answer
Python supports single and multiple inheritance. Attribute lookup follows the C3 linearisation recorded in __mro__, and super() walks that order cooperatively rather than jumping to one fixed parent.
When a class is defined, Python computes a method resolution order using C3 linearisation. It preserves the order of bases and ensures a consistent, monotonic lookup for diamond-shaped hierarchies. print(Cls.__mro__) shows the exact path.
class Base:
def describe(self):
return "base"
class Logged(Base):
def describe(self):
return "logged " + super().describe()
class Cached(Base):
def describe(self):
return "cached " + super().describe()
class Service(Logged, Cached):
pass
print(Service().describe())
print([cls.__name__ for cls in Service.__mro__])Using inheritance well
- Subclass when the subtype genuinely is a specialisation and honours the parent contract
- Use mixins for small, orthogonal behaviour such as logging or retry support
- Use abstract base classes to define required methods and catch incomplete implementations
- Prefer composition when the relationship is has-a rather than is-a
Deep hierarchies become fragile because a change in a base class can alter many subclasses. In tests, inheritance is often harder to isolate than composition with injected collaborators. If a subclass overrides a method and ignores the parent's contract, callers relying on the base type will break.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How does C3 linearisation resolve a diamond hierarchy?
- 2What does super() actually do in multiple inheritance?
- 3When would you choose a mixin over a base class?
- 4How do abstract base classes help enforce a contract?
Say it in the interview
Python resolves attributes through the MRO, which is computed with C3 linearisation. super is cooperative, so in multiple inheritance it moves to the next class in that order rather than to one specific parent. I use inheritance only for true is-a relationships, mixins for small cross-cutting behaviour, and abstract base classes to enforce contracts. When the relationship is has-a, composition keeps the design simpler and easier to test.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.