Show a Python method being overridden, and how the child calls the parent version.
Overriding in Python is simple. You define a method with the same name in a child class, and the child version runs whenever the method is called on an object of that class. There is no special keyword for it and no extra setup.
class Parent:
def greet(self):
return 'parent greeting'
class Child(Parent):
def greet(self):
return 'child greeting'
def greet_parent(self):
return super().greet()
child = Child()
print(child.greet()) # child greeting
print(child.greet_parent()) # parent greeting
print(isinstance(child, Parent)) # True
The parent method still exists, and you can reach it with super(). It is better to call super().greet() than to write Parent.greet(self), because super() looks up the next class in the method resolution order of the current object. That keeps working if the class is later combined with a mix-in, while naming the parent class directly hard-codes the parent into the child and breaks as soon as the shape of the hierarchy changes.
The last line is worth noticing. Overriding a method does not change the relationship between the types. A Child is still a Parent, so any code that expects a parent object can accept a child object without knowing anything about the override.