Polymorphism in Python

In this tutorial, we will understand about Polymorphism in Python, which is one of the concepts of object-oriented programming. We will also learn how to implement Polymorphism in our Python programs with examples.

Not only in Python, but it is also so popular technique of all other programming languages that can save a lot of time in coding.

What is Polymorphism?

Polymorphism is the ability of an entity to take various forms. It refers to the use of an entity to take different forms in various situations. A real-life example is a student, who takes the role of a student when he/she is at college, the same person acts like a child when he/she is at home and also can take the role of a friend when he/she is surrounded by friends.

Advantages of Polymorphism:-

  • It helps to reuse the codes.
  • Easy to debug the codes.
  • Multiple data types can be stored using a single variable.

Polymorphism in Python

Like most of the other popular programming languages, this is also available in Python OOP concept.

Defining methods with the same name in more than one class can be done in Python using Polymorphism. As we know in inheritance a child class inherits the properties of parent class but there are some situations where the parent class doesn’t fit into child class. In such a scenario we have to again code the method in the child class. Polymorphism can be implemented using different methods. You can use a different function, class method, and using inheritance.

Polymorphism with functions and Objects

Look at the code below:

class Crow(): 
     def type(self): 
       print("Bird") 
     def color(self):
       print("Black") 
class Elephant(): 
     def type(self): 
       print("Animal") 
     def color(self): 
       print("Gray") 
      
def func(obj): 
       obj.type() 
       obj.color()
        
obj_crow = Crow() 
obj_elephant = Elephant() 
func(obj_crow) 
func(obj_elephant)

Output:-

Bird
Black
Animal
Gray

Polymorphism with Class Methods

Here is the code:

class Lion:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def info(self):
        print(f"I am a Lion. My name is {self.name}. I am {self.age} years old.")

    def make_sound(self):
        print("Roar")


class Bear:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def info(self):
        print(f"I am a Bear. My name is {self.name}. I am {self.age} years old.")

    def make_sound(self):
        print("Groarrr")


obj_lion = Lion("Simba", 10)
obj_bear = Bear("Fluffy", 7)

for animal in (obj_lion, obj_bear):
    animal.make_sound()
    animal.info()
    animal.make_sound()

Output:-

Roar
I am a Lion. My name is Simba. I am 10 years old.
Roar
Groarrr
I am a Bear. My name is Fluffy. I am 7 years old.
Groarrr

Polymorphism with inheritance

class Country:
     def intro(self):
       print("There are many countries in the world")
 
     def economy(self):
       print("Some are developed nations while others are developing nations")
 
class USA(Country):
     def economy(self):
       print("USA is a developed nation")
 
class India(Country):
     def economy(self):
       print("India is a developing nation")
 
obj_country = Country()
obj_usa = USA()
obj_india = India()
 
obj_country.intro()
obj_country.economy()
 
obj_usa.intro()
obj_usa.economy()
 
obj_india.intro()
obj_india.economy()

Output:-

There are many countries in the world
Some are developed nations while others are developing nations
There are many countries in the world
USA is a developed nation
There are many countries in the world
India is a developing nation

Hope you got a clear understanding of what polymorphism is and how do we implement it in Python. Thank you for reading this tutorial. I hope it helps you.

Also read: Understanding self in Python class

Leave a Reply

Your email address will not be published. Required fields are marked *