Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Polymorphism in Python

Polymorphism means having vivid or different forms. In the programming world, Polymorphism refers to the ability of the function with the same name to carry different functionality altogether. It creates a structure that can use many forms of objects.

This permits functions/arguments to use entities of different types at different times.

In object-oriented programming, Polymorphism allows a particular object referring to a particular class to be used in a similar fashion as if it was a different object referring to altogether a different class.

Polymorphism In Python
Polymorphism In Python

Implementing Polymorphism in Python with Class

Python can use different types of classes, in the same way, using Polymorphism. To serve this purpose, one can create a loop that iterates through a tuple of objects. Post which, one can call the methods without having a look at the type of class to which the object belongs to.

Example: Polymorphism with Classes and Objects

class Rabbit(): 
    def age(self): 
        print("This function determines the age of Rabbit.") 
  
    def color(self): 
        print("This function determines the color of Rabbit.") 
  
class Horse(): 
    def age(self): 
        print("This function determines the age of Horse.") 
  
    def color(self): 
        print("This function determines the color of Horse.") 
  
obj1 = Rabbit() 
obj2 = Horse() 
for type in (obj1, obj2): # creating a loop to iterate through the obj1, obj2
    type.age() 
    type.color() 
    

Output:

This function determines the age of Rabbit.
This function determines the color of Rabbit.
This function determines the age of Horse.
This function determines the color of Horse.


Implementing Polymorphism in Python with Inheritance

We will be defining functions in the derived class that has the same name as the functions in the base class. Here, we re-implement the functions in the derived class. The phenomenon of re-implementing a function in the derived class is known as Method Overriding.

Example: Polymorphism with Inheritance

class Animal: 
  def type(self): 
    print("Various types of animals") 
      
  def age(self): 
    print("Age of the animal.") 
    
class Rabbit(Animal): 
  def age(self): 
    print("Age of rabbit.") 
      
class Horse(Animal): 
  def age(self): 
    print("Age of horse.") 
      
obj_animal = Animal() 
obj_rabbit = Rabbit() 
obj_horse = Horse() 
  
obj_animal.type() 
obj_animal.age() 
  
obj_rabbit.type() 
obj_rabbit.age() 
  
obj_horse.type() 
obj_horse.age() 

Output:

Various types of animals
Age of the animal.
Various types of animals
Age of rabbit.
Various types of animals
Age of horse.

Recommended Readings:


Compile-Time Polymorphism or Method Overloading?

Unlike many other popular object-oriented programming languages such as Java, Python doesn’t support compile-time polymorphism or method overloading. If a class or Python script has multiple methods with the same name, the method defined in the last will override the earlier one.

Python doesn’t use function arguments for method signature, that’s why method overloading is not supported in Python.


Operator Overloading in Python

Python supports operator overloading. This is another type of polymorphism where an operator behaves differently based on the type of the operands.

  • + operator adds two numbers and concatenate two strings
  • * operator multiplies two numbers and when used with a string and int, repeats the string given int times and concatenate them.

Read More at Operator Overloading in Python.


Advantages of Polymorphism

  • The codes and classes written once can be reused and implemented multiple times.
  • It helps in reducing the coupling between different functionalities and behavior of objects.

References