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 Inheritance Super!

Iain Watson
Iain Watson
3,031 Points

Override add_item in Super! Task 2 of 3 (and cf override init in characters.py)

I'm not sure what this is asking. In characters.py there were 2 inits in Thief(Character) the 1st of which was used to add the attribute sneaky. I'm not sure how to override a method in the superclass and then have it still run in the subclass. And why would you want to do that?

inventory.py
class Inventory:
    def __init__(self):
        self.slots = []

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

class SortedInventory(Inventory):
    def __init__(self):
        super().__init(item)

1 Answer

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hello Watson, The Task asking to override the add_item() function, when you call super(), it is not necessary to override the init() funtion, with super you can override any function from the parent class

super().add_item(item)

Of course for this you need to create an add_item() function in the child class to in order to be able to override the parent method

def add_item(item):
    super().add_item(item)

What you tried it's the init() method but the quiz doesn't ask that. Another thing in the future when you overide a buildin function or magic method, you should use "_" in both sides

__init__()

otherwise if you let just the first 2 "__" that will create a protected method or variable if you use for variables

__name of the function or variable

I hope it was for your help. Happy coding.

Iain Watson
Iain Watson
3,031 Points

Thanks Oszkar, I was really confused about super(). Having read your suggestions and reading the links provided in the teacher notes to super() and the Deep Thoughts blog I am beginning to get the gist of it. Thanks again, Iain