Phase 7 of 25 · Topic 7.4

Multiple Inheritance & Method Resolution Order (C3 Linearization / MRO)

1Concept

Python supports multiple inheritance. Attribute and method lookups follow the Method Resolution Order (MRO) calculated using the C3 Linearization algorithm. `super()` delegates calls to the next class in the MRO, not necessarily the parent class.

2Architecture Diagram

Diamond Hierarchy:
      [ Base ]
       /    \
    [ A ]   [ B ]
       \    /
      [ Leaf ] ---> MRO: Leaf -> A -> B -> Base -> object

3Code Example

Python 3.12
class Base:
    def execute(self):
        print("Base execution")

class ServiceA(Base):
    def execute(self):
        print("ServiceA execution")
        super().execute()

class ServiceB(Base):
    def execute(self):
        print("ServiceB execution")
        super().execute()

class CompositeService(ServiceA, ServiceB):
    def execute(self):
        print("CompositeService start")
        super().execute()

service = CompositeService()
service.execute()
print("\nMRO Chain:", [cls.__name__ for cls in CompositeService.__mro__])

4Expected Output

CompositeService start
ServiceA execution
ServiceB execution
Base execution

MRO Chain: ['CompositeService', 'ServiceA', 'ServiceB', 'Base', 'object']

5Key Takeaways

  • Inspect MRO using `Class.__mro__` or `Class.mro()`.
  • `super()` calls the next class in the C3 Linearization sequence, enabling cooperative multiple inheritance.
  • Inconsistent MRO inheritance hierarchies trigger a `TypeError: Cannot create a consistent method resolution order`.