Zone Of Makos

Menu icon

Polymorphism in Python

Polymorphism is another key concept in object-oriented programming (OOP). In Python, polymorphism refers to the ability of different data types to be used interchangeably in the same context. This means that you can use different classes in the same way, even if they have different methods and properties. In this lesson, we'll explore how to use polymorphism in Python and how it can help you write more flexible and reusable code.

Polymorphism with Inheritance

One way to achieve polymorphism in Python is through inheritance. If you have a base class and multiple derived classes that inherit from it, you can use them interchangeably in the same context. For example, let's say you have a base class called Animal and two derived classes called Dog and Cat . Both Dog and Cat have a make_sound() method, but they make different sounds. You can use both of these classes interchangeably, like this:


class Animal:
    def make_sound(self):
        pass

class Dog(Animal):
    def make_sound(self):
        print("Woof!")

class Cat(Animal):
    def make_sound(self):
        print("Meow!")

def animal_sounds(animal):
    animal.make_sound()

dog = Dog()
cat = Cat()

animal_sounds(dog)
animal_sounds(cat)

In this example, the animal_sounds() function takes an object of any class that inherits from Animal and calls its make_sound() method. You can see that the function is called twice, once with an object of the Dog class and once with an object of the Cat class. The output of the program is "Woof!" followed by "Meow!", which shows that the different objects are being used interchangeably in the same context.

Polymorphism with Duck Typing

Another way to achieve polymorphism in Python is through duck typing. Duck typing is a concept in dynamic programming languages like Python that allows you to use any object that has the required methods and properties, regardless of its class. For example, let's say you have two classes called Car and Bicycle . Both classes have a drive() method, which allows you to move them forward. You can use both of these classes interchangeably, like this:


class Car:
    def drive(self):
        print("Driving the car.")

class Bicycle:
    def drive(self):
        print("Riding the bicycle.")

def ride(thing):
    thing.drive()

car = Car()
bike = Bicycle()

ride(car)
ride(bike)

In this example, the ride() function takes an object of any class that has a drive() method and calls that method. You can see that the function is called twice, once with an object of the Car class and once with an object of the Bicycle class. The output of the program is "Driving the car." followed by "Riding the bicycle.", which shows that the different objects are being used interchangeably in the same context.

Polymorphism with Function Overloading

Function overloading is a concept that is used in many programming languages to achieve polymorphism. In Python, however, function overloading is not supported in the same way as in other languages. Instead, you can use default arguments to simulate function overloading. For example, let's say you want to define a function called print_data() that can print data of different types, such as integers, strings, and lists. You can define the function like this:


def print_data(data, prefix="Data: "):
    print(prefix + str(data))

print_data(42)
print_data("Hello, world!", prefix="String: ")
print_data([1, 2, 3], prefix="List: ")

In this example, the print_data() function takes a mandatory argument called data and an optional argument called prefix . If the prefix argument is not specified, it defaults to "Data: ". You can see that the function is called three times with different arguments and different prefixes. The output of the program is:


Data: 42
String: Hello, world!
List: [1, 2, 3]

In this example, the print_data() function is used to print different types of data in the same context, which is an example of polymorphism.

Conclusion

That's it for this lesson on polymorphism in Python! As you can see, polymorphism is a powerful concept in object-oriented programming that can help you write more flexible and reusable code. In the next lesson, we'll explore another key concept in OOP: encapsulation.