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 Basic Object-Oriented Python Emulating Built-ins Emulating Built-ins

Anthony Costanza
Anthony Costanza
2,123 Points

Not sure how to even find the answer to this...

Not sure how to even find the answer to this...

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

    def __iter__(Bookcase)

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

    def __eq__(self, other):

        return self.author == other.author and self.title == other.title
bookcase.py
from book import Book


class BookCase:
    def __init__(self):
       self.books = []

    def add_books(self, book):
        self.books.append(book)

2 Answers

Heya Anthony Costanza, Since we are wanting to iterate through the list that is located in the BookCase class, we want to add the __iter__(self): method inside of that class in bookcase.py rather than the Book class in the book.py file. Then we want it to yield from self.books

Hopefully, this helps. Let us know if you are still stuck! Keep up the good work :D

it keeps saying 'BookCase' object has no attribute 'books'

Hi My friends, Hopefully, the code below worked for me. Keep up the good work :D

from book import Book class BookCase: def init(self): self.books = []

def add_books(self, book):
    self.books.append(book)
def __iter__(self):
    yield from self.books