PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Polymorphism in Python

Polymorphism in Python

Updated on: October 21, 2021 | 15 Comments

Object-Oriented Programming (OOP) has four essential characteristics: abstraction, encapsulation, inheritance, and polymorphism.

This lesson will cover what polymorphism is and how to implement them in Python. Also, you’ll learn how to implement polymorphism using function overloading, method overriding, and operator overloading.

Table of contents

  • What is Polymorphism in Python?
    • Polymorphism in Built-in function len()
  • Polymorphism With Inheritance
    • Example: Method Overriding
    • Overrride Built-in Functions
  • Polymorphism In Class methods
    • Polymorphism with Function and Objects
  • Polymorphism In Built-in Methods
  • Method Overloading
  • Operator Overloading in Python
    • Overloading + operator for custom objects
    • Overloading the * Operator
    • Magic Methods

What is Polymorphism in Python?

Polymorphism in Python is the ability of an object to take many forms. In simple words, polymorphism allows us to perform the same action in many different ways.

For example, Jessa acts as an employee when she is at the office. However, when she is at home, she acts like a wife. Also, she represents herself differently in different places. Therefore, the same person takes different forms as per the situation.

person takes different forms
A person takes different forms

In polymorphism, a method can process objects differently depending on the class type or data type. Let’s see simple examples to understand it better.

Polymorphism in Built-in function len()

The built-in function len() calculates the length of an object depending upon its type. If an object is a string, it returns the count of characters, and If an object is a list, it returns the count of items in a list.

The len() method treats an object as per its class type.

Example:

students = ['Emma', 'Jessa', 'Kelly']
school = 'ABC School'

# calculate count
print(len(students))
print(len(school))Code language: Python (python)

Output

3
10
Polymorphic len() function
Polymorphic len() function

Polymorphism With Inheritance

Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes and methods of a parent class. The existing class is called a base class or parent class, and the new class is called a subclass or child class or derived class.

Using method overriding polymorphism allows us to defines methods in the child class that have the same name as the methods in the parent class. This process of re-implementing the inherited method in the child class is known as Method Overriding.

Advantage of method overriding

  • It is effective when we want to extend the functionality by altering the inherited method. Or the method inherited from the parent class doesn’t fulfill the need of a child class, so we need to re-implement the same method in the child class in a different way.
  • Method overriding is useful when a parent class has multiple child classes, and one of that child class wants to redefine the method. The other child classes can use the parent class method. Due to this, we don’t need to modification the parent class code

In polymorphism, Python first checks the object’s class type and executes the appropriate method when we call the method. For example, If you create the Car object, then Python calls the speed() method from a Car class.

Let’s see how it works with the help of an example.

Example: Method Overriding

In this example, we have a vehicle class as a parent and a ‘Car’ and ‘Truck’ as its sub-class. But each vehicle can have a different seating capacity, speed, etc., so we can have the same instance method name in each class but with a different implementation. Using this code can be extended and easily maintained over time.

Polymorphism with Inheritance
Polymorphism with Inheritance
class Vehicle:

    def __init__(self, name, color, price):
        self.name = name
        self.color = color
        self.price = price

    def show(self):
        print('Details:', self.name, self.color, self.price)

    def max_speed(self):
        print('Vehicle max speed is 150')

    def change_gear(self):
        print('Vehicle change 6 gear')


# inherit from vehicle class
class Car(Vehicle):
    def max_speed(self):
        print('Car max speed is 240')

    def change_gear(self):
        print('Car change 7 gear')


# Car Object
car = Car('Car x1', 'Red', 20000)
car.show()
# calls methods from Car class
car.max_speed()
car.change_gear()

# Vehicle Object
vehicle = Vehicle('Truck x1', 'white', 75000)
vehicle.show()
# calls method from a Vehicle class
vehicle.max_speed()
vehicle.change_gear()Code language: Python (python)

Output:

Details: Car x1 Red 20000
Car max speed is 240
Car change 7 gear

Details: Truck x1 white 75000
Vehicle max speed is 150
Vehicle change 6 gear

As you can see, due to polymorphism, the Python interpreter recognizes that the max_speed() and change_gear() methods are overridden for the car object. So, it uses the one defined in the child class (Car)

On the other hand, the show() method isn’t overridden in the Car class, so it is used from the Vehicle class.

Overrride Built-in Functions

In Python, we can change the default behavior of the built-in functions. For example, we can change or extend the built-in functions such as len(), abs(), or divmod() by redefining them in our class. Let’s see the example.

Example

In this example, we will redefine the function len()

class Shopping:
    def __init__(self, basket, buyer):
        self.basket = list(basket)
        self.buyer = buyer

    def __len__(self):
        print('Redefine length')
        count = len(self.basket)
        # count total items in a different way
        # pair of shoes and shir+pant
        return count * 2

shopping = Shopping(['Shoes', 'dress'], 'Jessa')
print(len(shopping))
Code language: Python (python)

Output

Redefine length
4

Polymorphism In Class methods

Polymorphism with class methods is useful when we group different objects having the same method. we can add them to a list or a tuple, and we don’t need to check the object type before calling their methods. Instead, Python will check object type at runtime and call the correct method. Thus, we can call the methods without being concerned about which class type each object is. We assume that these methods exist in each class.

Python allows different classes to have methods with the same name.

  • Let’s design a different class in the same way by adding the same methods in two or more classes.
  • Next, create an object of each class
  • Next, add all objects in a tuple.
  • In the end, iterate the tuple using a for loop and call methods of a object without checking its class.

Example

In the below example, fuel_type() and max_speed() are the instance methods created in both classes.

class Ferrari:
    def fuel_type(self):
        print("Petrol")

    def max_speed(self):
        print("Max speed 350")

class BMW:
    def fuel_type(self):
        print("Diesel")

    def max_speed(self):
        print("Max speed is 240")

ferrari = Ferrari()
bmw = BMW()

# iterate objects of same type
for car in (ferrari, bmw):
    # call methods without checking class of object
    car.fuel_type()
    car.max_speed()Code language: Python (python)

Output

Petrol
Max speed 350

Diesel
Max speed is 240

As you can see, we have created two classes Ferrari and BMW. They have the same instance method names fuel_type() and max_speed(). However, we have not linked both the classes nor have we used inheritance.

We packed two different objects into a tuple and iterate through it using a car variable. It is possible due to polymorphism because we have added the same method in both classes Python first checks the object’s class type and executes the method present in its class.

Polymorphism with Function and Objects

We can create polymorphism with a function that can take any object as a parameter and execute its method without checking its class type. Using this, we can call object actions using the same function instead of repeating method calls.

Example

class Ferrari:
    def fuel_type(self):
        print("Petrol")

    def max_speed(self):
        print("Max speed 350")

class BMW:
    def fuel_type(self):
        print("Diesel")

    def max_speed(self):
        print("Max speed is 240")

# normal function
def car_details(obj):
    obj.fuel_type()
    obj.max_speed()

ferrari = Ferrari()
bmw = BMW()

car_details(ferrari)
car_details(bmw)
Code language: Python (python)

Output

Petrol
Max speed 350
Diesel
Max speed is 240

Polymorphism In Built-in Methods

The word polymorphism is taken from the Greek words poly (many) and morphism (forms). It means a method can process objects differently depending on the class type or data type.

The built-in function reversed(obj) returns the iterable by reversing the given object. For example, if you pass a string to it, it will reverse it. But if you pass a list of strings to it, it will return the iterable by reversing the order of elements (it will not reverse the individual string).

Let us see how a built-in method process objects having different data types.

Example:

students = ['Emma', 'Jessa', 'Kelly']
school = 'ABC School'

print('Reverse string')
for i in reversed('PYnative'):
    print(i, end=' ')

print('\nReverse list')
for i in reversed(['Emma', 'Jessa', 'Kelly']):
    print(i, end=' ')Code language: Python (python)

Output:

Reverse string
e v i t a n Y P 

Reverse list
Kelly Jessa Emma 

Method Overloading

The process of calling the same method with different parameters is known as method overloading. Python does not support method overloading. Python considers only the latest defined method even if you overload the method. Python will raise a TypeError if you overload the method.

Example

def addition(a, b):
    c = a + b
    print(c)


def addition(a, b, c):
    d = a + b + c
    print(d)


# the below line shows an error
# addition(4, 5)

# This line will call the second product method
addition(3, 7, 5)
Code language: Python (python)

To overcome the above problem, we can use different ways to achieve the method overloading. In Python, to overload the class method, we need to write the method’s logic so that different code executes inside the function depending on the parameter passes.

For example, the built-in function range() takes three parameters and produce different result depending upon the number of parameters passed to it.

Example:

for i in range(5): print(i, end=', ')
print()
for i in range(5, 10): print(i, end=', ')
print()
for i in range(2, 12, 2): print(i, end=', ')Code language: Python (python)

Output:

0, 1, 2, 3, 4, 
5, 6, 7, 8, 9, 
2, 4, 6, 8, 10,

