Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Constructicons

I don't understand how the Book and Bookcase classes work together

It seems like you could just construct the bookcase without defining the book class? I'm not sure I understand why the Book class had to be defined at all.

2 Answers

Steven Parker
Steven Parker
231,268 Points

Perhaps you have a different purpose in mind for the Bookcase? But the one in the video example is specifically designed to hold a collection of Book objects. It even has a method ("create_bookcase") to create the Book objects and store them in the collection.

Anthony Grodowski
Anthony Grodowski
4,902 Points

Steven Parker but how does class Book pass these books to class Bookcase? I mean I see the for title, author in book_list: books.append(Book(title, author)) chunk of code but how class Bookcase knows that these informations come from class Book? It doesn's seem like we're importing class Book into class Bookcase. I also don't get why do we need books and book_list in class Bookcase.

Another thing that confuses me is that we're using and instance object in a classmethod (books). We've set books variable that it's an instance object and then we're using that in a classmethod. Could you please explain whole class Bookcase to me?

Steven Parker
Steven Parker
231,268 Points

That bit of code you quoted: specifically creates book objects :point_right: books.append(Book(title, author)).

So there's no question that it contains book objects. Now the information it uses to create them is not already in book objects. That's what "book_list" is all about. It's just a list of titles and authors used for creating the books.

If you have more to ask, it might be better to start a fresh new question.

Anthony Grodowski
Anthony Grodowski
4,902 Points

Thanks Steven Parker ! I think you didn’t get my point. I’m wondering HOW does Bookcase class know that it’s from Book class. I mean what if Book class was in a another file? Than I think we would need to provide special information to Bookcase from where pick Book instances. Am I right?

Steven Parker
Steven Parker
231,268 Points

Since Bookcase depends on Book, it would not build without it. If Book was defined in another file, then it would be necessary to import it.

can you please explain in detail if you don"t mine Steven Parker thanks in advance

Steven Parker
Steven Parker
231,268 Points

The relationship between "Book" objects and "Bookcase" is pretty simple. The "Book" just holds two strings, one each for Title and Author, and the "Bookcase" holds a collection of "Book" objects.. It sounds like you may have a bit different question from Kia's.

You might want to ask it as a new question, which would allow it to be seen by more students.