Python Object-Oriented Python Inheritance Super-Duper!

Andrew McLane
Andrew McLane
3,385 Points

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 = 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! :)