Diamond inheritance duplicates ancestors; C3 MRO linearizes so each class appears once
The diamond problem appears when two classes inherit from a common base and a fourth class inherits from both. In languages without a clear rule, the base can be initialized or overridden twice, causing duplicated state or ambiguous method dispatch. Python solves this statically: C3 linearization produces a single order that includes each ancestor exactly once, and preserves the relative order of bases. So in class D(B, C) where B and C both derive from A, the MRO is D, B, C, A, object. With cooperative super() calls, init and other methods run exactly once per class in the chain.
The diamond is not inherently a bug; it becomes a bug when base classes are not written cooperatively.
Cooperative pattern: each init calls super().init(**kwargs) and pops only the arguments it consumes.
Trade-off: cooperative super() keeps the chain consistent but makes signatures harder to read and breaks naive direct calls.
Common mistake: calling BaseClass.init(self) explicitly, which bypasses the MRO and can run a class twice or skip one entirely.
Common mistake: assuming the leftmost base wins for every attribute. The MRO decides, and it may put a right-side class before a shared ancestor.
Version note: the diamond behavior described is C3 linearization from Python 2.3 onward and is uniform across Python 3.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience