Polymorphism in Python

This article provides a tutorial on polymorphism in Python, a fundamental concept in object-oriented programming.

Updated March 9, 2023

Hello future Python wizard, welcome to Python Help!

Today, we’re going to talk about polymorphism, a key concept in object-oriented programming that allows objects of different classes to be treated as if they were of the same class.

Polymorphism is achieved through the use of inheritance and the concept of method overriding, where a subclass provides its own implementation of a method defined in its parent class. This allows us to create more flexible and modular code, where objects of different classes can be used interchangeably.

Let’s start with an example. Suppose we have a class called Shape that has a method called draw(). We want to create two subclasses of Shape: Circle and Rectangle, both of which implement their own version of draw().

Here’s what the implementation could look like:

class Shape:
    def draw(self):
        pass

class Circle(Shape):
    def draw(self):
        return "Drawing a circle"
    
class Rectangle(Shape):
    def draw(self):
        return "Drawing a rectangle"

Notice how we’ve defined the Circle and Rectangle classes with the Shape class as their parent. Both subclasses override the draw() method to provide their own implementation.

Now, let’s test it out:

shapes = [Circle(), Rectangle()]

for shape in shapes:
    print(shape.draw())

This will output:

Drawing a circle
Drawing a rectangle

Notice how we’re able to iterate over a list of Shape objects, even though they’re actually instances of Circle and Rectangle. This is polymorphism in action!

Let’s take a look at another example. Suppose we have a class called Animal that has a method called speak(). We want to create two subclasses of Animal: Cat and Dog, both of which implement their own version of speak().

Here’s what the implementation could look like:

class Animal:
    def speak(self):
        pass

class Cat(Animal):
    def speak(self):
        return "Meow"
    
class Dog(Animal):
    def speak(self):
        return "Woof"

Notice how we’ve defined the Cat and Dog classes with the Animal class as their parent. Both subclasses override the speak() method to provide their own implementation.

Now, let’s test it out:

animals = [Cat(), Dog()]

for animal in animals:
    print(animal.speak())

This will output:

Meow
Woof

Again, notice how we’re able to treat objects of different classes (Cat and Dog) as if they were of the same class (Animal), thanks to polymorphism.

In summary, polymorphism is a powerful concept in object-oriented programming that allows objects of different classes to be treated as if they were of the same class. This is achieved through the use of inheritance and method overriding, where a subclass provides its own implementation of a method defined in its parent class. In Python, polymorphism allows us to create more flexible and modular code, where objects of different classes can be used interchangeably.