✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 03 · OOP Fundamentals

Set up each book.
Keep its state clear.

Mini Library Manager now gives every new Book complete starting data, including whether it is available.

Learning goalInitialize Book attributes with required and default arguments, then update an attribute on one object.
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.

Give a new book its starting state.

You already used __init__ to receive a title and author. A constructor can also set a default value. Here, every new book is available unless the caller says otherwise.

class Book:
    def __init__(self, title, author, available=True):
        self.title = title
        self.author = author
        self.available = available

dune = Book("Dune", "Frank Herbert")
print(dune.available)

title and author are required arguments, so every Book call must provide them. available=True is a default argument, so it is used when no third value is supplied.

True

After creation, an attribute can change. Assigning dune.available = False updates only the dune object.

↳

Constructor checklist: Use self first, receive the values you need, and save them with self.attribute. Defaults belong in the parameter list after required arguments.

Two books, different availability
class Book:
    def __init__(self, title, author, available=True):
        self.title = title
        self.author = author
        self.available = available

dune = Book("Dune", "Frank Herbert")
foundation = Book("Foundation", "Isaac Asimov", False)
dune.available = False

print(dune.available)
print(foundation.available)

dune starts with the default True and is then changed to False. foundation receives False directly when it is created.

Required versus default arguments
Book("Dune", "Frank Herbert")
# title and author are required

Book("Foundation", "Isaac Asimov", False)
# available overrides its default

Required arguments describe information every book must have. A default makes optional state convenient while keeping a predictable starting value.

Build a reliable constructor

  1. 1. Receive required dataPut title and author in __init__ so a Book cannot be created without them.
  2. 2. Add optional statePlace available=True after required parameters to give new books a default availability.
  3. 3. Save on selfAssign self.available = available so the value belongs to this instance.
  4. 4. Update one bookUse book.available = False when that particular book is borrowed.

Common mistakes

  • Putting a default before required dataRequired parameters must come before default parameters in a function definition.
  • Forgetting to save a parameterReceiving available is not enough; assign it to self.available to store it on the object.
  • Changing the class instead of the objectWrite dune.available = False to change one book’s state.
  • Expecting a default to overwrite a supplied valueA default is used only when the caller leaves that argument out.

Python Playground

Loading Python runtime…

Create another Book with the default availability, then mark one book as borrowed.

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

Track the default value first, then the later assignment to dune only.

class Book:
    def __init__(self, title, author, available=True):
        self.title = title
        self.author = author
        self.available = available

dune = Book("Dune", "Frank Herbert")
foundation = Book("Foundation", "Isaac Asimov", False)
dune.available = False

print(dune.available)
print(foundation.available)
Choose the output

Practice independently

Mini Challenge — Add book status

Extend the library model with a default availability attribute and update one book after it is created.

Your goalCreate a Book with title, author, and available=True by default. Make one book unavailable and print its title and availability.

book_status_challenge.py

Optional hints
  • Save the default argument with self.available = available.
  • After Book(...) returns, use new_book.available = False to update only that object.
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.