Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Object-oriented PythonSeniorOccasionalTroubleshooting

How does Python resolve the classic diamond inheritance problem, and what does "cooperative multiple inheritance" mean in practice?

Answer

In a diamond where `B` and `C` both inherit from `A`, and `D` inherits from both `B` and `C`, Python's C3-linearized MRO guarantees each ancestor appears exactly once, in a consistent order, so there is no ambiguity about which `A` is used. Cooperative multiple inheritance means every class in the hierarchy calls `super().__init__(**kwargs)` (rather than calling a specific parent class by name) so that each `__init__` in the chain runs exactly once, in MRO order, passing along whatever keyword arguments the next class in line still needs.

Strong answer includes

  • C3-linearized MRO guarantees each ancestor appears once, consistently
  • cooperative pattern uses super() instead of naming a specific parent
  • every __init__ runs once in MRO order via chained super() calls

Practical example

If `B.__init__` and `C.__init__` both call `A.__init__` directly instead of

Python example
super().__init__()

, `A.__init__` runs twice in a

Python example
D()

instance; using

Python example
super().__init__()

everywhere makes it run exactly once.

#inheritance#mro#super

Sources

Data modelPython Software Foundation · Dunder methods, object identity, attribute lookup and the descriptor and context manager protocolsThe Python Language ReferencePython Software Foundation · Precise scoping, evaluation order and expression semantics