Python Polymorphism

Detailed Tutorial on Python Polymorphism

Welcome to codeswithpankaj.com! In this tutorial, we will explore the concept of polymorphism in Python. We'll cover what polymorphism is, how it works in Python, and provide detailed examples to illustrate its application.

Table of Contents

  1. Introduction to Polymorphism

  2. Types of Polymorphism

    • Compile-time Polymorphism

    • Runtime Polymorphism

  3. Polymorphism in Python

  4. Method Overloading

  5. Method Overriding

  6. Operator Overloading

  7. Polymorphism with Inheritance

  8. Duck Typing

  9. Practical Examples

  10. Summary

1. Introduction to Polymorphism

What is Polymorphism?

Polymorphism is a core concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to be used for different data types.

Why Use Polymorphism?

  • Flexibility: Polymorphism allows for flexibility and the reuse of code.

  • Maintainability: It makes the code easier to maintain and extend.

  • Readability: It enhances code readability and organization.

2. Types of Polymorphism

Compile-time Polymorphism

Also known as static polymorphism, it is achieved through method overloading and operator overloading. This type of polymorphism is resolved during compile time.

Runtime Polymorphism

Also known as dynamic polymorphism, it is achieved through method overriding. This type of polymorphism is resolved during runtime.

3. Polymorphism in Python

Python supports polymorphism through method overriding, operator overloading, and duck typing.

4. Method Overloading

What is Method Overloading?

Method overloading allows a class to have multiple methods with the same name but different parameters. Python does not support method overloading by default, but we can achieve it using default arguments.

class MathOperations:
    def add(self, a, b, c=0):
        return a + b + c

math_op = MathOperations()
print(math_op.add(2, 3))
print(math_op.add(2, 3, 4))

5. Method Overriding

What is Method Overriding?

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class.

class Animal:
    def sound(self):
        print("Animal makes a sound")

class Dog(Animal):
    def sound(self):
        print("Dog barks")

dog = Dog()
dog.sound()

6. Operator Overloading

What is Operator Overloading?

Operator overloading allows us to define custom behavior for operators in user-defined classes.

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __str__(self):
        return f"Vector({self.x}, {self.y})"

v1 = Vector(2, 3)
v2 = Vector(3, 4)
v3 = v1 + v2
print(v3)

7. Polymorphism with Inheritance

Polymorphism works well with inheritance, allowing methods to be overridden in derived classes.

Example: Polymorphism with Inheritance

class Bird:
    def fly(self):
        print("Bird can fly")

class Sparrow(Bird):
    def fly(self):
        print("Sparrow can fly")

class Ostrich(Bird):
    def fly(self):
        print("Ostrich cannot fly")

def make_bird_fly(bird):
    bird.fly()

sparrow = Sparrow()
ostrich = Ostrich()

make_bird_fly(sparrow)
make_bird_fly(ostrich)

8. Duck Typing

What is Duck Typing?

Duck typing is a concept where the type or class of an object is less important than the methods it defines. If an object implements certain methods, it can be used in place of another object with similar methods.

class Bird:
    def fly(self):
        print("Bird can fly")

class Airplane:
    def fly(self):
        print("Airplane can fly")

def make_it_fly(obj):
    obj.fly()

bird = Bird()
airplane = Airplane()

make_it_fly(bird)
make_it_fly(airplane)

9. Practical Examples

Example 1: Polymorphism with a Common Interface

class Shape:
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2

class Square(Shape):
    def __init__(self, side):
        self.side = side

    def area(self):
        return self.side ** 2

def print_area(shape):
    print(shape.area())

circle = Circle(5)
square = Square(4)

print_area(circle)
print_area(square)

Example 2: Polymorphism in Built-in Functions

print(len("codeswithpankaj.com"))  # String length
print(len([1, 2, 3, 4, 5]))        # List length
print(len({"a": 1, "b": 2, "c": 3}))  # Dictionary length

10. Summary

In this tutorial, we explored the concept of polymorphism in Python, its types, and how to implement it using method overloading, method overriding, operator overloading, and duck typing. We also provided practical examples to illustrate the application of polymorphism in Python. Polymorphism is a powerful feature that enhances code flexibility, readability, and maintainability.

For more tutorials and in-depth explanations, visit codeswithpankaj.com!


This tutorial provides a comprehensive overview of Python polymorphism, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!

Last updated