Updated April 11, 2023
Definition of Python Polymorphism
Python Polymorphism is one form of process where one occurrence or object can have different forms. Polymorphism generally appears in many forms where the significance lies as per the requirement whether to implement it within the class, function, or as a member variable. One single entity acts as a reference that can acquire different forms at one single time according to the need. The entity can either be an operator, object or method wherein if in case it is present at a functional level it will deal with all the operators or member variables related to it.
Syntax:
Python polymorphism also has a specific syntax unlike other object-oriented programming languages which is represented as follows:
# Defined polymorphic function syntax:
This syntax is represented for polymorphic function type.
def function() \\ definition of function
{
Return reslt;
}
Driver code
{
print (add(8, 3));
print (add(3,5,7));
}
# Class Polymorphic function syntax:
Same can be done in a class which is represented as follows:
class Fruit(): // Fruit class
def type (self):
print ("the fruit looks like this") //Defines and print the fruits
def type();
print();
objct i_o = Fruit() // call the fruit
for loop
How does Polymorphism works in Python?
- Whenever the term polymorphism is used it mostly signifies the object-oriented language concept religiously.
- It makes the requirement fulfilled if the scenario comes in terms of class, function or methods available.
- When the scenario comes wherein the classes present are of two different types and we want to make the objects behave in a similar form then a way out is to create a loop that can have the ability to traverse and iterate over the tuples which are present as part of objects.
- Polymorphism in python is also possible using child class that have several concepts supporting inheritance and the child class successfully inherits all the properties of the parent class at the time of execution. Even it has the possibility where the child class might have some of the methods that can also be used again especially at the time of implementation.
- When it comes in terms of functions where it needs to make the implementation of polymorphism there also two types of polymorphism can be considered majorly into inbuilt polymorphic function and user-defined polymorphic function where it is possible to create any number of custom functions with different types of operations into picture.
- Build-in functions also provides ease as mostly get covered by importing the required standard library.
- The functions and objects present within the polymorphism activity makes it instantiated with two different class but still can get a lot of similarity at the time of execution.
Examples
Let us discuss examples of Polymorphism in Python.
Example #1
This example represents the inheritance concept with python polymorphism where the class is tried to be implemented and is known as method overriding as shown in the output.
class Tree:
def tree_type(self):
print("There_are_many_types_of_trees.")
def grass(self):
print("Most of the grasses have no_category.")
class herbs(Tree):
def tree_type(self):
print("herbs are also one form of tree and plant.")
class root(Tree):
def tree_type(self):
print("root don’t lie in any tree category.")
obj_tree = Tree()
obj_gr = root()
obj_herbs = herbs()
obj_tree.tree_type()
obj_gr.grass()
obj_herbs.tree_type()
obj_herbs.tree_type()
obj_gr.tree_type()
obj_herbs.grass()
Output:
Explanation:
This example basically represents python polymorphism with the help of Trees and herbs and roots as type defined as part of the tree where the methods in the child class have same name as that of a parent class. Although there is a possibility that modify a method in child class which inherits the methods as part of parent class. This entire process where the method is tried to get implemented again is known as method Overriding.
Example #2
This example demonstrates the implementation of polymorphism using a function and its respective methods as shown in the output below.
class Music():
def Bollywood(self):
print("Music has a self_healing power")
def singer(self):
print("Singers have to be versatile&melodious in singing.")
def type(self):
print("Music in its zone is getting evolved_.")
class Classical_Music():
def Hollywood(self):
print("Garanas have their own taste and trend in Classical_music")
def singer(self):
print("Singers_start the root with classical music and slowly to other taste.")
def type(self):
print("Classical_Music still needs to be fused to make it jazzy..")
def func(obj_0):
obj_0.Bollywood()
obj_0.singer()
obj_0.type()
obj_mus = Music()
obj_singer = Classical_Music()
func(obj_mus)
Output:
Explanation: A function is created where the members of the class within a function are handled and then the created object is called for the manipulation of the entire function to instantiate.
Example #3
This example demonstrates the polymorphism in Python using class methods where the two types of classes which have different significance comes to be same although need some manipulation as shown below.
class Sedan():
def Brand(self):
print("BMW is considred_one_of the best sedans.")
def car_type(self):
print("It is types_of_Cars which makes it unique")
def type(self):
print("This brand has created a mark to every_buyer.")
class SUV():
def Brand(self):
print("Porsche is_also_one _of the luxurious brand_evveryone wants for..")
def car_type(self):
print("It has stylish look and feel with lot of comfrt")
def type(self):
print("These types are cars are_dreams for some.")
obj_sed = Sedan()
obj_suv = SUV()
for automobiles in (obj_sed, obj_suv):
automobiles.Brand()
automobiles.car_type()
automobiles.type()
Output:
Explanation: This Program demonstrates the polymorphism in python where the two differently defined class of sedan and SUV both have same type of behavior at the run time making them function as usual.
Example #4
This example demonstrates the custom-defined function where the parameters passed so defined performs the operation and returns the result as expected which is shown in the output.
def subtrct(p_0, q_0, r_0 = 0):
return p_0 - q_0-r_0
print(subtrct(2, 3))
print(subtrct(2, 3, 4))
Output:
Example #5
This program demonstrates the build-in function like getting the length of the string at the time of execution of code using python polymorphism which is shown as below.
print(len("Hope_Everything_Is_Fine!"))
print(len([20, 18, 15]))
Output:
Conclusion
Polymorphism in Python has lot of significance when it comes to run time and dynamic type of polymorphism. It helps in making the user make the custom functions also. It provides enough support for the classes, function and member variables to make respective usage when it comes to reference and implementation.
Recommended Articles
We hope that this EDUCBA information on “Polymorphism in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.