Python Tutorial for Beginners

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):

#instantiate objects

blu = Penguin()

peggy = Penguin()

# passing the object




Penguin can swim
Penguin can't swim

Video :

Did you find this article helpful?