Online Tutorials & Training Materials | STechies.com
Register Login

What is Polymorphism in Python?

|| 0

What is Polymorphism in Python?
Stechies

Python Polymorphism

Sometimes objects are available in different forms of types. Say, if button is an object in the reckoning, then it is possible to have draw outputs in different forms/ types - check button, round button, square button, button with images, etc. However, they all share a similar logic: onClick() and can be accessed via the same method. This concept is best defined as polymorphism.

The term polymorphism has its origin the Greek words poly meaning many and morphism meaning form. It creates structures that are capable of taking or using different forms of objects.

Example of Polymorphism in Python

class Employee:
    def salary(self):
        name = "xyz"
        dep = "xxx"
        salary = 20000
        tax = 0.15*salary
        netsalary = salary-tax
        print(name + " from "+dep+ " department earns monthly(after tax) ",netsalary)
class Admin:
    def salary(self):
        name = "abc"
        dep = "yyy"
        salary = 30000
        tax = 0.15*salary
        netsalary = salary-tax
        print(name + " from "+dep+ " department earns monthly(after tax) ",netsalary)


obj_emp = Employee()
obj_adm = Admin()

obj_emp.salary()
obj_adm.salary()

Output:

xyz from xxx department earns monthly(after tax) 17000.0
abc from yyy department earns monthly(after tax)  25500.0

Polymorphism with Abstract Class Methods

Polymorphism with an abstract class is one of the most used methods in Python. The abstract structure finds its definition in the Document class. In the event of the user creating an editor, it will be difficult for him to know in advance about the type of documents that will be opened in due course -word format or pdf format? The method of using polymorphism with abstract class helps in accessing them with ease rather than having numerous types for each document.
In order to get the right result, an abstract class called document has to be created.

The newly created class will not have any implementations; however, it will succeed in defining the structure that will be contained by all forms (this will be in the form of functions). Once the function show() is defined, then both word and pdf documents must contain the show() function.

Example: Polymorphism with Abstract Class Methods

from abc import ABC, abstractmethod

class Polygon(ABC):
    def area(self):
        pass

class Triang(Polygon):
    def area(self):
        width = 20
        height = 10
        area = (width * height) / 2
        print(area)

class rectang(Polygon):
    def area(self):
        width = 20
        height = 10
        area = (width * height)
        print(area)


TA = Triang()
TA.area()

RA = rectang()
RA.area() 

Output:

100.0
200

Polymorphism with a Function

The following program depicts the use of polymorphic functions in the functional programming language, Python.

Example polymorphism with function

# A simple Python function to demonstrate Polymorphism
def multiply(a, b, z=1):
	return a * b*z
# Driver code 
print(multiply(3, 4)) 
print(multiply(2, 3, 4))

Output:

12
24

Literally, polymorphism in Python refers to its ability to take on various forms. It allows for the declaration of methods / functions in the child class. The newly defined function will possess the same name as is defined by the specific parent class. As is already known, all methods belonging to the parent class is inherited by the child class. However, it is common for coders to encounter certain methods/ functions that have been inherited by the child class from the parent class but do not fit in to perfection. In order to avoid runtime errors, the specific methods have to be re-implemented in the child class. The ensuing process is referred to as Method Overriding.

Once a method or function has been overridden in the child class, then the new version of the function is to be called on the basis of the object type that is used for the purpose of invoking the same. In the event of a child class object being used for calling an overridden method, the child class version belonging to the function will be called. Conversely, in case of the parent class object being used for calling any overridden method, the version belonging to the parent class function will be called.

Conclusion

Polymorphism is when various kinds respond to same function. In other words, it means that the same functions are defined on different types of objects. It is a very useful attribute of Python programmers as it makes coding easier and more intuitive. In Python, protocols are provided for coding purposes. These protocols are useful for implementing consistent behavior for different kinds of built-in objects in Python.