Show Python inheritance, including overriding a method and calling the parent version with super().
A subclass writes the name of its parent in brackets, and it inherits everything that the parent defines. The example below shows a child class that changes one method and inherits the rest.
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'
dog = Dog('Rex')
print(dog.describe()) # Rex says woof
print(isinstance(dog, Animal)) # True
print(issubclass(Dog, Animal)) # True
Dog inherits __init__ from Animal, so you can build a Dog with a name without writing an initialiser at all. It replaces speak with its own version, and because describe was written in the parent and calls self.speak(), the parent's method uses the child's version. That behaviour has a name, and it is polymorphism. It is also why describe did not need to be replaced, even though it prints the new sound.
When a subclass needs the parent's behaviour as well as its own, super() is the way to reach it. The detail that matters is that super() inside a method refers to the next class in the current method resolution order, and not simply to the literal parent class. That is what makes it safe to use inside a mix-in, and it is why it is different from writing Parent.method(self) by hand.