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 trialKia Matthews
3,402 PointsI 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
231,268 PointsPerhaps 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.
sathish
5,697 Pointscan you please explain in detail if you don"t mine Steven Parker thanks in advance
Steven Parker
231,268 PointsThe 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.
Anthony Grodowski
4,902 PointsAnthony Grodowski
4,902 PointsSteven Parker but how does
class Book
pass these books toclass Bookcase
? I mean I see thefor title, author in book_list: books.append(Book(title, author))
chunk of code but howclass Bookcase
knows that these informations come fromclass Book
? It doesn's seem like we're importingclass Book
intoclass Bookcase
. I also don't get why do we needbooks
andbook_list
inclass 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
231,268 PointsSteven Parker
231,268 PointsThat bit of code you quoted: specifically creates book objects
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
4,902 PointsAnthony Grodowski
4,902 PointsThanks 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
231,268 PointsSteven Parker
231,268 PointsSince 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.