Python is a high-level, interpreted, and open-sourced programming language. It has gained a lot of focus among many programmers and developers because it supported libraries that help in many tasks such as exploratory data analysis, GUI programming, etc. Also, programming in python is very interesting.
We are going to discuss an interesting feature of python in this article. So let’s get started!
What is Polymorphism?
The word polymorphism is derived from the Greek word poly (meaning many), and morphism (forms). It means a single function name or method name can have multiple forms. And this fulfils the wish of avoiding code duplication in the implementation.
But polymorphism in python is a little different from polymorphism in other programming languages. Programming languages like java and c++ support compile-time polymorphism (method overloading). In method overloading, multiple methods can have the same method name but differ in their parameter signature. This feature is not supported by python. If multiple methods have the same function, then the newest function implementation will override the earlier function implementation.
Function polymorphism in python can be categorized into two user-defined polymorphism and pre-defined polymorphism.
We can implement user-defined polymorphism to call the same function with a different parameter signature, and this can be considered as a little compensation to the unavailability of method overloading in python. Let’s walk through an example code
In the above code, even though the number of parameters passed is not equal both the print statements refer to the same method. In the second function call, parameter c is assigned with a default value of 1.
Similarly few predefined methods in python exhibit polymorphism features, where a single method can accept parameters of different datatypes. Methods like len() exhibits this feature. And here’s the code to illustrate that.
In the above code, the same len() method is used for a list, tuple, string, and dictionary.
Checkout: Python Applications in Real World
An operator like ‘+’ can be used in multiple ways. For example, it can be used to add two numbers, strings, as well as lists, etc. And here’s the code to demonstrate that.
Method overriding is also considered as runtime polymorphism, and it is supported by many languages like java, c++, and python.
This feature is related to inheritance, a child class in python inherits member functions and member variables from its parent class. And if we feel that the implementation of the parent class method is not relevant, then we can override that method in the child class. And modifying the member functions as per the requirement in the child class is referred to as method overriding.
print(“two wheeler needs fuel to run”)
print(“bikes are suitable for a maximum of 2 people”)
print(“electric bikes run on a battery”)
print(“petrol bike runs on petrol”)
In the above code, two_wheeler is the parent class and electric_bike, petrol_bike are the child classes. And the methods fuel(), capacity() are inherited by the child classes electric_bike and petrol_bike. Now, we can update the implementation of the methods if required, and inside the electric_bike class we’ve reimplemented the fuel() method, similarly reimplemented the fuel() method in the petrol_bike class.
For example in the code ebike.fuel() prints “electric bikes run on a battery” and pbike.fuel() prints petrol bike runs on petrol”.
Polymorphism in Class Methods
Python allows different classes to have the same method name, and this comes under the polymorphism feature. And the invocation of the method is based on the object we use to call the method.
print(“bicycle doesn’t need any fuel!”)
print(“bicycles are suitable for single person ride”)
print(“electric bikes run on battery”)
print(“electric bikes are suitable for maximum of 2 people”)
print(“petrol bike runs on petrol”)
print(“petrol bikes are suitable for maximum of 2 people”)
for obj in l:
In the above code, we’ve created three objects of the three classes bicycle, electric_bike, and petrol_bike. And all three classes have the same method names. Now, it’s the task of the compiler to decide which method has to be invoked based on the type of object used to invoke the method.
For example, ecobike.fuel() will invoke the fuel() method of the bicycle class and ebike.fuel() will invoke the fuel() method of the electric_bike class. We append all these objects to a list and in every iteration, we are going to call the same function names but the type of object which invokes the method is going to change. In the first iteration methods of the bicycle class is called, and methods of electric_bike, petrol_bike in further iterations.
In this article, we’ve understood what polymorphism means, discussed how python is different in the case of method overloading. Walked through demonstrations of various possibilities of polymorphism in pythons such as operator overloading, function polymorphism, method overriding, and polymorphism in class methods.
Now that you are aware of polymorphism in python, implement your next python code using all these features!
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.