Objects working together
Last updated
A library isn't one book. It's books, members and loans, all tied together. In this lesson you'll give a loan a book and a member as attributes, give each member a list of books, write methods that ask other objects to do their part, pass objects to functions and get them back, and read the error you get when a class is stored where an object should be.
By the end of this lesson you can
- Give an object attributes that hold other objects, and reach through them with dots
- Keep a list of objects inside another object
- Write methods that call other objects' methods, so each object looks after its own data
- Pass objects to functions and return them, knowing both sides share one object
An object as an attribute
A loan is a book lent to a member for some days. So a Loan object holds a Book object and a Member object.
An attribute can hold any object, including one made from your own class. self.book = book stores the book that was passed in.
To reach inside, use dot after dot: loan.book is the book, and loan.book.title is its title.
Lesson 3.1 said a name is a label on an object. An attribute is a label too. loan.book and dune are two labels on one book, which is what line 23 checks. So when line 24 renames the book, the loan sees the new title. Watch it run, and see loan and dune change together.
That's what you want here: there's one copy of Dune in the building, and the loan points at it.
A list of objects
A member can have many loans, so a Member keeps a list of books. __init__ gives every new member their own empty list, self.loans = [].
sam.loans is a list, so sam.loans.append(...) works like any list. Each item in it is a Book, so line 18 can read book.title from each one.
Ana's list is empty: she has her own list, separate from Sam's, because __init__ made a new one for each member.
Methods that call other objects' methods
When Sam borrows Dune, two things must happen. The book loses a copy from the shelf, and Sam's list gains the book.
Each object should look after its own data. So Member.borrow doesn't change book.copies itself. It asks the book to do it, with book.lend(), and then adds the book to the member's own list. If there's no copy left, lend raises a ValueError (lesson 2.6). It's raised before the book is added to the list, so a failed loan changes nothing.
Watch it run and follow line 32. It jumps into Member.borrow, then into Book.lend, and back out.
The rule "no lending with zero copies" lives in Book, the class that owns copies. Any code that lends a book goes through it. Stage 4 calls this idea encapsulation.
Objects as arguments and return values
Objects can be passed to functions and given back, like any value. find takes a list of books and some words, and it returns the first book whose title contains them, or None if none do.
The book find gives back isn't a copy. It's the same object that's in catalogue. So when line 23 changes its author, catalogue[1] shows the change.
Change line 20 to look for "zzz". find gives back None, and line 21 stops with an AttributeError, because None has no title. Code that calls find must check for None first: if book is None:.
The class instead of an object
Line 13 should lend Dune, but something's off. Run it.
type object 'Book' has no attribute 'title'. Line 13 passed Book, the class, where it meant dune, a book made from the class. Python accepted it: it has no way to know what Loan expects. The mistake only shows up on line 14, when something reads loan.book.title. A class has no title. Each book gets its own in __init__.
Change Book to dune on line 13 and run it again. When you see type object in an error, look for a class name with no brackets after it.
In other languages
This step is optional. It's for anyone who will meet Java or Rust later.
| Python | Java | Rust | |
|---|---|---|---|
| An object in an attribute | The attribute is a label on the other object. Both can be reached and changed | The field holds a reference, as in Python | The struct owns the value inside it. Sharing it takes a borrow or a smart pointer |
| Asking another object to act | book.lend() | book.lend() | book.lend(), on a &mut Book |
| Passing an object to a function | The function gets a label on the same object | The method gets a reference to the same object | The value moves in, or is lent with & or &mut |
| A class where an object should be | Allowed. Fails when an attribute is read | Won’t compile | Won’t compile |
Building an object out of other objects is called composition. Rust leans on it in place of inheritance: see Simple Rust 4.1. Python can do both, and lesson 4.15 compares them.
Check what you learned
Three questions. Pick or type an answer, then check it.
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- The Python Tutorial: Instance Objects
- The Python Tutorial: Class and Instance Variables: each object has its own attributes, set in __init__
- The Python Language Reference: Objects, values and types: objects that hold references to other objects
- Built-in Types: Mutable Sequence Types: append, and remove, which finds the item with ==
- Built-in Exceptions: AttributeError
- The Java Tutorials: Objects: Java objects that refer to other objects, and calling their methods
- The Rust Programming Language: Method Syntax: Rust methods that take other values