Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Emulating Built-ins

Ivan Frances Alcantara
Ivan Frances Alcantara
4,528 Points

Why does sword.description return only sharp? (min 7:25)

If I am not wrong, Kenneth creates an item in Python shell with instance = Item(attribute1, attribute2, attribute3); and then calls description on it, which returns only attribute2. I have not seen .description being used before in Python Basics course, so I'm a little bit lost there.

1 Answer

I'm not sure in which video he creates the items.py file but I downloaded the project files to see what they contain. In items.py is an Item class:

class Item:
    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __str__(self):
        return '{}: {}'.format(self.name, self.description)

class Weapon(Item):
    def __init__(self, name, description, power):
        super().__init__(name, description)
        self.power = power

The Item class has a description attribute and is imported in the video with the line:

from items import Item

He then creates an instance of the class with:

sword = Item('sword', 'sharp')

at which point you can return 'sharp' with sword.description