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!

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 do not understand how we access to __str__ method of the Book class

we create from @classmethod a booklist function or method? Asking for the argument book_list. And then we assign the book_ list argument to the book empty list. Finally, we return the result as an instance of the class Bookcase. The part that I don't understand is how we can use the str method from the class Book if we created an instance from a different class, Bookcase.

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 is a decoderate
    @classmethod
    def create_bookcase(cls, book_list):
        books = []
        for title, author in book_list:
            # books append into the class Book with the two arguments title, author
            books.append(Book(title, author))
        # returns a instance of the class Bookcase
        # also we pass the list of book to class that we just created
        return cls(books)
Ahsan Lake
Ahsan Lake
7,405 Points

Each member of the books list is a book object. The book object str method is overridden to display "{} by {}".format(self.title, self.author) instead of the default object str method.

No matter where the object was created, it's str method will be overridden to do that.

2 Answers

Eloy Pires
Eloy Pires
2,806 Points

because when the Bookcase was created he put 2 books (instances of Book class in that), so when str method was call, it was a str call for Book object who was in Bookcase object.

def create_bookcase(cls, book_list):
        books = []
        for title, author in book_list:
            # books append into the class Book with the two arguments title, author
            books.append(Book(title, author))
        # returns a instance of the class Bookcase
        # also we pass the list of book to class that we just created
        return cls(books)

Thank you Eloy Pires , Literally when we create a Bookcase @classmethod , we are creating two instances of Book class, and then we appended and returned those two instances in of a list. Then, we initialize(create) an instance of the Bookcase, without even touch the init method of Bookcase. Am I correct?

Eloy Pires
Eloy Pires
2,806 Points

i am not sure, but i think the init is called when the return calls cls(books).

what i understand is:

  • Bookcase class (with init) create a list of Books (objects) when receive same number of Books (objects). So you have to convert title and author in a book yourself.
  • the @classmethod create_bookcase receive a list of titles and authors then convert they in Book (object), after that calls a Bookcasa (cls who was given) with a list of Books and finaly returns this instance.

i hope you can understand what i mean.

Thank again for the answer, Eloy Pires .