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 Adding to our Panda

Not sure how to "Call a Method" inside another method within a class

I'm trying to follow this third part and it seemed straight forward enough, but I'm honestly lost. They want us to check if "is_hungry" is true. If it is, call the method (eat(self)) inside the check_if_hungry(self) method. I don't get how i'm supposed to do that.

panda.py
class Panda:
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self, name, age):
        self.is_hungry = True
        self.name = name
        self.age = age

    def eat(self):
        self.is_hungry = False
        return "{name} eats {food}.".format(name = self.name, food = self.food)

    def check_if_hungry(self):
        if self.is_hungry == True:
            self.eat(self)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Duane Smith, any instance attribute (names with that namespace) may be referenced using self. This is valid for method references.

Using the self. prefix says look for this reference in the current instanceโ€™s namespace.

To see whatโ€™s in a namespace use dir(). From within you can use dir(self). From outside use dir(instance_name).

Note: when calling an instance method, do not include the โ€œselfโ€ parameter. So, use self.eat()

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

Thanks Chris Freeman for the feedback!