Let’s assume we have an area() method to calculate the area of a square and rectangle. The method will calculate the area depending upon the number of parameters passed to it.

  • If one parameter is passed, then the area of a square is calculated
  • If two parameters are passed, then the area of a rectangle is calculated.

Example: User-defined polymorphic method

class Shape:
    # function with two default parameters
    def area(self, a, b=0):
        if b > 0:
            print('Area of Rectangle is:', a * b)
        else:
            print('Area of Square is:', a ** 2)

square = Shape()
square.area(5)

rectangle = Shape()
rectangle.area(5, 3)
Code language: Python (python)

Output:

Area of Square is: 25
Area of Rectangle is: 15

Operator Overloading in Python

Operator overloading means changing the default behavior of an operator depending on the operands (values) that we use. In other words, we can use the same operator for multiple purposes.

For example, the + operator will perform an arithmetic addition operation when used with numbers. Likewise, it will perform concatenation when used with strings.

The operator + is used to carry out different operations for distinct data types. This is one of the most simple occurrences of polymorphism in Python.

Example:

# add 2 numbers
print(100 + 200)

# concatenate two strings
print('Jess' + 'Roy')

# merger two list
print([10, 20, 30] + ['jessa', 'emma', 'kelly'])
Code language: Python (python)

Output:

300
JessRoy
[10, 20, 30, 'jessa', 'emma', 'kelly']

Overloading + operator for custom objects

Suppose we have two objects, and we want to add these two objects with a binary + operator. However, it will throw an error if we perform addition because the compiler doesn’t add two objects. See the following example for more details.

Example:

class Book:
    def __init__(self, pages):
        self.pages = pages

# creating two objects
b1 = Book(400)
b2 = Book(300)

# add two objects
print(b1 + b2)
Code language: Python (python)

Output

TypeError: unsupported operand type(s) for +: 'Book' and 'Book'

We can overload + operator to work with custom objects also. Python provides some special or magic function that is automatically invoked when associated with that particular operator.

For example, when we use the + operator, the magic method __add__() is automatically invoked. Internally + operator is implemented by using __add__() method. We have to override this method in our class if you want to add two custom objects.

Example:

class Book:
    def __init__(self, pages):
        self.pages = pages

    # Overloading + operator with magic method
    def __add__(self, other):
        return self.pages + other.pages

b1 = Book(400)
b2 = Book(300)
print("Total number of pages: ", b1 + b2)
Code language: Python (python)

Output

Total number of pages: 700

Overloading the * Operator

The * operator is used to perform the multiplication. Let’s see how to overload it to calculate the salary of an employee for a specific period. Internally * operator is implemented by using the __mul__() method.

Example:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def __mul__(self, timesheet):
        print('Worked for', timesheet.days, 'days')
        # calculate salary
        return self.salary * timesheet.days


class TimeSheet:
    def __init__(self, name, days):
        self.name = name
        self.days = days


emp = Employee("Jessa", 800)
timesheet = TimeSheet("Jessa", 50)
print("salary is: ", emp * timesheet)
Code language: Python (python)

Output

Wroked for 50 days
salary is:  40000

Magic Methods

In Python, there are different magic methods available to perform overloading operations. The below table shows the magic methods names to overload the mathematical operator, assignment operator, and relational operators in Python.

Operator NameSymbolMagic method
Addition +__add__(self, other)
Subtraction-__sub__(self, other)
Multiplication *__mul__(self, other)
Division/__div__(self, other)
Floor Division //__floordiv__(self,other)
Modulus %__mod__(self, other)
Power **__pow__(self, other)
Increment +=__iadd__(self, other)
Decrement -=__isub__(self, other)
Product *=__imul__(self, other)
Division /+__idiv__(self, other)
Modulus %=__imod__(self, other)
Power**=__ipow__(self, other)
Less than <__lt__(self, other)
Greater than >__gt__(self, other)
Less than or equal to<=__le__(self, other)
Greater than or equal to >=__ge__(self, other)
Equal to ==__eq__(self, other)
Not equal !=__ne__(self, other)
magic methods

