Overriding methods and super()
🌍 In real life. Every phone can make a sound when a call comes in. A school phone just rings; a smartphone plays your favourite song instead. The smartphone still is a phone, but it does that one thing its own way.
In Python, a child class can override a method: it writes a method with the same name as the parent's, and its own version wins. That's the lookup tower again: the child's floor is searched before the parent's.
The idea in one sentence: a child overrides a parent's method by writing one with the same name, and super().method() still lets it run the parent's version too.
Its words:
- override: replace a parent's method in the child
super(): "my parent class", used to call the parent's version- extend: run the parent's version and then add to it, like
super().__init__(name)followed by the child's own setup
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
def describe(self):
return self.name + " says " + self.speak()
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
print(Dog("Rex").describe())
print(Cat("Tom").describe())
print(Animal("Blob").describe())Sometimes the child wants to add to the parent's work instead of replacing it. super() means "my parent class":
super().__init__(name)runs the parent's__init__first- then the child sets up its own extras
Think of a new student joining a sports club: they get the normal school record (the parent's part) plus a club badge (the child's extra).
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hi, I'm " + self.name
class Student(Person):
def __init__(self, name, school):
super().__init__(name)
self.school = school
def greet(self):
return super().greet() + " from " + self.school
s = Student("Ana", "Green School")
print(s.name, s.school)
print(s.greet())Why super() instead of copying? If the child copied the parent's setup code, a later change to the parent would have to be made twice, and one copy would be forgotten. super().__init__(...) always runs the parent's current version.
Mistakes and edge cases.
- Forgetting super().init. If the child writes its own
__init__and never calls the parent's, the parent's attributes are never set:AttributeError: 'B' object has no attribute 'n'. - A different name by accident.
def speek(self):doesn't overridespeak; the parent's version still runs. - Calling super without brackets. It's
super().speak(), with brackets aftersuper.
Quiz
What does this print?
class Bird: def move(self): return "flies" class Penguin(Bird): def move(self): return "swims" print(Penguin().move(), Bird().move())What does this print?
class Shape: def name(self): return "shape" class Square(Shape): def name(self): return "square, a kind of " + super().name() print(Square().name())What does
super().__init__(name)do inside a child's__init__?- Runs the parent's __init__, so the parent's attributes get set
- Makes a new parent object
- Deletes the child's __init__
What does this print?
class Animal: def __init__(self, name): self.name = name class Dog(Animal): def __init__(self, name, trick): super().__init__(name) self.trick = trick d = Dog("Rex", "roll over") print(d.name + " can " + d.trick)Which line has a mistake?
class Animal: def __init__(self, name): self.name = name class Dog(Animal): def __init__(self, name, trick): self.trick = trick d = Dog("Rex", "sit") print(d.name)
Coding tasks
Animalis written for you. WriteCat, a kind of Animal, whosespeak()returns"Meow"instead.Rectangleis written for you. WriteSquare(side), a kind of Rectangle, whose__init__usessuper().__init__so that width and height are bothside.Dogis written for you. WritePuppy, a kind of Dog, whosespeak()gives the Dog's sound with an extra"!"on the end, usingsuper().