Python › Multiple Inheritance & MRO
How can you use isinstance() and issubclass() to check relationships in a multiple-inheritance hierarchy?
IntermediateAnswer
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)) # TrueReal-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.