Filed Under: Python, Python Object-Oriented Programming (OOP)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Object-Oriented Programming (OOP)

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Comments

  1. ImageMUJEEB says

    July 25, 2022 at 3:01 pm

    There are some topics of OOP that are missing and i would like to learn those topics from this website as i am prepairing for my exams. such as ducktyping, exception handling , abstraction, etc

    Reply
  2. ImageTash says

    July 13, 2022 at 2:21 pm

    Hey Vishal,
    just wondering why Jessa’s wife role is big and purple… more important than her others perhaps? and what does it mean to ‘act like a wife’? just curious…

    Reply
    • ImageVishal says

      July 19, 2022 at 11:52 am

      Hi Tash, the creator forgot to change the purple color to blue.

      Reply
  3. ImageDimitris says

    May 2, 2022 at 11:34 pm

    The second line of the following example was probably there by accident

    students = ['Emma', 'Jessa', 'Kelly']
    school = 'ABC School'
    
    print('Reverse string')
    for i in reversed('PYnative'):
        print(i, end=' ')
    .....
    .....
    .....
    Reply
  4. ImageDimitris says

    May 2, 2022 at 11:30 pm

    Can we use three books in this example?

    class Book:
        def __init__(self, pages):
            self.pages = pages
    
        # Overloading + operator with magic method
        def __add__(self, other):
            return self.pages + other.pages
    
    b1 = Book(400)
    b2 = Book(300)
    print("Total number of pages: ", b1 + b2)
    Reply
    • ImagePaul says

      July 12, 2022 at 3:38 pm

      class Book:
          def __init__(self, pages):
              self.pages = pages
              
      
          # Overloading + operator with magic method
          def __add__(self, other):
              if type(other) == int:
                  return Book(self.pages + other)
              else:
                  return Book(self.pages + other.pages)
          
      
                 
      # Creating object
      b1 = Book(400)
      b2 = Book(300)
      b3 = Book(400)
      
      total = b1 + b2 + b3
      print(total.pages)
      Reply
    • ImagePaul says

      July 12, 2022 at 3:41 pm

      class Book:
          def __init__(self, pages):
              self.pages = pages
              
      
          # Overloading + operator with magic method
          def __add__(self, other):
              if type(other) == int:
                  return Book(self.pages + other)
              else:
                  return Book(self.pages + other.pages)
          
      
                 
      # Creating object
      b1 = Book(400)
      b2 = Book(300)
      b3 = Book(400)
      
      total = b1 + b2 + b3
      print(total.pages)
      Reply
  5. ImageHyuk Cho says

    October 15, 2021 at 6:31 am

    It is just a typo in the Magic Methods.
    Current: Greater than or equal to >= __gt__(self, other)
    Correct: Greater than or equal to. >= __ge__(self, other)

    I enjoy reading the compact, precise, and correct summary of Python programming topics in PYnative.
    Thanks,

    HC

    Reply
    • ImageVishal says

      October 21, 2021 at 4:38 pm

      Hey Hyuk Cho, Thank you for your observation. I have updated the article accordingly.

      Reply
  6. ImageDias says

    October 10, 2021 at 5:26 pm

    In the Section: “Example: Method Overriding”

    After the code presentation, we have the following explanation:

    “As you can see, due to polymorphism, the Python interpreter recognizes that the max_speed() and fuel_type() methods are overridden for the car object. So, it uses the one defined in the child class (Car)”

    The method fuel_type() is not yet in the presented code . Maybe it should be change_gear().

    Reply
    • ImageVishal says

      October 12, 2021 at 10:57 am

      Thank you, Dias, for your observation.

      Reply
  7. ImageAiborlang says

    September 30, 2021 at 1:53 pm

    class English:
        def greeting(self):
            print("Hello")
    class French:
        def greeting(self):
            print("Bonjour")
    def intro(language):
        language.greeting()
    
    flora = English()
    aalase = French()
    intro(flora)
    intro(aalase)
    Reply
    • ImageAiborlang says

      September 30, 2021 at 1:53 pm

      Help me find the errors

      Reply
      • ImageDharambir says

        January 14, 2022 at 1:50 pm

        class English:
            def greeting(self):
                print("Hello")
        class French:
            def greeting(self):
                print("Bonjour")
        
        eng = English()
        frnch = French()
        for book in (eng,frnch):
            book.greeting()
        Reply
    • ImageThe guardian says

      May 23, 2022 at 11:02 am

      which errors

      Reply

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Python Python Object-Oriented Programming (OOP)
TweetF  sharein  shareP  Pin

  Python OOP

  • Python OOP
  • Classes and Objects in Python
  • Constructors in Python
  • Python Destructors
  • Encapsulation in Python
  • Polymorphism in Python
  • Inheritance in Python
  • Python Instance Variables
  • Python Instance Methods
  • Python Class Variables
  • Python Class Method
  • Python Static Method
  • Python Class Method vs. Static Method vs. Instance Method
  • Python OOP exercise

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com

Advertisement