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 Emulating Built-ins

Confusion in code

class Inventory: def init(self): self.slots = []

def add(self, item):
    self.slots.append(item)

def __len__(self):
    return len(self.slots)

def __contains__(self, item):
    return item in self.slots

def __iter__(self):
    yield from self.slots

Where does self.slots comes from, it was not defined inside init method. any suggetion will be appreciated ?

1 Answer

Jake Kobs
Jake Kobs
9,215 Points

In the init method, since slots isn't being passed within the parameters, it's automatically being assigned to an empty list. Therefore, slots is being created in the init method.