✦ Python Intermediate & OOP Collections module

Concept 03 · Polymorphism

One Call.
Different Results.

Book, EBook, and PrintedBook can now be handled together. How can one loop work with different kinds of books without checking their types?

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Different objects can respond to the same method call in their own way.

Book, EBook, and PrintedBook can now be handled together. How can one loop work with different kinds of books without checking their types?

class Book:
    def __init__(self, title): self.title = title
    def display_info(self): print(f"Book: {self.title}")

class EBook(Book):
    def display_info(self): print(f"EBook: {self.title}")

class PrintedBook(Book):
    def display_info(self): print(f"Printed book: {self.title}")

books = [Book("Dune"), EBook("Python Basics"), PrintedBook("The Hobbit")]
for book in books:
    book.display_info()

Every object has display_info(). The call is the same, but Python chooses the implementation from the object’s real type. The loop variable name does not choose the method. Inheritance gives EBook Book attributes and methods. Method overriding gives EBook its own display_info(). Polymorphism lets the same display_info() call work with each object and produce its own result.

Book: Dune EBook: Python Basics Printed book: The Hobbit

Try changing one value, then explain which part of the collection changed and why.

↳

Keep this in mind: Read the collection operation before predicting the result.

Try it
class Book:
    def __init__(self, title): self.title = title
    def display_info(self): print(f"Book: {self.title}")

class EBook(Book):
    def display_info(self): print(f"EBook: {self.title}")

class PrintedBook(Book):
    def display_info(self): print(f"Printed book: {self.title}")

books = [Book("Dune"), EBook("Python Basics"), PrintedBook("The Hobbit")]
for book in books:
    book.display_info()

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

polymorphism.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

class Book:
    def message(self): print("A book")
class EBook(Book):
    def message(self): print("A digital book")
items = [Book(), EBook(), Book()]
for item in items: item.message()
Choose the output

Practice independently

Mini Challenge — Add an AudioBook

Add an AudioBook and let the existing loop display it.

Your goalCreate an AudioBook class that inherits from Book and overrides display_info(). Add an AudioBook object to the existing books list without changing the for loop body.

add_audiobook.py

Optional hints
  • Define AudioBook(Book) and override display_info() to show its title.
  • Append AudioBook("Python Stories") before the existing loop; do not change the loop body.
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.