Inheritance
Inheritance lets you create a class from another one: class Cat(Animal):. The child class (Cat) automatically gets the attributes and methods of the parent class (Animal).
The child class can override a method to change its behaviour, like speak(). With super(), it can call the parent class's method, for example super().__init__(...) in its constructor.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Cat(Animal):
def speak(self):
return self.name + " says meow"
class Dog(Animal):
def speak(self):
return self.name + " says woof"
for pet in [Cat("Tom"), Dog("Rex")]:
print(pet.speak())