Python Inheritance
Python inheritance lets a new class (the child or subclass) reuse the attributes and methods of an existing class (the parent or base class). The child can add new features and change existing ones, so you avoid copying code between similar classes.
Create a Child Class
Put the parent class name in brackets after the child class name. The child gets every method of the parent automatically.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
pass
rex = Dog("Rex")
print(rex.speak())Rex makes a sound
Method Overriding
If the child defines a method with the same name, it replaces the parent version for objects of the child class.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Cat(Animal):
def speak(self):
return f"{self.name} says meow"
print(Animal("Generic").speak())
print(Cat("Luna").speak())Generic makes a sound Luna says meow
Using super()
super() calls a method from the parent class. The most common case is extending __init__: let the parent set up its attributes, then add the new ones.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def describe(self):
return f"{self.name} earns {self.salary}"
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def describe(self):
return super().describe() + f" and leads {self.team_size} people"
m = Manager("Ayesha", 9000, 6)
print(m.describe())Ayesha earns 9000 and leads 6 people
Polymorphism
Different classes can offer methods with the same name. Code that calls that method works with any of them, without checking their type.
class Shape:
def area(self):
return 0
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
class Triangle(Shape):
def __init__(self, base, height):
self.base, self.height = base, height
def area(self):
return 0.5 * self.base * self.height
for shape in [Square(3), Triangle(4, 5)]:
print(type(shape).__name__, shape.area())Square 9 Triangle 10.0
isinstance() and issubclass()
An object of a child class also counts as an instance of the parent class.
class Vehicle: pass
class Car(Vehicle): pass
my_car = Car()
print(isinstance(my_car, Car))
print(isinstance(my_car, Vehicle))
print(issubclass(Vehicle, Car))True True False
Multiple Inheritance
A class can inherit from more than one parent, written as class C(A, B). Python searches parents from left to right (the method resolution order). It is powerful but can get confusing, so it is mostly used for small "mixin" classes that add one feature.
Frequently asked questions
What does super() do in Python?
super() returns a proxy to the parent class so you can call its methods, most often super().__init__(...) to reuse the parent's setup code.
Does Python support multiple inheritance?
Yes. A class can list several parent classes. Python uses the method resolution order (MRO) to decide which parent method runs first; you can inspect it with ClassName.__mro__.
What is method overriding in Python?
It is when a child class defines a method with the same name as one in its parent, replacing the parent's behaviour for child objects.