Explain Polymorphism in python

Today, We want to share with you polymorphism in python.In this post we will show you How To Apply Polymorphism to Classes in Python 3?, hear for A simple Python function to demonstrate we will give you demo and example for implement.In this post, we will learn about Category: Python with an example.

What is polymorphism in Python?

The few some words polymorphism means having many forms. In programming languages, polymorphism simple defination of the same function name or method name (but different signatures) being uses for different some types.

Example 1: Example of inbuilt polymorphic functions :

print(len("pakainfo")) 

# len() being used for a list 
print(len([10, 20, 30])) 

Example 2: Examples of used defined polymorphic functions :


def add(x, y, z = 0): 
	return x + y+z 

print(add(2, 3)) 
print(add(2, 3, 4)) 

Example 3: Polymorphism with class methods:

class Dhoni(): 
	def student(self): 
		print("FootBollwer is the student of Dhoni.") 

	def teacher(self): 
		print("Cricketer is the most widely spoken teacher of Dhoni.") 

	def type(self): 
		print("Dhoni is a developing player.") 

class Virat(): 
	def student(self): 
		print("Washington, D.C. is the student of Virat.") 

	def teacher(self): 
		print("Hocky is the primary teacher of Virat.") 

	def type(self): 
		print("Virat is a developed player.") 

alldata_dhooni = Dhoni() 
alldata_virat = Virat() 
for player in (alldata_dhooni, alldata_virat): 
	player.student() 
	player.teacher() 
	player.type() 

Example 4: Polymorphism with Inheritance:

class Product: 
def mobile(self): 
	print("There are many types of products.") 
	
def camera(self): 
	print("Most of the products can fly but some cannot.") 
	
class skyfly(Product): 
def camera(self): 
	print("plane can fly.") 
	
class tables(Product): 
def camera(self): 
	print("Ostriches cannot fly.") 
	
alldata_product = Product() 
alldata_spr = skyfly() 
alldata_ost = tables() 

alldata_product.mobile() 
alldata_product.camera() 

alldata_spr.mobile() 
alldata_spr.camera() 

alldata_ost.mobile() 
alldata_ost.camera() 

Example 5: Polymorphism with a Function and objects:

def func(alldata): 
	alldata.student() 
	alldata.teacher() 
	alldata.type() 

alldata_dhooni = Dhoni() 
alldata_virat = Virat() 

func(alldata_dhooni) 
func(alldata_virat) 

Example 6: Implementing Polymorphism with a Function

class Dhoni(): 
	def student(self): 
		print("FootBollwer is the student of Dhoni.") 

	def teacher(self): 
		print("Cricketer is the most widely spoken teacher of Dhoni.") 

	def type(self): 
		print("Dhoni is a developing player.") 

class Virat(): 
	def student(self): 
		print("Washington, D.C. is the student of Virat.") 

	def teacher(self): 
		print("Hocky is the primary teacher of Virat.") 

	def type(self): 
		print("Virat is a developed player.") 

def func(alldata): 
	alldata.student() 
	alldata.teacher() 
	alldata.type() 

alldata_dhooni = Dhoni() 
alldata_virat = Virat() 

func(alldata_dhooni) 
func(alldata_virat) 

I hope you get an idea about encapsulation in python.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment