super()

Call the parent method from a subclass. Write the extra behaviour after (or around) that call.

Syntax

class Child(Parent):
def method(self, n):
super().method(n)
  • If you override __init__, call super().__init__() or the parent setup never runs.

Example

class Counter:
    def __init__(self):
        self.n = 0

    def add(self):
        self.n += 1

class Fast(Counter):
    def add(self):
        super().add()
        self.n += 1

c = Fast()
c.add()
print(c.n)

Output:

2

Common mistakes

  • Forgetting self on an override is a TypeError. Forgetting super() skips the parent method entirely.

Practise it