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

Kohei Ashida
Kohei Ashida
4,882 Points

Why don't you need to import Book class?

In this video, why did Kenneth only import Bookcase class not with Book class as well? I couldn't understand why this command worked without importing Book class.

>>> from books import Bookcase                                                                                                                                            
>>> bc = Bookcase.create_bookcase([('Moby-Dick', 'Herman Melville'), ('Jungle Book', 'Rudyard Kipling')])                                                                 
>>> str(bc.books[0])                                                                                                                                                      
'Moby-Dick by Herman Melville'   

Is that because the Book class is called in the create_bookcase function below?

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

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

Thanks!

2 Answers

Steven Parker
Steven Parker
229,732 Points

You're exactly right, "Bookcase" is imported with its dependencies are resolved.

However, you'd need a separate "import" if you wanted to reference "Book" directly.