polymorphism-in-python

Polymorphism in Python: Explained in a Simple Way

Polymorphism is a property of programming languages that enables the same class or an object to have multiple types and act differently based on the type.

The type at any given time can only be one; it cannot switch back and forth between different types.

polymorphism in python

In Python, this is implemented through special methods known as “class methods” or “deterministic final methods”. Let’s understand polymorphism in Python in detail with examples.

What is Polymorphism?

Polymorphism is the ability of a single piece of code to take multiple forms. For example, consider the case of an algorithm that can be implemented in two different ways.

If a programmer replaces the code with a single statement, the algorithm will be applied based on the syntax of the statement, not the syntax of the original code.

The first letter in the word “polymorphism” is “poly” which means “many”. And the second letter is “morphism” which means “form”. So the entire word means “many forms” which can be used in a single piece of code.

Polymorphism in Python

When we talk about polymorphism in Python, we’re referring to the ability of a single class to implement multiple interfaces.

Interfaces are just like contracts that define the set of methods that an object should implement. Let’s say we have a Python class, “Animal”, which has the following two methods: def walk(self): def run(self): Now, if we want to make this class implement the “Walker” and “Runner” interfaces, we can do it as follows:

Polymorphism with Equational Languages

An equational language is one in which the syntax of an expression determines what operation to perform.

So, the same expression can produce different results based on the values of its variables. For example, in a language like algebra, the expression “x + y” will be evaluated as “x + y” regardless of the values of “x” and “y”.

In Python, we use special methods called “class methods” or “deterministic final methods” to implement polymorphism.

We can pass any method as an optional parameter to the “in” and “not in” operators. In the example below, we’re trying to check whether an instance of the Animal class has the “Walk” method.

Polymorphism with Object-Oriented Languages

Object-oriented languages use class hierarchies to implement polymorphism. A single class can inherit properties from multiple parent classes, and implement multiple interfaces.

When a method is not found in the current class, the method of the parent class is executed. For example, let’s create a Python class called “Animal” with the “walk()” and “run()” methods:

A class hierarchy is created by creating multiple classes that inherit from the Animal class. For example, “Dog”, “Cat”, and “Mouse” classes inherit from the “Animal” class and have “bark()” and “meow()” methods:

Polymorphic Behavior in Python

When an object is passed to an operator such as “in” or “not in”, the interpreter first tries to find that method in the object.

If the method is not found, the interpreter will try to find that method in the parent class. If the method is not found in any parent class, the interpreter will raise a “NotImplementedError” error.

Let’s use the example below. We’re trying to find whether an instance of the Animal class has the “Walk” method.

Types of Polymorphism in Python

– Overloading : It is the ability of a single method to take different forms depending on the arguments passed to it. For example, you can have two methods called “add()”. The only difference is that one method adds integers while the other adds strings.

– Overriding : It is the ability of a child class to redefine the functionality of a parent class. For example, let’s create a “Walk” method in the Animal class to walk a given distance.

How to implement polymorphism in Python?

There are two ways to implement polymorphism in Python. They are: – Using “in” and “not in” operators – Using inheritance

Using “in” and “not in” operators

You can implement polymorphism using the “in” and “not in” operators if you want to check for the presence of a particular method in an instance.

The syntax for the “in” operator is: obj in otherobj The syntax for the “not in” operator is: obj not in otherobj Let’s understand it with the help of the example below.

Here, we’re checking whether an instance of the Animal class has the “Walk” method. And if it has, we’re printing the distance that should be walked by an animal of that class.

Using inheritance

Inheritance is the process of creating a new class by extending an existing class. The new class gets all the properties of the existing class, and you can override some or all of the properties.

Let’s understand it with the help of the example below. Here, we’re creating an Animal class that has the “Walk” and “Run” methods. And the “Dog”, “Cat”, and “Mouse” classes inherit from the Animal class.

Moreover, when an instance of the Animal class is passed, we’re printing the distance walked by a mammal and by a rodent.

And when an instance of the “Dog”, “Cat”, and “Mouse” classes is passed, we’re printing the distance walked by a dog, a cat, and a mouse, respectively.

Conclusion

In this article, we explored what polymorphism is and how it is implemented in both equational and object-oriented programming languages.

We also looked at the various types of polymorphism in Python and how to implement it. Now that you understand the basics of polymorphism, you can write better code by taking advantage of it.

And if you’re preparing for a programming interview, this would come in handy.

Add a Comment

Your email address will not be published. Required fields are marked *