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

Quick question about Kenneth assigning the class Inventory() = inventory in the repl (2:35)...

Kenneth does this 3 times in the video, and the time stamp 2:35 is the first time he does it. When Kenneth assigns inventory = Inventory() in the repl, is he assigning the class Inventory() to a new variable named inventory so he can call it with ease, or is he doing something else?

This is the code. This file, called inventory, has the class Inventory in it:

inventory.py

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

thanks in advance!

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

He is creating an instance of Inventory in a variable called inventory.

thanks Josh!