🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Polymorphism

AI & DATA SCIENCE // polymorphism

Polymorphism means objects of different classes can be used interchangeably through a shared interface, with each class providing its own specific behavior for the same method call.

Syntax

for shape in shapes:
    print(shape.area())  # each shape computes area differently

Deep Dive Course

In Python, polymorphism most often shows up as several classes defining a method with the same name and signature but different implementations — calling that method on any of them, without checking which specific class it is, runs the correct version automatically. Python's dynamic typing makes this especially natural: since there's no strict interface declaration required, any object with the right method just works, a style often called 'duck typing', whether or not it shares a common base class.

1Understanding Polymorphism

In Python, polymorphism most often shows up as several classes defining a method with the same name and signature but different implementations — calling that method on any of them, without checking which specific class it is, runs the correct version automatically. Python's dynamic typing makes this especially natural: since there's no strict interface declaration required, any object with the right method just works, a style often called 'duck typing', whether or not it shares a common base class.

💡

You don't need a shared base class or formal interface in Python to get polymorphic behavior — duck typing means any two unrelated classes that both define the same method name can be used interchangeably by code that just calls it.

editor.html
class Circle:
    def area(self):
        return 3.14 * 5 ** 2

class Square:
    def area(self):
        return 4 ** 2

for shape in [Circle(), Square()]:
    print(shape.area())
localhost:3000

2Practical Example

Here is a real-world application of Polymorphism showing how it is used in production Python code.

editor.html
class Duck:
    def make_sound(self):
        return "Quack"

class Person:
    def make_sound(self):
        return "I'm imitating a duck!"

for entity in [Duck(), Person()]:
    print(entity.make_sound())
localhost:3000

3Best Practices

Follow these guidelines when working with Polymorphism:

1. Design methods with consistent names and signatures across related classes so calling code can treat them interchangeably

2. Rely on duck typing rather than checking types explicitly (isinstance chains) when different objects just need to support the same method call

3. Use an abstract base class (via the abc module) when you want to formally require and document a shared interface across implementations

⚠️

Tip: You don't need a shared base class or formal interface in Python to get polymorphic behavior — duck typing means any two unrelated classes that both define the same method name can be used interchangeably by code that just calls it.

editor.html
class Circle:
    def area(self):
        return 3.14 * 5 ** 2

class Square:
    def area(self):
        return 4 ** 2

for shape in [Circle(), Square()]:
    print(shape.area())
localhost:3000

Examples

Example 01Basic Usage
class Circle:
    def area(self):
        return 3.14 * 5 ** 2

class Square:
    def area(self):
        return 4 ** 2

for shape in [Circle(), Square()]:
    print(shape.area())
Example 02Advanced Example
class Duck:
    def make_sound(self):
        return "Quack"

class Person:
    def make_sound(self):
        return "I'm imitating a duck!"

for entity in [Duck(), Person()]:
    print(entity.make_sound())

Best Practices

  • Design methods with consistent names and signatures across related classes so calling code can treat them interchangeably
  • Rely on duck typing rather than checking types explicitly (isinstance chains) when different objects just need to support the same method call
  • Use an abstract base class (via the abc module) when you want to formally require and document a shared interface across implementations

Interview Question

What is 'duck typing', and how does it relate to polymorphism in Python?

Hint: Think about what Python actually checks before calling a method.

Duck typing means Python doesn't check an object's actual class before calling a method on it — it only checks, at the moment of the call, whether that object happens to have a method with the right name. 'If it walks like a duck and quacks like a duck, it's a duck' as far as the calling code is concerned. This is what makes polymorphism so lightweight in Python: any two unrelated classes that both implement the same method name can be used interchangeably by code that calls it, with no shared base class or formal interface required.

Exercises

MediumPractice using Polymorphism in a real scenario.
View Solution
class Circle:
    def area(self):
        return 3.14 * 5 ** 2

class Square:
    def area(self):
        return 4 ** 2

for shape in [Circle(), Square()]:
    print(shape.area())

Frequently Asked Questions

What is 'duck typing', and how does it relate to polymorphism in Python?

Duck typing means Python doesn't check an object's actual class before calling a method on it — it only checks, at the moment of the call, whether that object happens to have a method with the right name. 'If it walks like a duck and quacks like a duck, it's a duck' as far as the calling code is concerned. This is what makes polymorphism so lightweight in Python: any two unrelated classes that both implement the same method name can be used interchangeably by code that calls it, with no shared base class or formal interface required.

Related Functions

inheritancedunder-methodsisinstance()