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.
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
print (add(8, 3));
# 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
objct i_o = Fruit()// call the fruit
How does Polymorphism work 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.
Let us discuss examples of Polymorphism in Python.
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.
print("Most of the grasses have no_category.")
print("herbs are also one form of tree and plant.")
print("root don’t lie in any tree category.")
obj_tree = Tree()
obj_gr = root()
obj_herbs = herbs()
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.
This example demonstrates the implementation of polymorphism using a function and its respective methods as shown in the output below.
print("Music has a self_healing power")
print("Singers have to be versatile&melodious in singing.")
print("Music in its zone is getting evolved_.")
print("Garanas have their own taste and trend in Classical_music")
print("Singers_start the root with classical music and slowly to other taste.")
print("Classical_Music still needs to be fused to make it jazzy..")
obj_mus = Music()
obj_singer = Classical_Music()
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.
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.
print("BMW is considred_one_of the best sedans.")
print("It is types_of_Cars which makes it unique")
print("This brand has created a mark to every_buyer.")
print("Porsche is_also_one _of the luxurious brand_evveryone wants for..")
print("It has stylish look and feel with lot of comfrt")
print("These types are cars are_dreams for some.")
obj_sed = Sedan()
obj_suv = SUV()
for automobiles in (obj_sed, obj_suv):
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.
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, 4))
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([20, 18, 15]))
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.
This is a guide to Polymorphism in Python. Here we discuss the Definition, syntax, How Polymorphism works in Python?, examples with code implementation. You may also have a look at the following articles to learn more –