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
Brian Rodriguez
2,811 PointsConfused about how inheritance is shown in this video (OOP Python, "Complex Relationships")
I'm a little confused about how exactly inheritance is shown in this video (OOP Python, "Complex Relationships"). I'm just learning OOP for the first time, so perhaps this is obvious.
So, when Kenneth creates an instance of the thief class, he passes in a string for the name kenneth = Thief('kenneth'). But where exactly does the inheritance come in to play? Does the thief class inherit the init function from the character class? Is that why we can set the name of the instance of the Thief object named kenneth when it is created (by passing a string argument) despite the name attribute not appearing in the definition of Thief class?
Sorry, I am not sure how to link this to specific course/video.
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsGreat Questions!!
-
Where exactly does the inheritance come in to play?
When an object is referenced within the
Thiefclass, if not found explicitly withinThief, the parent classCharacteris checked for the reference. -
Does the thief class inherit the init function from the character class?
Exactly! When
Thiefis instantiated, its__init__method is run, since a local__init__is not defined, theCharacter.__init__()is run. -
Is that why we can set the name of the instance of the Thief object named kenneth when it is created (by passing a string argument) despite the name attribute not appearing in the definition of Thief class?
Yes. Though the
__init__run is from theCharacterclass, it is the self argument that points to the current instance (which is aThiefinstance) so the lineself.name = namesets thenameattribute in theThiefinstance.
Post back if you need more help. Good luck!!
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsLink to course video Complex Relations
The above line was accomplished using
Link to course video [Complex Relations](https://teamtreehouse.com/library/complex-relationships)