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

I don't understand how we got control of the sneaky attribute with super().__init__.

I can't figure out how Kenneth "got control" over the Sneaky attribute by creating another init and then using super() right after. How is it that we are able to define Sneaky now (at the end of the video) as opposed to before (in the beginning of the video).

If anyone could help me understand that I would appreciate it alot. Thanks in advance! ☜(゚ヮ゚☜)

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

1) In the example at this point, super() was being run and self.sneaky was being set after this in the new init, meaning it wasn't initialised in the same way but just kind of heaped on at the end, but as it wasn't initialised in the correct way, it won't be mutable.

2) It is inheriting from it's parent class, both exist but now Thief has it's own init which is pretty much just reusing the code in the superclass init. So, super isn't using the parent classes init, but copying it and passing in all the parameters we pass in and running it.

3) It is referring to the parent class, super() is pretty much making a copy of that code to save your time and removing the need to write a whole init again.

I legit think I got it Josh, thank you so much for your time and everything!!

Josh Keenan
Josh Keenan
19,652 Points

Truly a pleasure, I was where you were a few years ago and it seems those who helped me have moved on, so I'll be about to help out with all Python jazz on here. If you ever have something and no one answers you can tag users using @ in a comment or answer like so Benyamin Kohanchi. So don't hesitate to call, you got this!

Josh Keenan - I don't believe the part about self.sneaky not being mutable is correct. You can create an instance of the class. and then set sneaky to False and it will be False. See the console log.

from characters import Thief

player = Thief("Carlos")

player.name

'Carlos'

player.sneaky

True

player.sneaky = False

player.sneaky

False

Josh Keenan
Josh Keenan
19,652 Points

So before, sneaky would always return as True regardless of any changes you may make to it in a given instance. By modifying the __init__ parameters it will set the attribute but keep it mutable so it may later be changed, giving more control to you. Hope this makes sense, feel free to ask any more questions if not.

Thanks for the response Josh, I really appreciate it! So, just to clarify,

1) When you said:

So before, sneaky would always return as True regardless of any changes you may make to it in a given instance.

what is holding back sneaky from becoming mutable? I think the reason I'm having such a hard time with super() is because I don't understand why the issue exists - or what is causing it exactly. I feel like I'm beating around the bush, if you know what I mean.


2) Also,

By modifying the __ init __ parameters it will set the attribute but keep it mutable so it may later be changed ...

we added __ init __ as a new method in the subclass Thief. Does that override the parent class Character's __ init __ which Thief is inheriting from or do the two __ init __ coexist? I think I'm slightly off here because im not sure whether the parent class' __ init __ is even used when we call the method Thief.


3) And finally, is super() referring to the __ init __ in the parent class Character or is it referring to the __ init __ in Thief, where the super() is located?


Thanks a million Josh, please correct me on vocab usage 'cuz im still getting use to it. ☜(゚ヮ゚☜)