📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Inheritance

Inheritance

6 min read
Inherit from a parent class: class Child(Parent). Call parent methods with super(). Override methods in the child class to change behaviour. isinstance() checks if an object belongs to a class or its parent.

Inheritance

class Vehicle:
    def __init__(self, make, speed):
        self.make  = make
        self.speed = speed
    def describe(self):
        return f"{self.make} going {self.speed}km/h"

class Car(Vehicle):
    def __init__(self, make, speed, doors):
        super().__init__(make, speed)
        self.doors = doors
    def describe(self):
        return super().describe() + f" ({self.doors} doors)"

car = Car("Toyota", 120, 4)
print(car.describe())