✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 04 · OOP Fundamentals

Let each book act.
Keep state with the object.

Mini Library Manager now gives Book actions for borrowing, returning, and showing its own information.

Learning goalDefine and call instance methods that use self to read or change one Book object’s attributes.
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.

Put book behavior beside book data.

Attributes describe a book’s state. Methods describe what that particular book can do. Each method receives self first, so it can read or update the attributes of the instance that called it.

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

    def borrow(self):
        self.available = False

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

Calling dune.borrow() makes Python pass dune as self. The method changes dune.available, not the availability of every book.

False

A second instance calls the same method definition with a different self. That is why one book can be borrowed while another stays available.

↳

Read self as “this book”: Inside borrow, self.available means the available attribute of the Book instance before the dot in dune.borrow().

Borrow one book, keep one available
class Book:
    def __init__(self, title, author, available=True):
        self.title = title
        self.author = author
        self.available = available

    def borrow(self):
        self.available = False

    def return_book(self):
        self.available = True

    def display_info(self):
        print(f"{self.title} by {self.author}: {self.available}")

dune = Book("Dune", "Frank Herbert")
hobbit = Book("The Hobbit", "J. R. R. Tolkien")
dune.borrow()
dune.display_info()
hobbit.display_info()

dune.borrow() changes only dune. hobbit has its own available attribute, which remains True.

Three useful Book methods
def borrow(self):
    self.available = False

def return_book(self):
    self.available = True

def display_info(self):
    print(self.title)

borrow and return_book change state. display_info reads attributes and shows information without changing the book.

Trace a method call

  1. 1. Define behavior in the classIndent def borrow(self) inside Book so every Book instance can use it.
  2. 2. Call it with a dotdune.borrow() selects the borrow behavior for the dune instance.
  3. 3. Python supplies selfPython passes dune as self, so self.available refers to dune.available.
  4. 4. Check another instancehobbit.available is independent because hobbit is a different object.

Common mistakes

  • Leaving out selfAn instance method needs self first to receive the object that called it.
  • Writing available instead of self.availableUse self.available to change the attribute stored on the instance.
  • Calling Book.borrow() without an objectUse dune.borrow() so Python knows which Book should be borrowed.
  • Expecting one method call to change every instanceA method changes only the object passed as self unless it explicitly works with other objects.

Python Playground

Loading Python runtime…

Borrow one book, return it, and use display_info() to see that the other book keeps its own state.

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

Follow the self passed by each method call. dune is borrowed; hobbit is never changed.

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

    def borrow(self):
        self.available = False

    def return_book(self):
        self.available = True

    def display_info(self):
        print(f"{self.title} by {self.author}: {self.available}")

dune = Book("Dune", "Frank Herbert")
hobbit = Book("The Hobbit", "J. R. R. Tolkien")
dune.borrow()
dune.display_info()
hobbit.display_info()
Choose the output

Practice independently

Mini Challenge — Return a book

Add a method that changes the state of one borrowed book back to available.

Your goalDefine return_book(self) to set self.available to True, then call it on one Book instance and print the result.

return_book_challenge.py

Optional hints
  • Define the method inside the class with def return_book(self).
  • Inside it, assign self.available = True, then call borrowed_book.return_book().
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.