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

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)

3 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

Leslio McKeown
Leslio McKeown
9,531 Points

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
Maryam Talakoob
Maryam Talakoob
1,835 Points

Hi all, Could you please help me with the following code. I spent nearly 45 minutes trying to debug it but still get an error: AssertionError: False is not true : Make sure you have a __iter__ method in your class Thank you so much. p.s. I checked for Bookcase vs BookCase as well as every space and indentation to no avail. :-(

class Book: def init(self, author, title): self.author = author self.title = title def str(self): return f"{self.author}, {self.title}" def eq(self, other): if type(other) is not type(self): return False return self.author == other.author and self.title == other.title 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
Maryam Talakoob
Maryam Talakoob
1,835 Points

I figured it out. I had to use this code in the Bookcase.py tab not the book.py tab. It was not clear which file the question was landing on. I understood now. thank you every one.