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

Tom Watson
Tom Watson
9,987 Points

Why use __init__ on Bookcase()?

Why do we need to use __init__ on the Bookcase() class? All I see that is happening here is we are assigning the book attribute to self, but I can't see where this is used? Is it at cls(books)?

I've tried to run this code without defining __init__, and obviously it doesn't work, i'm just trying to understand why!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The line:

    self.books = books

is needed to create the attribute books on the Bookshelf class instance. It is in this attribute that the list of Book instances are stored. Without this attribute the Bookshelf instance would contain nothing and the Bookshelf __init__ method that initializes the Bookshelf instance would not do anything with the list of books passed in.

Please post back if you need more help. Good luck!