Inheritance
A child class gets everything from its parent: class Child(Parent). It can add new methods and override old ones. super() calls the parent version, which is commonly used in __init__.
Example
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return super().speak() + ": woof"
d = Dog("Rex", "Labrador")
print(d.speak())
print(d.breed)
print(isinstance(d, Animal))Result
Rex makes a sound: woof Labrador True
Exercise
Call the parent version.
().speak()Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.