──────────────────────────────────────

25. Equipment Checkout · Model a Club Item

Create two objects and change one object’s state through a method.

Lesson files · lessons/models-and-state/objects-and-state

Before you start
- Store related values in a dictionary record.
- Define a function with an input and use a Boolean to represent a condition.

By the end
- Add a method that returns a borrowed item to available stock.
- Trace which object self refers to when comparing the camera and tripod results.

Where you can use it
Make a camera available again when it is returned while keeping the status of each other item.

A class describes the data and behaviour of a kind of object. The constructor starts each instance with its own attributes. A method receives that instance as self. Changing self.available should affect the item that received the call.

Predict
Which item remains available?
- The tripod.
- Both items.

Worked example (example.py)

class Item:
    def __init__(self, name):
        self.name = name
        self.available = True

    def borrow(self):
        self.available = False

camera = Item("Camera")
tripod = Item("Tripod")
camera.borrow()
print(camera.available)
print(tripod.available)


Example result
False
True

Add a give_back method. Borrow the camera, give it back, and print the availability of the camera and the tripod.

Starter program (starter.py)

class Item:
    def __init__(self, name):
        self.name = name
        self.available = True

    def borrow(self):
        self.available = False

camera = Item("Camera")
tripod = Item("Tripod")
camera.borrow()
print(camera.available)
print(tripod.available)


Hint
Use self.available = True inside give_back. Call it on camera before the prints.

Case 1. Try the sample
Input
(No input.)
Expected output
True
True

Stretch
Store a count of loans and see how it differs between the two items.

Reflect
Which value tells you that the method changed the right object?

Check your prediction
The tripod. The method operates on the camera instance that received the call.
