Polymorphism
Polymorphism lets objects of different classes respond to the same method, each in its own way. A circle and a square both have an area() method, but the calculation is different.
Thanks to this, you can write a single piece of code that works for all these objects, for example a loop that calls s.area() without caring whether s is a circle or a square.
class Circle:
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r ** 2
class Square:
def __init__(self, side):
self.side = side
def area(self):
return self.side ** 2
for s in [Circle(1), Square(3)]:
print(type(s).__name__, s.area())