↓ Skip to main content
Python

Python: Polymorphism

·2 mins
Table of Contents
Animal base class with make_sound, and Dog and Cat subclasses returning Bark and Meow
Recreated in October 2026 from the post; the original image was lost

Polymorphism is a key feature of Object Oriented Programming that allows objects of different classes to be treated as objects of a common class. It is done by implementing the same methods across all classes and making use of inheritance, interfaces and overloading.

The basic idea behind polymorphism is to allow an object to be used in multiple ways. In Python, polymorphism is achieved through the use of methods and inheritance. This means that classes can be created to represent objects of different types and the same method can be called on those objects and it will work differently depending on the object it is called on.

For example, consider a base class ‘Shape’ with a method ‘area’. This method can be implemented differently in subclasses ‘Rectangle’, ‘Triangle’ and ‘Circle’. When we call the method ‘area’ on an object of the subclass, the implementation specific to that subclass will be executed. This is an example of polymorphism, where the same method ‘area’ is behaving differently for different objects.

Another example of polymorphism in Python can be found in the use of operator overloading. In Python, the ‘+’ operator can be overloaded to perform different operations depending on the types of the operands. For instance, when the operands are integers, the ‘+’ operator performs arithmetic addition. However, when the operands are strings, the ‘+’ operator concatenates the strings.

In conclusion, polymorphism is a powerful feature in Python that allows us to write flexible and reusable code. By implementing the same methods across different classes and using inheritance, interfaces and overloading, we can write code that can be used in different ways for different objects. This makes our code more maintainable and easier to understand.

Example
#

In this example, the make_sound method is defined in both the Dog and Cat classes. The make_animals_sound function takes a list of Animal objects and prints the sound they make using the make_sound method. The Dog and Cat objects can be treated as Animal objects because they inherit from the Animal class. This allows the make_animals_sound function to handle objects of different classes in a polymorphic way.

class Animal:
    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        return "Bark"

class Cat(Animal):
    def make_sound(self):
        return "Meow"

def make_animals_sound(animals):
    for animal in animals:
        print(animal.make_sound())

animals = [Dog(), Cat()]
make_animals_sound(animals)

# Output:
# Bark
# Meow
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Python: `argparse`

·2 mins
Recreated in October 2026 from the post; the original image was lost The argparse module is a powerful tool for parsing command line arguments in Python. It provides an easy way to define the arguments required by your script and handle the input data passed to your script by the user.

Python: Decorators

·1 min
Let’s learn about decorators @iamdecorator Decorators are used to modify or extend the behavior of functions and classes. They are useful for wrapping a function or class with additional functionality, such as logging, access control, performance measurement, or adding attributes. This can reduce code duplication, increase readability and maintainability.

Python vs Bash

·1 min
Recreated in October 2026 from the post; the original image was lost Python and bash are both programming languages, but they have different purposes and use cases.