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 Intro to Inheritance

lindseyk
lindseyk
4,506 Points

Curious about the general structure and order of setting things up:

  1. Why is the list of potential colors set up outside of the Monster class, rather than inside of it?

  2. Also, why are some attributes set up before the init, and others are set up from within the init function?

(I'm new at programming.) :)

Thanks!

1 Answer

Brett Petersen
Brett Petersen
16,511 Points

To answer your first question, the COLORS list is declared and set outside of the Monster class because any variable that you set inside the class is actually a class attribute. Since, in this case, we only want a Monster to have one color that is chosen at random from the list of colors, the list itself is not the attribute that we want to give to one of our Monsters, and it, therefore, makes sense to declare our list of colors outside of the class.

For your second question, the attributes that are setup before the __init__ function are the 'default attributes' which will be common to all monsters. The purpose of the __init__ function, in this case however, is to give us a different monster nearly each time a new Monster is created. The attributes of each new monster are then set in __init__ according to the constraints of the default attributes for the Monster class.

Hope that helps!

[Added backticks to __init__ -cf]

lindseyk
lindseyk
4,506 Points

Yes, more clear now - thank you!