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 trialAllison Welch
3,256 PointsWhy is 'is_hungry' attribute not included inside __init__?
Is it because it's always true? If that's the case, then why wouldn't you put it at the top with the other attributes instead of inside the init method?
# insert your code here
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
def __init__(self):
self.is_hungry = True
1 Answer
Steven Parker
231,236 PointsThe variables "at the top" are class variables, and are shared by all instances of the class. This makes sense for something like "species", since all pandas will be the same species.
The variables declared inside the __init__ method (and referenced through "self.") are instance variables, and are unique for each instance. This makes sense for something like "is_hungry" since all pandas may not be hungry at the same time, even though every newly created one starts out that way.
Allison Welch
3,256 PointsAllison Welch
3,256 PointsYes, as I worked through some of the later videos it makes more sense now. Thank you for your response!
Steven Parker
231,236 PointsSteven Parker
231,236 PointsAllison Welch — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!