• Articles
  • Tutorials
  • Interview Questions

Polymorphism in Python: Types and Examples with Code

Polymorphism in Python: Types and Examples with Code

Polymorphism in Python is a core concept that allows objects to demonstrate diverse behaviors while maintaining code simplicity. By offering a shared interface for different classes, it permits functions and operators to adapt their actions based on input types. This enables code that is easier to read and maintain, promotes efficient software development, and encourages a more versatile approach to problem-solving.

Check out this YouTube video to learn about Python:

What is Polymorphism in Python?

What is Polymorphism in Python?

Polymorphism in Python is a fundamental principle of object-oriented programming, enabling objects to adopt various forms and enhancing code flexibility and reusability. 

Through polymorphism, distinct object classes can be accessed through a unified interface, facilitating functions or operators to behave differently based on their input. It provides a streamlined, generalized approach to coding, as methods can operate on objects from different classes, simplifying implementation and modification in software development.

How to Use Polymorphism in Python?

In Python, polymorphism is implemented through various mechanisms, including duck typing, operator overloading, and method overriding. Below are the following that will elaborate on the concepts of using polymorphism in Python:

  • Duck Typing: Duck typing is a concept in Python that focuses on the behavior of objects rather than their type or class. It allows you to work with objects based on their capabilities or methods, rather than explicitly checking their types. If an object behaves like a duck, it’s treated as a duck.

Here is an example of duck typing in Python:

Code:

class Dog:    
def speak(self):
        return "Woof!"
class Cat:
    def speak(self):
        return "Meow!"
def animal_sound(animal):
    return animal.speak()
# Usage of duck typing
dog = Dog()
cat = Cat()
print(animal_sound(dog))  
print(animal_sound(cat))  

Output: 

Woof!
Meow!

In this example, the animal_sound function accepts objects of different classes (Dog and Cat) as long as they have a speaking method.

  • Operator Overloading: Operator overloading allows you to define how operators like +, -, *, etc., behave when applied to objects of your custom classes. This is achieved by defining special methods in your class, such as __add__, __sub__, __mul__, etc.

Here is a simple example of operator overloading in Python:

Code:

class ComplexNumber:
    def __init__(self, real, image):
        self.real = real
        self.imag = imag
    def __add__(self, other):
        return ComplexNumber(self.real + other.real, self.imag + other.imag)
    def __str__(self):
        return f"{self.real} + {self.imag}i"
# Usage of operator overloading
num1 = ComplexNumber(2, 3)
num2 = ComplexNumber(1, 4)
result = num1 + num2
print(result)  

Output:

3 + 7i

Here we have defined the __add__ method to specify how the + operator should work with instances of the ComplexNumber class.

  • Method Overriding: Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is a form of polymorphism where a subclass can replace or extend the behavior of its superclass’s method.

Below is an example of method overriding in Python:

Code

class Animal:
    def speak(self):
        pass
class Dog(Animal):
    def speak(self):
        return "Woof!"
class Cat(Animal):
    def speak(self):
        return "Meow!"
# Usage of method overriding
dog = Dog()
cat = Cat()
print(dog.speak())
print(cat.speak())  

Output:

Woof!
Meow!

In this example, both the Dog and Cat classes override the speak method defined in the Animal superclass to provide their own implementations.

Examples of Polymorphism in Python

Python’s polymorphism comes in various forms, such as class structures, flexible functions, and differences inherited from parent classes, making your code more versatile and practical.

To learn this latest programming language, sign up for Intellipaat’s trending Python Course and become proficient in it!

Get 100% Hike!

Master Most in Demand Skills Now !

Examples of Polymorphism in Python

In the following, we have highlighted the examples of polymorphism in Python along with the code:

Polymorphism with Classes

In Python, polymorphism can be exemplified through class definitions. Different classes can implement the same method name, but each class provides its own unique behavior. This way, you can create a common interface, and various classes can implement it differently.

Example of Code:

class Shape:
    def calculate_area(self):
        pass
class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    def calculate_area(self):
        return 3.14 * self.radius ** 2
