Python is a scripting language that is highly readable, interactive, high-level, object-oriented, and interpreted. Python has lesser syntactic structures than other programming languages, and it typically uses English terms instead of punctuation.

Main Features Of Python include:

  • Beginner-friendly Language - Python is easy to learn, maintain, implement and read. It is interactive in nature.
  • Object-Oriented - Python encapsulates code within objects by supporting the Object-Oriented programming approach or style or approach.
  • Industry-oriented - Python is extendable, portable, scalable, cross-platform friendly with a standard library, and has support for GUI applications and interactive mode.

What is Polymorphism in Python?

In Python, polymorphisms refer to the occurrence of something in multiple forms. As part of polymorphism, a Python child class has methods with the same name as a parent class method. This is an essential part of programming. A single type of entity is used to represent a variety of types in different contexts (methods, operators, objects, etc.)

Polymorphism may be used in Python in various ways. Polymorphism can be defined using numerous functions, class methods, and objects. So, let us deep-dive into each of these ways.

Learn more about Python and Data Science with our Caltech Post Graduate Program In Data Science.

Polymorphism With Function and Objects

To induce polymorphism using a function, we need to create such a function that can take any object. Here is an example of Polymorphism using functions and objects:

Code

class Dog(): 

     def animal_kingdom(self): 

       print("Mammal") 

     def legs(self):

       print("Four") 

class Lizard(): 

     def animal_kingdom(self): 

       print("Mammal") 

     def legs(self): 

       print("Four") 

def function1(obj): 

       obj.animal_kingdom() 

       obj.legs()

obj_dog = Dog() 

obj_lizard = Lizard() 

function1(obj_dog) 

function1(obj_lizard)

Output

Mammal

Four

Mammal

Four

In the above example, the function, function1() takes in an object named obj, which in turn lets the functions call the methods, animal_kingdom() and legs() of both the classes, Dog and Lizard. To do this, we must create the instances of both classes.

Polymorphism With Class Methods

Let us discuss how to implement Polymorphism in Python using Class Methods. 

In the same way, Python makes use of two separate class types. For this, we design a for loop that iterates through an item tuple. After that, we need to perform method calling without regard for the class type of each object. We take it for granted that these methods have their existence in each of the classes.

A perfect example depicting Polymorphism with Class Methods using Python:

Code

class Car():

     def wheels(self):

       print(4)

     def mode_of_transport(self):

       print("Private usually")

class Bus():

     def wheels(self):

       print(8)

     def mode_of_transport(self):

       print("Public usually") 

obj_car = Car()

obj_bus = Bus()

for vehicle in (obj_car, obj_bus):

    vehicle.wheels()

    vehicle.mode_of_transport()

Output

4

Private usually

8

Public usually

In the above example, class methods wheels(), mode_of_transport(), which belong to Car and Bus classes, are directly invoked using the instances of these two classes in the for loop, which loops through both the class methods.

Polymorphism With Inheritance

Polymorphism, a child class method is allowed to have the same name as the class methods in the parent class. In inheritance, the methods belonging to the parent class are passed down to the child class. It's also possible to change a method that a child class has inherited from its parent.

This is typically used whenever a parent class inherited method is not appropriate for the child class. To rectify this situation, we use Method Overriding, which enables re-implementing of a method in a child class.

What Is Method Overriding?

Method overriding is an object-oriented programming technique that lets us change the function implementation of a parent class in the child class. Method overriding is basically a child class's ability to implement change in any method given by one of its parent classes.

Conditions of Method Overriding:

  • There should be inheritance. Within a class, function overriding is not possible, therefore a child class is derived from a parent class.
  • A function of the child class should have the same number of parameters as that of the parent class.

We know that in inheritance, a child class gets access to the protected and public methods and variables of the parent class whenever it inherits it. We exploit this concept to implement Polymorphism using Inheritance as well.

A perfect example of Method Overriding and Polymorphism with Inheritance is:

Code

class Vehicle:

     def desc(self):

       print("So many categories of vehicle")

     def wheels(self):

       print("Differs according to the vehicle category")

class car(Vehicle):

     def wheels(self):

       print(4)

class bus(Vehicle):

     def wheel(self):

       print(8)

obj_vehicle = Vehicle()

obj_car = car()

obj_bus = bus()

obj_vehicle.desc()

obj_vehicle.wheels()

obj_car.desc()

obj_car.wheels()

obj_bus.desc()

obj_bus.wheels()

Output

So many categories of vehicle

Differs according to the vehicle category

So many categories of vehicle

4

So many categories of vehicle

Differs according to the vehicle category

Master Python and Data Science With Simplilearn

In this article, we discussed Polymorphism and its different types in Python. Polymorphism is a very important OOP Concept and has several real-life uses as well in Python or any other OOP-related programming languages. 

With relevant practice, examples, and deep diving, one can master any concept related to Python and/or OOPS. For this, we recommend checking out our Caltech Post Graduate Program In Data Science to truly master Python and Data Science.

Data Science & Business Analytics Courses Duration and Fees

Data Science & Business Analytics programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Data Analytics Bootcamp

Cohort Starts: 7 May, 2024

6 Months$ 8,500
Applied AI & Data Science

Cohort Starts: 14 May, 2024

3 Months$ 2,624
Post Graduate Program in Data Analytics

Cohort Starts: 27 May, 2024

8 Months$ 3,749
Post Graduate Program in Data Science

Cohort Starts: 28 May, 2024

11 Months$ 4,199
Caltech Post Graduate Program in Data Science

Cohort Starts: 29 May, 2024

11 Months$ 4,500
Data Scientist11 Months$ 1,449
Data Analyst11 Months$ 1,449