A key idea in object-oriented programming (OOP) is polymorphism, which enables methods to perform different actions depending on the object they are acting upon.
Python’s polymorphism is a potent feature that improves the reuse and flexibility of code. This post will go over the fundamentals of polymorphism in Python, how it functions, and some examples of how to use it.
What is Polymorphism?
The definition of polymorphism is having multiple forms. Programming polymorphism refers to using the same function name (but distinct signatures) for various types. The data types and quantity of arguments used in each function are the primary differences.
Example of inbuilt polymorphic functions
# Python program to demonstrate in-built poly-
# morphic functions
# len() being used for a string
print(len(“geeks”))
# len() being used for a list
print(len([10, 20, 30]))
Class Polymorphism in Python
The code below demonstrates that Python can use two different class types similarly. We construct a for loop that goes through an object tuple iteratively. Then, without worrying about what class type each object belongs to, call the methods. We take it for granted that each class has these methods.
class India():
def capital(self):
print(“New Delhi is the capital of India.”)
def language(self):
print(“Hindi is the most widely spoken language of India.”)
def type(self):
print(“India is a developing country.”)
class USA():
def capital(self):
print(“Washington, D.C. is the capital of USA.”)
def language(self):
print(“English is the primary language of USA.”)
def type(self):
print(“USA is a developed country.”)
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country.type()
Polymorphism with Inheritance
Python polymorphism allows us to define methods with the same names in the parent and child classes. The methods of the parent class are passed down to the child class through inheritance.
A method that a child class has inherited from the parent class, however, can be changed. When the method that the child class inherits from the parent class doesn’t quite fit, this is especially helpful. We re-implement the method in the child class in such circumstances. Method overriding is the process of re-implementing a method in the child class.
class Bird:
def intro(self):
print(“There are many types of birds.”)
def flight(self):
print(“Most of the birds can fly but some cannot.”)
class sparrow(Bird):
def flight(self):
print(“Sparrows can fly.”)
class ostrich(Bird):
def flight(self):
print(“Ostriches cannot fly.”)
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Polymorphism with a Function and Objects
Polymorphism can also be achieved by creating a function that accepts any object. Let’s create a function called “func()” in this example. It will accept an object that we will call “obj.” This function can be called by any instantiated object, even though we are calling it “obj.”
Let’s now give the function an assignment that makes use of the “obj” object that we gave it. Let’s refer to the three methods—capital(), language(), and type()—that are defined in the two classes as “India” and “USA” in this instance. If we don’t already have instantiations of the “India” and “USA” classes, let’s make them now. With those, we can use the same func() function to call their action:
def func(obj):
obj.capital()
obj.language()
obj.type()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
Conclusion
Polymorphism in Python allows methods to execute different actions depending on the type of object, which increases code reuse and flexibility. With the help of inheritance, function objects, class methods, and built-in functions, Python programmers can write more flexible and manageable code. To write object-oriented code effectively, one must comprehend these ideas.