Polymorphism in Python: Types and Examples with Code

Polymorphism in Python: Types and Examples with Code

Polymorphism is the key concept of Object Oriented Programming(OOPs) in Python. Polymorphism makes code more flexible and easy for developers because it makes objects behave similarly even when they have separate implementations. This makes programs easy to read, write, and execute.

Table of Contents:

What is Polymorphism?

Polymorphism is derived from the Greek words ‘poly’ and ‘morphism’. ‘Poly’ means many and ‘morph’ means forms. So, polymorphism means many forms of objects or functions depending upon their usage and working. 

Example:

Python

Here, area() method works for both classes. This is polymorphism.

Understanding Polymorphism in OOPs

The role of polymorphism in OOPs is to ensure that different types of objects can use the same methods or operators. Python provides flexibility of code, which makes it suitable for polymorphism.

Example: Inheritance of parent class methods in child classes by overriding.

Python

Benefits of Polymorphism in OOPs

Following are some of the benefits of Polymorphism:

  • Polymorphism offers simplified code as a single interface can be used to handle multiple data types
  • Functions and Methods of a class can be used for different purposes, which provides code reusability.
  • It offers code flexibility as child classes can use methods of parent classes without changing codes.
  • Multiple teams can work on different parts of code without any conflict.

Polymorphism in Functions

Polymorphism allows functions to take various types of data as input. This eliminates the need to create separate functions for lists, strings, integers, or other data types.

Example:

Python

Here, the len() function is used multiple times for different data types.

Unlock Your Python Potential and explore it for free!
Start mastering Python today with our hands-on course and build real-world projects.
quiz-icon

Polymorphism in Operators

Operators in Python follow polymorphism inherently; their operations are dependent on the data type of the operands. One example of such an operator is the ‘+’ operator. This characteristic of an operator is called operator overloading.

Example: 

Python

What is Method Overloading?

Python allows the creation of multiple methods with the same name depending upon their behavior of accepting the number of arguments and the data types. This is called Method Overloading. It helps increase flexibility, as it allows simplification of code. Python doesn’t allow traditional overloading, but it manages to perform that by using default or variable-length arguments.

Example:

Python

What is Method Overriding?

In Python, a child class or subclass can use a method already defined in the parent class and implement it in its own way. This allows subclasses to retain their original interface and still customize the behavior of those methods.

Example:

Python

Types of Polymorphism in Python

Polymorphism in Python is of two types:

  • Compile-Time Polymorphism
  • Run-Time Polymorphism

Let’s learn more about them with the help of examples.

Compile-Time Polymorphism

When the method or the operator is determined at the compilation time then it is termed as compile-time polymorphism. This can be achieved by operator or method overloading or by setting default arguments.

Example:

Python

Run-Time Polymorphism

When the method behavior is decided at the run-time, then it is termed as Run-Time Polymorphism. This type of polymorphism follows method overriding.

Example:

Python

How to Implement Polymorphism in Python

By using methods with the same names in multiple classes, we can implement polymorphism in Python. The behavior of these methods is dependent on the type of the object at runtime. 

Example:

Python

Dynamic Polymorphism in Python

In Dynamic Polymorphism, the methods in the parent class are overridden by the methods in the subclass at run time.

Example:

Python
Become a Python Pro!
Join our expert-led course and learn Python from scratch to build powerful applications.
quiz-icon

Polymorphism and Abstraction in Python

Abstraction is the process of hiding details that are not required, while polymorphism is the process of sharing the same function in multiple classes depending on their needs. Together, polymorphism and abstraction can simplify the code.

Example:

Python

Real-World Use Cases of Polymorphism in Python

There are various applications of polymorphism in the real world. Some of these application areas are Data Analytics, Machine Learning, Web Development, Payment Systems, etc. Polymorphism helps in writing reusable codes.

Example:

Python

Conclusion

Polymorphism is one of the main concepts in Python programming that contributes significantly to making code flexible and reusable; it is easy for objects representing different classes, in Python to be considered examples of a parent class. Programming languages support function overloading or method overloading, method overriding, and polymorphism that offer dynamic binding. With experience in concepts such as polymorphism, the developer can easily increase the performance of code and solve real problems with a solution that isn’t only cleaner but also more scalable. If you want to explore more about Python you can enroll in our Python Certification Course and get yourself industry-ready.

Get 100% Hike!

Master Most in Demand Skills Now!

FAQs

1. What is Polymorphism in Python?

Polymorphism allows multiple classes to share a common method or operator by changing its arguments.

2. What are the two types of Polymorphism?

In Python, compile-time polymorphism and run-time polymorphism are used.

3. How to achieve polymorphism in Python?

You can achieve polymorphism in Python by operator overloading, method overloading, and inheritance.

4. What is Dynamic Polymorphism?

Dynamic polymorphism uses method overriding to resolve method calls at runtime.

5. Is Operator Overloading Under Polymorphism?

Yes, operator overloading is a part of polymorphism, it comes under compile-time polymorphism.

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 8th Feb 2025
₹20,007
Cohort starts on 15th Feb 2025
₹20,007

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.