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

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