---
title: "Explain the Python Polymorphism with example."  
description: "Explain the Python Polymorphism with example."  
author: "ICSM Computer"  
published: 2025-10-05  
updated: 2025-10-07  
canonical: https://www.mindstick.com/forum/161936/explain-the-python-polymorphism-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 3 minutes  

---

# Explain the Python Polymorphism with example.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [Polymorphism](https://www.mindstick.com/articles/1827/objective-c-polymorphism) with example.**

## Replies

### Reply by ICSM Computer

> ## What is Polymorphism?
>
> **Polymorphism** means *“many forms”*.\
> In programming, **polymorphism allows the same function or method name to behave differently** depending on the object that calls it.

In Python, polymorphism is mainly seen with:

- **Built-in functions** that work with different types.
- **Class methods** that are overridden in subclasses.
- **Duck typing** — “If it walks like a duck and quacks like a duck, it’s a duck.”

## Example 1: Built-in Polymorphism

Some Python functions work differently depending on the data type.

```python
# len() works with multiple types
print(len("Hello"))     # 5  → length of string
print(len([10, 20, 30]))  # 3  → length of list
print(len({"a": 1, "b": 2}))  # 2  → number of keys in dict
```

**Same function name (**`len`**)**, different behavior depending on the data type.

## Example 2: Polymorphism with Class Methods (Method Overriding)

When a **child class overrides a method** from its parent, both have the same name but different implementations.

```python
class Bird:
    def speak(self):
        return "Some generic bird sound"

class Parrot(Bird):
    def speak(self):
        return "Squawk!"

class Crow(Bird):
    def speak(self):
        return "Caw!"

# Using polymorphism
for bird in [Parrot(), Crow(), Bird()]:
    print(bird.speak())
```

## Output:

```plaintext
Squawk!
Caw!
Some generic bird sound
```

Here, the same method name `speak()` behaves differently depending on the object’s class — that’s polymorphism.

## Example 3: Polymorphism with a Common Interface

Multiple classes can implement the same method name, even if they’re unrelated.

```python
class Cat:
    def make_sound(self):
        return "Meow"

class Dog:
    def make_sound(self):
        return "Woof"

class Cow:
    def make_sound(self):
        return "Moo"

# Using polymorphism
animals = [Cat(), Dog(), Cow()]

for animal in animals:
    print(animal.make_sound())
```

## Output:

```plaintext
Meow
Woof
Moo
```

Python doesn’t care what the object *is*, as long as it has a `make_sound()` method — this is called **duck typing**:

> “If it quacks like a duck, it’s a duck.”

## Example 4: Polymorphism with Inheritance and `super()`

```python
class Shape:
    def area(self):
        return 0

class Rectangle(Shape):
    def __init__(self, w, h):
        self.w = w
        self.h = h

    def area(self):
        return self.w * self.h

class Circle(Shape):
    def __init__(self, r):
        self.r = r

    def area(self):
        from math import pi
        return pi * self.r ** 2

shapes = [Rectangle(5, 4), Circle(3)]

for shape in shapes:
    print(f"Area: {shape.area():.2f}")
```

## Output:

```plaintext
Area: 20.00
Area: 28.27
```

Both shapes use the same `area()` method — but behave differently.

## Summary

| Concept | Description |
| --- | --- |
| **Definition** | One interface, many implementations |
| **Common in** | Method overriding, built-in functions, duck typing |
| **Key benefit** | Code flexibility and reusability |
| **Example use** | Functions that operate on different object types with the same method names |


---

Original Source: https://www.mindstick.com/forum/161936/explain-the-python-polymorphism-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
