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

Confused 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.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Link to course video Complex Relations

The above line was accomplished using

Link to course video [Complex Relations](https://teamtreehouse.com/library/complex-relationships)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Great Questions!!

  • Where exactly does the inheritance come in to play? When an object is referenced within the Thief class, if not found explicitly within Thief, the parent class Character is checked for the reference.
  • Does the thief class inherit the init function from the character class? Exactly! When Thief is instantiated, its __init__ method is run, since a local __init__ is not defined, the Character.__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 the Character class, it is the self argument that points to the current instance (which is a Thief instance) so the line self.name = name sets the name attribute in the Thief instance.

Post back if you need more help. Good luck!!