Paul's Python CoursePPC

super()

Not met yet

super() reaches the parent class's methods for this same object. A subclass that writes its own __init__ replaces the parent's, so call super().__init__(...) to let the parent set up its part. In an overriding method, super().give_back(hours) reuses the parent's work so the subclass only adds what differs.

Try it

main.py
class Bike:
    def __init__(self, number):
        self.number = number
        self.rider = None

class ElectricBike(Bike):
    def __init__(self, number, charge):
        super().__init__(number)
        self.charge = charge

volt = ElectricBike(9, 80)
print(volt.number, volt.rider, volt.charge)

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks the parent's __init__ still runs when a subclass writes its own __init__.
  • Thinks super().method() calls the subclass's own version of the method again.

Where it comes up