Polymorphism In Python

What we are going to see through this article is we will see in detail about Polymorphism In Python. Python polymorphism is a form of process in which an event or object has different forms.

Polymorphism usually appears in many forms, where the emphasis is on whether it should be implemented as a class, function, or membership variable. A particular company operates that can take on different forms simultaneously as needed.

We can use five different methods to perform a task in the below article. Those are:

  • Inbuilt polymorphic functions,
  • User-defined polymorphic functions,
  • Class methods,
  • Inheritance,
  • a Function and objects.
Polymorphism In Python

Polymorphism in Python using inbuilt polymorphic functions

# Python program to demonstrate in-built poly-
# morphic functions

# len() being used for a string
print(len("codingdiksha"))

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

Output

This is the output.

12
5

Polymorphism in Python using user-defined polymorphic functions

Below, The User-defined polymorphic functions will use to do a task here.

# A simple Python function to demonstrate
# Polymorphism

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

# Driver code
print(add(2, 3))
print(add(2, 3, 4))

Output

This is the output.

5
9

Using class methods

Here, We can use the class methods to perform a task here.

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()

Output

This is the output.

New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

Using Inheritance

Instead of using the class methods, We can use the Inheritance method to execute a task here.

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()

Output

This is an example output for your reference.

There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

Using a Function and objects

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.")

def func(obj):
	obj.capital()
	obj.language()
	obj.type()

obj_ind = India()
obj_usa = USA()

func(obj_ind)
func(obj_usa)

Read Also: String Comparison In Python

Output

At last, This is the output.

New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

Final Thoughts

What we learned through this article is Polymorphism In Python. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.

Leave a Comment