✦ Python Intermediate & OOP Collections module

Concept 05 · Composition

The Books Are Ready.
Let's Build a Library!

Should Library inherit from Book, or should it contain Book objects?

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.

One library. Different books. One working system.

Should Library inherit from Book, or should it contain Book objects?

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}")
class AudioBook(Book):
    def display_info(self): print(f"Audio book: {self.title}")
class Library:
    def __init__(self): self.books=[]
    def add_book(self, book): self.books.append(book)
    def show_books(self):
        for book in self.books: book.display_info()
    def __len__(self): return len(self.books)
library=Library()
for book in (Book("Dune"),EBook("Python Basics"),PrintedBook("The Hobbit"),AudioBook("Python Stories")): library.add_book(book)
library.show_books()
print(f"Total books: {len(library)}")

Inheritance — IS-A: EBook is a type of Book. That is why EBook inherits from Book. Composition — HAS-A: Library is not a type of Book. A Library has books and stores their objects in self.books. Objects working together: Library uses the objects it contains. When show_books() runs, each book uses its own display_info() method. The __len__() method lets us count the books with len(library).

Book: Dune EBook: Python Basics Printed book: The Hobbit Audio book: Python Stories Total books: 4

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}")
class AudioBook(Book):
    def display_info(self): print(f"Audio book: {self.title}")
class Library:
    def __init__(self): self.books=[]
    def add_book(self, book): self.books.append(book)
    def show_books(self):
        for book in self.books: book.display_info()
    def __len__(self): return len(self.books)
library=Library()
for book in (Book("Dune"),EBook("Python Basics"),PrintedBook("The Hobbit"),AudioBook("Python Stories")): library.add_book(book)
library.show_books()
print(f"Total books: {len(library)}")

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.

composition.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 Library:
    def __init__(self): self.books=[]
    def add_book(self, book): self.books.append(book)
    def __len__(self): return len(self.books)
library=Library(); library.add_book("Dune"); library.add_book("The Hobbit")
print(len(library)); library.add_book("Python Basics"); print(len(library))
Choose the output

Practice independently

Mini Challenge — Find a Book

Add find_book() to search a Library collection.

Your goalLoop through self.books, return a matching Book object, or return None when it is missing.

find_book.py

Optional hints
  • Compare book.title with title inside a for loop.
  • Return the book when it matches; return None after the loop.
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.