Python › Multiple Inheritance & MRO

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

Intermediate

Answer

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.

Common follow-ups: How does this interact with virtual subclasses registered via ABCs?;What's the performance cost of isinstance checks in a deep hierarchy?