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

Create a __str__ method for this book class. Return a string with the author and title of the book. Ex: John Green, Pape

Can somebody please be so kind and post the complete code for this tasks 1,2,3 i want to compare it with my mess that i did. I have been trying to resolve this for to long now. Please post the code so i can compare it.

2 Answers

Task 1 and 2

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

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

    def __eq__(self, other):
        return self.author + self.title == other

Task 3

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

Thank you so much, this was the real deal answer. I made so many mistakes in my code with this one that i completely erased it at the end, now i understand better this whole task.

Kind Regards

i wrote in this specific code and its giving me a

AssertionError: False is not true : Make sure you have a __iter__ method in your class

code even though its clear I have the def_iter_ method in place already

Megan L
Megan L
2,905 Points
class Book:
    def __init__(self, author, title):
        self.author = author
        self.title = title

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