Python Polymorphism

Polymorphism is a fundamental concept in Python, allowing programmers to use a single interface for different data types or classes. Understanding polymorphism is key to writing flexible and efficient code. In this guide, we will explore various aspects of polymorphism in Python, complete with examples to illustrate each concept.

1. Function Polymorphism

Function polymorphism in Python refers to the ability to use a single function name to perform different tasks, depending on the context or the data type of the arguments passed. This is often achieved through the use of conditional statements or the inherent polymorphic nature of Python functions.

Example:

def multiply(a, b):
    return a * b

print(multiply(5, 3))     # Outputs: 15 (integer multiplication)
print(multiply('a', 3))   # Outputs: aaa (string repetition)

In this example, the multiply function works for both integers and strings, demonstrating function polymorphism.

2. Tuple

Tuples in Python are a simple yet powerful example of polymorphism. They can store multiple items of different data types in a single tuple object.

Example:

my_tuple = (1, "Hello", 3.14)
print(my_tuple)

This tuple contains an integer, a string, and a float, showcasing Python’s ability to handle different data types polymorphically.

3. Dictionary

Dictionaries in Python are inherently polymorphic. They can hold various data types as values and use immutable types for keys.

Example:

my_dict = {'id': 101, 'name': 'John', 'is_student': True}
print(my_dict)

Here, the dictionary contains an integer, a string, and a boolean, illustrating polymorphism in data storage.

4. Class Polymorphism

Class polymorphism in Python is achieved when a class inherits from another class. This allows the inherited class to modify or extend the functionalities of the parent class.

Example:

class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return "Bark!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

def animal_sound(animal):
    print(animal.speak())

dog = Dog()
cat = Cat()

animal_sound(dog)  # Outputs: Bark!
animal_sound(cat)  # Outputs: Meow!

In this example, both Dog and Cat classes override the speak method of their parent class Animal, demonstrating class polymorphism.

5. Inheritance Class Polymorphism

Inheritance class polymorphism is when a subclass inherits from a base class but changes its behavior. This is a cornerstone of object-oriented programming in Python.

Example:

class Vehicle:
    def type(self):
        pass

class Car(Vehicle):
    def type(self):
        return "Car"

class Truck(Vehicle):
    def type(self):
        return "Truck"

def vehicle_type(vehicle):
    print(vehicle.type())

car = Car()
truck = Truck()

vehicle_type(car)   # Outputs: Car
vehicle_type(truck) # Outputs: Truck

In this case, Car and Truck subclasses redefine the type method from the Vehicle class, showcasing inheritance class polymorphism.

Understanding and implementing polymorphism in Python enhances your programming skills and allows for more dynamic and flexible code. By mastering these concepts, you are taking a significant step forward in your Python programming journey.