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 Basic Object-Oriented Python Welcome to OOP Creating a Panda Class

Allison Welch
Allison Welch
3,256 Points

Why 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?

panda.py
# insert your code here
class Panda:
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self):
          self.is_hungry = True

1 Answer

Steven Parker
Steven Parker
231,271 Points

The 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
Allison Welch
3,256 Points

Yes, as I worked through some of the later videos it makes more sense now. Thank you for your response!

Steven Parker
Steven Parker
231,271 Points

Allison Welch — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!