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 (retired) Inheritance Subclasses

Doubts !!

The doubts looks simpler,but please help out..

  1. Whats the purpose of 'random' here?(I meant why random is imported)

2.How azog.hit_points becomes 2? what's the purpose of initializing min_hit_points,max_hit_points and min & max_experience?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points
  1. Whats the purpose of 'random' here?(I meant why random is imported)

random is imported to gain access to the choice and the randint methods. choice() selects a random choice from a list. It is being used in the Monster class __init__ method to pick the color. randint is used to set the hit_points and experience attributes.

  1. How azog.hit_points becomes 2? what's the purpose of initializing min_hit_points,max_hit_points and min & max_experience?

azog is a Goblin. The class Goblin overwrites the attribute max_hit_points inherited from Monster. When a new Goblin is created, the __init__ method also inherited from Monster executes which sets hit_points. The self.max_hit_points used is the overwritten value in Goblin which has precedence over the value in the Monster parent class.

Does this make it clearer now?

Gotcha! Thank you so much :)