Polymorphism

Carson West

Python 1 Home

Polymorphism Polymorphism allows objects of different classes to be treated as objects of a common type. This is particularly useful when dealing with inheritance.

class Shape:
    def area(self):
        raise NotImplementedError

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    def area(self):
        return 3.14159 * self.radius * self.radius

class Square(Shape):
    def __init__(self, side):
        self.side = side
    def area(self):
        return self.side * self.side

shapes = [Circle(5), Square(4)]]
for shape in shapes:
    print(shape.area()) # Polymorphic call to area()

Inheritance (This needs its own note)

Classes and Objects (This needs its own note)

Related notes: