Python Intermediate: How to use Polymorphism in Python

Home /

Table of Contents

Python Polymorphism

Polymorphism is defined as the presence of multiple forms. Polymorphism in programming refers to the use of the same function name (but different signatures) for different types.

In-build polymorphism

Example:

print(len("python"))
 
print(len([10, 20, 30]))

Output:

6
3

User-Defined Polymorphic Functions

Example:

def multi(p, q, r = 1):
   return p * q * r
 
print(multi(2, 3))
print(multi(2, 3, 4))

Output:

6
24

Class Method Polymorphism

The code below demonstrates how Python can use two different class types in the same way. We construct a for loop that iterates over a tuple of objects. Then call the methods without regard for the class type of each object. We presume that these methods exist in each class.

Example:

class Technologies_1():
   print("")
   def language(self):
       print("---->","Python", "Java", "Javascript")
 
   def framework(self):
       print("---->","Django", "Spring Boot", "React JS")
 
   def usage(self):
       print("---->","Fullstack", "Fullstack", "Frontend")
       print("--------------------")
 
class Technologies_2():
   def language(self):
       print("---->","Go", "Rust", "C#")
 
   def framework(self):
       print("---->","Gin", "Actix", ".Net")
 
   def usage(self):
       print("---->","Backend","Backend","Fullstack")
       print("--------------------")
      
 
tech_one = Technologies_1()
tech_two = Technologies_2()
for stack in (tech_one, tech_two):
   stack.language()
   stack.framework()
   stack.usage()

Output:

----> Python Java Javascript
----> Django Spring Boot React JS
----> Fullstack Fullstack Frontend
--------------------
----> Go Rust C#
----> Gin Actix .Net
----> Backend Backend Fullstack
--------------------

Polymorphism with Inheritance

In Python, polymorphism with Inheritance allows us to define methods in the child class that have the same name as methods in the parent class. In inheritance, the child class inherits the parent class’s methods. However, a method in a child class that has been inherited from the parent class can be modified. This is especially useful when the method inherited from the parent class does not exactly fit the child class. In such cases, the method is re-implemented in the child class. Method Overriding refers to the process of re-implementing a method in a child class.

Example:

class Python:
   def intro(self):
       print("There are many types of framework in python.")
 
   def usage(self):
       print("Most of the framework is fullstack type.")
 
class Django(Python):
   def usage(self):
       print("---->","Django is a fullstack framework")
 
class FastAPI(Python):
   def usage(self):
       print("---->","FastAPI is not a fullstack framework.")
 
class Flask(Python):
   def usage(self):
       print("---->","Flask is a fullstack framework.")
 
class Pyramid(Python):
   def usage(self):
       print("---->","Pyramid is a fullstack framework.")
 
lang = Python()
frame1 = Django()
frame2 = FastAPI()
frame3 = Flask()
frame4 = Pyramid()
 
lang.intro()
lang.usage()
 
frame1.intro()
frame1.usage()
 
frame2.intro()
frame2.usage()
 
frame3.intro()
frame3.usage()
 
frame4.intro()
frame4.usage()

Output:

There are many types of framework in python.
Most of the framework is fullstack type.
There are many types of framework in python.
----> Django is a fullstack framework
There are many types of framework in python.
----> FastAPI is not a fullstack framework.
There are many types of framework in python.
----> Flask is a fullstack framework.
There are many types of framework in python.
----> Pyramid is a fullstack framework.

Function and Object Polymorphism

It is also possible to write a function that accepts any object, enabling polymorphism. In this example, let’s write a function called “func()” that takes an object called “stack.” Though we use the name ‘stack,’ any instantiated object can be called into this function. Next, give the function something to do that makes use of the ‘stack’ object we passed it. In this case, let’s call the three methods language(), framework(), and usage(), which are defined in the two classes ‘Technologies_1’ and ‘Technologies_2,’ respectively. Next, if we don’t already have them, let’s make instantiations of the ‘Technologies_1’ and ‘Technologies_2’ classes. We can call their actions with the same func() function.

Example:

class Technologies_1():
  print("")
  def language(self):
      print("---->","Python", "Java", "Javascript")
  def framework(self):
      print("---->","Django", "Spring Boot", "React JS")
  def usage(self):
      print("---->","Fullstack", "Fullstack", "Frontend")
      print("--------------------")

class Technologies_2():
  def language(self):
      print("---->","Go", "Rust", "C#")
  def framework(self):
      print("---->","Gin", "Actix", ".Net")
  def usage(self):
      print("---->","Backend","Backend","Fullstack")
      print("--------------------")
    
 
def func(stack):
  stack.language()
  stack.framework()
  stack.usage()
 
tech_one = Technologies_1()
tech_two = Technologies_2()
func(tech_one)
func(tech_two)

Output:

----> Python Java Javascript
----> Django Spring Boot React JS
----> Fullstack Fullstack Frontend
--------------------
----> Go Rust C#
----> Gin Actix .Net
----> Backend Backend Fullstack
--------------------

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article