PythonMastery

Inheritance in Python

Make a child class that inherits attributes and methods from a parent class, override a method, and call the parent's __init__ with super().

With inheritance, a new class (the child) starts with everything from an existing class (the parent). The child can then add new methods or replace, or override, the ones it inherited.

The program

python
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound"

    def describe(self):
        return f"{self.name} is a {type(self).__name__.lower()}"


class Cat(Animal):
    def speak(self):
        return f"{self.name} says: Meow!"


class Dog(Animal):
    def __init__(self, name, tricks):
        super().__init__(name)
        self.tricks = tricks

    def speak(self):
        return f"{self.name} says: Woof! (knows {len(self.tricks)} tricks)"


class Fish(Animal):
    pass


for animal in [Cat("Whiskers"), Dog("Rex", ["sit", "stay"]), Fish("Nemo")]:
    print(animal.speak(), "|", animal.describe())
output
Whiskers says: Meow! | Whiskers is a cat
Rex says: Woof! (knows 2 tricks) | Rex is a dog
Nemo makes a sound | Nemo is a fish

How it works

Checking the family

isinstance() is True for the object's class and for every parent class above it:

python
class Animal:
    pass

class Dog(Animal):
    pass

rex = Dog()
print(isinstance(rex, Dog))
print(isinstance(rex, Animal))
print(issubclass(Dog, Animal))
print(isinstance(Animal(), Dog))
output
True
True
True
False

Learn the idea: Inheritance & Polymorphism, Object-Oriented Programming