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

Book and Bookcase Relationship

How is it that in creating an instance using the classmethod create_bc that the title and author are passed to the Book class?

And we are appending a title and author to the books list. Would books = [title1, author1, title2, author2] in this case?

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

    def __str__(self):
        return '{} by {}'.format(self.title, self.author)

class Bookcase:
    def __init__(self, books = None):
        self.books = books

    @classmethod
    def create_bc(cls, book_list):
        books = []
        for title, author in book_list:
            books.append(Book(title, author))
        return cls(books)

book1 = Bookcase.create_bc([("title1", 'author1'), ('title2', 'author2')]) print(book1.books[0])

1 Answer

Steven Parker
Steven Parker
231,198 Points

The "create_bc" method doesn't just add strings to a list, it adds Book objects to a list that is an attribute of a "Bookcase" instance. So:

book1 = [title1, author1, title2, author2]          # this is NOT what the example does
book1 = Bookcase([Book("title1", "author1"), Book("title2", "author2")])  # but this is