✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 05 · OOP Fundamentals

Share the library.
Keep each book unique.

Mini Library Manager now separates information shared by every Book from information owned by one book.

Learning goalDistinguish class attributes from instance attributes, update shared state, and recognize when one instance shadows a class attribute.
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, many individual books.

Put a class attribute directly inside Book to share it. Put an instance attribute on self in __init__ to give each book its own value.

class Book:
    library_name = "Mini Library"

    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_available = True

book = Book("Dune", "Frank Herbert")
print(Book.library_name)
print(book.title)

library_name belongs to the Book class and is shared. title belongs to book, because self.title creates an attribute on that one instance.

Mini Library Dune

Changing Book.library_name updates the shared lookup for instances that do not have their own library_name. Assigning book.library_name creates a separate instance attribute that shadows the class attribute.

↳

Attribute lookup order: When Python reads book.library_name, it checks the book first and then the Book class. An instance value therefore hides the shared value for that one book.

Shared value and one shadowed value
class Book:
    library_name = "Mini Library"

    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_available = True

book_one = Book("Dune", "Frank Herbert")
book_two = Book("The Hobbit", "J. R. R. Tolkien")

Book.library_name = "City Library"
book_one.library_name = "Personal Shelf"

print(book_one.library_name)
print(book_two.library_name)

Book.library_name changes the shared name to City Library. book_one then gets its own Personal Shelf value, while book_two still uses the class value.

Where attributes belong
class Book:
    library_name = "Mini Library"  # class attribute

    def __init__(self, title):
        self.title = title            # instance attribute

Use a class attribute for one value shared by all books. Use an instance attribute for data that can differ from book to book.

Trace an attribute lookup

  1. 1. Read the instance firstbook_one.library_name first looks for library_name on book_one.
  2. 2. Fall back to the classWithout an instance value, Python reads Book.library_name.
  3. 3. Change shared stateBook.library_name = "City Library" changes the value used by unshadowed instances.
  4. 4. Shadow one instancebook_one.library_name = "Personal Shelf" affects only book_one.

Common mistakes

  • Using a shared list for per-book dataA mutable class attribute such as tags = [] is shared by every instance. Create a new list on self in __init__ when each book needs its own tags.
  • Expecting an instance assignment to update all booksbook_one.library_name creates or changes book_one’s value only; it does not change Book.library_name.
  • Calling every attribute sharedtitle, author, and is_available are instance attributes because they are assigned through self.
  • Forgetting the class name for a shared updateUse Book.library_name to deliberately update the value shared by the class.

Python Playground

Loading Python runtime…

Change the shared library name, then give one book its own name and compare both books.

library_attributes.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?

First follow the class update, then notice that book_one receives its own attribute.

class Book:
    library_name = "Mini Library"

    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_available = True

book_one = Book("Dune", "Frank Herbert")
book_two = Book("The Hobbit", "J. R. R. Tolkien")

Book.library_name = "City Library"
book_one.library_name = "Personal Shelf"

print(book_one.library_name)
print(book_two.library_name)
Choose the output

Practice independently

Mini Challenge — Label the library

Use one class attribute for the library name and instance attributes for a new book’s data.

Your goalDefine Book.library_name, create two books with their own titles, change the shared name, and give one book a personal label.

library_labels_challenge.py

Optional hints
  • Write library_name = "Mini Library" directly in the class, outside __init__.
  • Use Book.library_name for the shared update and book_one.library_name for the one-book label.
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.