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-Duper!

Using name arg inside __init__ and sneaky

I'm not sure why we keep adding "name" as an argument inside of init, and then setting self.name = name?

Also why do we keep creating an attribute named sneaky if when we call create an instance of the Thief class in the console we could just use sneaky as a kwarg and creates its attribute there?

Lastly, the video says that the parent class Character's init method doesn't allow the kwarg "sneaky", and I don't understand why that is. Thanks!

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

When setting the parameter of __init__, giving it a name argument means that whenever an instance of the class is created, name will be a required argument. What you actually DO with name is a separate step; it's not set as an attribute automatically. So Kenneth is just setting the name attribute to whatever is given to it when an instance is being created. If you didn't give it an argument, it'd throw this error: TypeError: __init__() missing 1 required positional argument: 'name'

We don't want to set it every time because, for the Thief class, we want the Thief to be sneaky by default. This way, we only have to set it explicitly when we want sneaky to be False. It's a way to ensure that the class will always have a sneaky attribute.

I'm not too sure on the last one, but I think he means to say that Thief's __init__ doesn't explicitly ask for a sneaky argument. It would accept one as a kwarg, though. He says "our character" but I think he's referring to Thief. I could be wrong, it's not super (haha) clear. Hopefully this helps! :)