Skip to main content

Practice ยท 3 of 3

Repair a broken diamond

The hierarchy below initializes Base.__init__ TWICE when D is constructed (the effects list gets two entries): A and B bypass cooperation with direct calls, and D adds a belt-and-braces direct call on top of its own super() chain. Fix A, B, and D so that every construction โ€” D(), A(), or B() โ€” appends exactly one 'base' entry, using cooperative super() throughout. Do not change Base. Given broken code: ``python class A(Base): def __init__(self): Base.__init__(self) class B(Base): def __init__(self): Base.__init__(self) class D(B, A): def __init__(self): super().__init__() Base.__init__(self) ``

Difficulty: advanced

Back to lesson: Practice: MRO & Mixin Practice