class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    def calculate_area(self):
        return self.length * self.width
# Usage of polymorphism with classes
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
    print(f"Area: {shape.calculate_area()}")

Output:

Area: 78.5
Area: 24

Polymorphism with Functions

Polymorphism with functions, also known as duck typing, allows you to work with objects based on their behavior rather than their specific types.

Example of Code:

class Circle:
    def area(self, radius):
        return 3.14159265359 * radius ** 2
class Square:
    def area(self, side_length):
        return side_length ** 2
def calculate_area(shape, size):
    return shape.area(size)
# Usage of polymorphism with functions
circle = Circle()
square = Square()
print(calculate_area(circle, 5))  # Output: 78.539816339745
print(calculate_area(square, 4))

Output:

78.5
16

Want to know about the real-world uses of Python? Read our detailed blog on Python Applications now.

Polymorphism with Inheritance

Inheritance allows you to achieve polymorphism by creating a superclass with a method and then overriding that method in its subclasses. This way, you can call the same method name on objects of different classes and obtain different results. 

Example of Code:

class Vehicle:
    def move(self):
        return "Vehicle is moving"
class Car(Vehicle):
    def move(self):
        return "Car is moving on the road"
class Boat(Vehicle):
    def move(self):
        return "Boat is sailing in the water"
# Usage of polymorphism with inheritance
vehicles = [Vehicle(), Car(), Boat()]
for vehicle in vehicles:
    print(vehicle.move())

Output:

Vehicle is moving
Car is moving on the road
Boat is sailing in the water

Prepare yourself for the industry by going through Python Interview Questions and Answers now!

Benefits of Using Polymorphism in Python

Benefits of Using Polymorphism in Python

Using polymorphism in Python offers several benefits that contribute to code development and maintenance.

Below, we have pointed out some of the key advantages of using polymorphism:

  • Increased Code Reuse-
    • Polymorphism allows you to write code that can be reused with different types of objects, provided they adhere to a common interface or behavior.
    • This reduces the need for duplicate code and promotes a more efficient development process.
    • You can write generic functions or methods that work with various object types, enhancing code reusability.
  • More Flexible Code-
    • Polymorphism makes your code more adaptable to changes and future extensions.
    • When you introduce new object types, you can create new subclasses or implementations without altering existing code.
    • This modular approach enables you to add functionality incrementally without affecting the stability of the existing codebase.
  • Improved Readability-
    • Polymorphism promotes abstraction, allowing you to focus on high-level concepts rather than specific object types.
    • Code that utilizes polymorphism tends to be more concise and easier to understand because it deals with common behaviors rather than implementation details.
    • It enhances code readability by encapsulating complex behaviors within well-defined interfaces or classes.
  • Simplified Maintenance-
    • With polymorphism, maintenance becomes more straightforward because you deal with common interfaces or base classes.
    • Changes and bug fixes can be applied at the abstract level, affecting all subclasses uniformly and reducing the chances of introducing inconsistencies.
    • This simplifies the debugging and maintenance process.
  • Enhanced Testing-
    • Polymorphism can simplify the testing process. You can create generic test cases that apply to multiple objects that share a common interface.
    • This reduces the need for writing separate test cases for each object type, improving testing efficiency.

Conclusion

Polymorphism is a potent concept within Python that offers a crucial dimension of flexibility and reusability in software development. It empowers developers to create code that is highly adaptable, reusable, and comprehensible. We have explored the inner workings of polymorphism, delving into its application through classes, functions, and inheritance, demonstrating how diverse objects can seamlessly integrate into a unified framework.

Furthermore, the advantages of polymorphism, such as the ability to use code across multiple contexts, enhance adaptability, and facilitate code management, underscore its pivotal role within Python and the broader realm of object-oriented programming.

If you have any doubts, you can reach out to us on our Python Community!

Course Schedule

Name Date Details
Python Course 01 Jun 2024(Sat-Sun) Weekend Batch
View Details
Python Course 08 Jun 2024(Sat-Sun) Weekend Batch
View Details
Python Course 15 Jun 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.