Advertisement

Multiple Inheritance & MRO interview questions and answers

15 Multiple Inheritance & MRO interview questions for Python roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Intermediate Save

How can you use isinstance() and issubclass() to check relationships in a multiple-inheritance hierarchy?

isinstance(obj, cls) checks if obj's class is cls or any of its ancestors per the full inheritance graph (not just direct parents), and issubclass(cls1, cls2) checks the same relationship between two classes. In multiple inheritance, both correctly consider all branches of the inheritance tree, so an instance of a class combining multiple mixins returns True for isinstance checks against any of those mixins.
class Flyer: pass
class Swimmer: pass
class Duck(Flyer, Swimmer): pass

d = Duck()
print(isinstance(d, Flyer))    # True
print(isinstance(d, Swimmer))  # True
print(issubclass(Duck, Flyer)) # True
Real-world example
A plugin system checks `isinstance(plugin, Cacheable)` to decide whether to wrap a plugin's results in a caching layer, working correctly regardless of how deep Cacheable sits in the plugin's multiple-inheritance chain.
Likely follow-ups
  • How does this interact with virtual subclasses registered via ABCs?
  • What's the performance cost of isinstance checks in a deep hierarchy?
Multiple Inheritance & MRO Metaclasses & Class Customization
Q12 Advanced Save

How would you design a mixin to be safely combinable with any base class's __init__ signature using **kwargs?

A well-designed cooperative mixin accepts **kwargs in its __init__, extracts only the arguments it needs, and forwards the rest via super().__init__(**kwargs) -- so it doesn't need to know the concrete base class's exact signature and remains combinable in different hierarchies without argument mismatches.
class TimestampMixin:
    def __init__(self, **kwargs):
        self.created_at = kwargs.pop('created_at', None)
        super().__init__(**kwargs)  # forward remaining kwargs up the chain

class Model:
    def __init__(self, name):
        self.name = name

class User(TimestampMixin, Model):
    pass

u = User(name='Alice', created_at='2024-01-01')
Real-world example
A reusable AuditMixin used across dozens of unrelated model classes accepts and forwards **kwargs so it works regardless of what specific constructor arguments each concrete model class requires.
Likely follow-ups
  • What happens if a mixin forgets to pop its own kwarg before forwarding?
  • Why is positional-argument-based __init__ risky for mixins?
Multiple Inheritance & MRO Functions & Scope
Q13 Intermediate Save

What's the difference between mixing in an abstract base class (using abc.ABC) versus a plain mixin class?

An ABC-based mixin can declare abstract methods that concrete combining classes must implement, enforced at instantiation time by ABCMeta -- providing a contract, not just shared implementation. A plain mixin class only provides concrete, ready-to-use methods with no enforcement mechanism; combining classes can simply ignore or fail to satisfy any implicit expectations without an error until something actually breaks at runtime.
from abc import ABC, abstractmethod

class Comparable(ABC):
    @abstractmethod
    def compare_key(self): ...
    def __lt__(self, other):
        return self.compare_key() < other.compare_key()

class Product(Comparable):
    def __init__(self, price):
        self.price = price
    def compare_key(self):
        return self.price  # required, enforced by ABC
Real-world example
A sorting mixin defined as an ABC forces every combining class to implement compare_key(), catching the mistake at instantiation time instead of a confusing runtime error deep inside a sort operation.
Likely follow-ups
  • Why combine ABC enforcement with mixin convenience methods like this?
  • How does this differ from a Protocol-based structural approach?
Multiple Inheritance & MRO Metaclasses & Class Customization
Q14 Advanced Save

How does Python handle attribute (not just method) resolution across multiple inheritance, and can it lead to surprising shadowing?

Class-level attribute lookup follows the same MRO as method lookup -- Python searches each class in MRO order for the attribute name and returns the first match. This means a class attribute defined on an earlier class in the MRO shadows one with the same name defined on a later class, which can cause surprising bugs if two unrelated mixins happen to define an attribute with the same name for different purposes.
class MixinA:
    status = 'A-status'

class MixinB:
    status = 'B-status'

class Combined(MixinA, MixinB):
    pass

print(Combined.status)  # 'A-status' -- MixinA wins due to MRO order, MixinB's is shadowed silently
Real-world example
Two independently-authored mixins both happen to define a class attribute named `config`, and combining them silently uses only one mixin's value, causing a hard-to-trace bug until someone inspects the MRO.
Likely follow-ups
  • How would you detect this kind of accidental name collision proactively?
  • Why do naming conventions like prefixing mixin attributes help avoid this?
Multiple Inheritance & MRO Descriptors & Properties
Q15 Beginner Save

Why is Python's approach to multiple inheritance considered more powerful but riskier than single-inheritance-only languages?

Multiple inheritance lets you compose behavior from several independent sources (mixins) without the artificial single-parent constraint, enabling flexible code reuse. The risk is added complexity: diamond problems, MRO ambiguity, attribute name collisions, and fragile cooperative super() chains all require careful discipline to avoid, which is why many style guides recommend using multiple inheritance sparingly and mainly for well-designed, narrow mixins.
# Flexible composition:
class Base: pass
class Serializable: pass
class Comparable: pass

class Model(Base, Serializable, Comparable):  # combines three concerns
    pass
Real-world example
A style guide at a company restricts multiple inheritance to small, single-purpose mixins (like LoggingMixin) and forbids combining multiple 'full' base classes with overlapping responsibilities, to keep MRO chains understandable.
Likely follow-ups
  • What do languages like Java do instead of multiple inheritance?
  • When is composition (has-a) preferable to multiple inheritance (is-a)?
Multiple Inheritance & MRO Metaclasses & Class Customization

Showing 11–15 of 15