Concept
Calling Parent Methods with `super()`
`super()` delegates method calls up to the parent class. Use `super().__init__(...)` to initialize parent state without repeating boilerplate code.
python
class Player:
def __init__(self, name):
self.name = name
class Mage(Player):
def __init__(self, name, mana):
super().__init__(name)
self.mana = mana
m = Mage("Gandalf", 100)
print(m.name, m.mana)
# 2. Calling Parent Methods with `super()` `super()` delegates method calls up to the parent class. Use `super().__init__(...)` to initialize parent state without repeating boilerplate code: ```python class Player: def __init__(self, name): self.name = name class Mage(Player): def __init__(self, name, mana): super().__init__(name) self.mana = mana m = Mage("Gandalf", 100) print(m.name, m.mana) ``` Never do `Player.__init__(self, name)` directly. Hardcoding parent class names breaks cooperative multiple inheritance and prevents modern dependency injection.
Checkpoint
Why do developers call `super().__init__(...)` in a child class constructor?
Practice
Create a base class `Enemy` with `__init__(self, hp)`. Create subclass `Boss(Enemy)` that takes `(self, hp, armor)` and calls `super().__init__(hp)`. Instantiate `Boss(1000, 50)` and print its `hp`.
01
Tap code blocks below to assemble
Available Blocks