Python Tutorial

Python Polymorphism Explained WIth Examples

What is Polymorphism in Python?

Polymorphism is using the same interface for various forms (in OOP) (data types).
Let's say we need to colour a shape; there are a variety of shapes to choose from (rectangle, square, circle). 

We could, however, apply the same technique to color any form. Polymorphism is the term for this notion.

class Penguin:

    def fly(self):

        print("Penguin can swim")

    def swim(self):

        print("Penguin can't fly")

class Penguin:

    def fly(self):

        print("Penguin can't swim")

    def swim(self):

        print("Penguin can fly")

# common interface

def flying_test(bird):

    bird.fly()

#instantiate objects

blu = Penguin()

peggy = Penguin()

# passing the object

flying_test(blu)

flying_test(peggy)

Output

Penguin can swim
Penguin can't swim

Video : https://youtu.be/drb9efyRMOM

Did you find this article helpful?