Intermediate LevelPython
How does super() work in single and multiple inheritance hierarchies and why avoid calling Parent.__init__(self)?
❓ Interview Prompt: Explain `super()`, proxy objects, cooperatively delegating calls up the MRO chain, and avoiding hardcoded parent class calls.
'super()' returns a temporary proxy object that delegates method calls to the next class in the calculated MRO chain:
Why avoid 'Parent.__init__(self)'?
Hardcoding explicit parent class calls ('Parent.__init__(self)') breaks multiple inheritance because base classes in a diamond hierarchy get initialized multiple times! 'super()' guarantees every class in the MRO chain is called exactly once.
Code Implementation Example
python
class Base:
def __init__(self):
print('Base init')
class ChildA(Base):
def __init__(self):
super().__init__()
print('ChildA init')
class ChildB(Base):
def __init__(self):
super().__init__()
print('ChildB init')
class GrandChild(ChildA, ChildB):
def __init__(self):
super().__init__() # Delegates down the MRO chain cooperatively!
gc = GrandChild()💡Cooperative initialization using super() across multiple inheritance.
🎯 Interview Key Takeaway
Always use super() instead of explicit Parent.__init__(self) to support cooperative multiple inheritance.
#super()#MRO#Inheritance#Intermediate
Back to all Python Questions→