Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Basic Object-Oriented Python Welcome to OOP Adding to our Panda

Duane Smith
Duane Smith
1,918 Points

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.

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:

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,464 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!!!