Python Polymorphism

Let’s look at polymorphism

  1. Notice

    Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Bell:
    def sound(self):
        return "ring"
class Drum:
    def sound(self):
        return "boom"
instrument = Bell()
print(instrument.sound())
Live state

Top-level scope

Bell→Object 2
Drum→Object 4
instrument→Object 5
Object 2 · typeclass BellObject 4 · typeclass DrumObject 5 · BellBell({})
This step

Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

Picture polymorphism

Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

Image brief

Alt text
A diagram for Python Polymorphism. Instructions: class Bell: def sound(self): return "ring" class Drum: def sound(self): return "boom" instrument = Bell() print(instrument.sound()). Displayed result: ring. The final example bindings are Bell --> object 2 (type); Drum --> object 4 (type); instrument --> object 5 (Bell). object 2: class Bell; object 4: class Drum; object 5: Bell({})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Polymorphism". Teaching focus: Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Bell: def sound(self): return "ring" class Drum: def sound(self): return "boom" instrument = Bell() print(instrument.sound()) | v DISPLAYED RESULT ring FINAL BINDINGS Bell --> object 2 (type) Drum --> object 4 (type) instrument --> object 5 (Bell) object 2: class Bell object 4: class Drum object 5: Bell({}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Polymorphism. Instructions: class Bell: def sound(self): return "ring" class Drum: def sound(self): return "boom" instrument = Bell() print(instrument.sound()). Displayed result: ring. The final example bindings are Bell --> object 2 (type); Drum --> object 4 (type); instrument --> object 5 (Bell). object 2: class Bell; object 4: class Drum; object 5: Bell({})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Bell:
    def sound(self):
        return "ring"
class Drum:
    def sound(self):
        return "boom"
instrument = Bell()
print(instrument.sound())
Live state

Top-level scope

Bell→Object 2
Drum→Object 4
instrument→Object 5
Object 2 · typeclass BellObject 4 · typeclass DrumObject 5 · BellBell({})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
ring

What do you expect this program to print?

What does this example display?

class Bell:
    def sound(self):
        return "ring"
class Drum:
    def sound(self):
        return "boom"
instrument = Bell()
print(instrument.sound())
What does this example display?
Choose one answer. You can try again.

Answer and explanation

ring

Follow the printed values in order. The output is: ring

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

What is Polymorphism?

Polymorphism means “many forms.”

It’s a concept where something (like a function or method) behaves differently based on the object it is working with.

Imagine this: A person can be a student, a player, and a singer. Same person — but different roles. That’s the idea behind polymorphism.

Simple Example 1: len() Function

Python has a built-in function len() that gives you the length of something.

print(len("hello"))       # Length of a string
print(len([1, 2, 3, 4]))  # Length of a list
print(len((10, 20)))      # Length of a tuple
5
4
2

Why This Output?

  • "hello" has 5 letters → len("hello") is 5
  • [1, 2, 3, 4] has 4 items → len([1, 2, 3, 4]) is 4
  • (10, 20) has 2 items → len((10, 20)) is 2

The same function (len()) gives correct results for different types — that’s polymorphism!

Simple Example 2: Polymorphism with Classes

Let’s look at how polymorphism works with objects from different classes.

class Dog:
    def speak(self):
        return "Woof!"

class Cat:
    def speak(self):
        return "Meow!"

# Using polymorphism
animals = [Dog(), Cat()]
for animal in animals:
    print(animal.speak())
Woof!
Meow!

Why This Output?

  • Both Dog and Cat have a method called speak().
  • When we call animal.speak(), Python knows which version to use based on the object.
  • Dog says Woof! and Cat says Meow!.

This is polymorphism — speak() behaves differently for different objects.

Polymorphism in Daily Life

Here are some fun examples:

  • You can draw with a pencil, pen, or crayon. Same action, different tools → different results.
  • You can drive a car, a bike, or a truck. Same action, different vehicles → different behavior.

Why Learn Polymorphism?

  • It makes your code more flexible and reusable.
  • You can write one function or method and use it for many types of data.

Conclusion

Polymorphism allows the same action to work differently depending on the object.

What to remember

Connect what you noticed in the example with the idea below.

Remember

Different objects can support the same operation. A caller can use that shared operation without checking each object’s concrete class.

Watch out for this idea

Objects must have the same class to support the same operation.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python List Comprehension →