Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Check Whether Animal Is Hungry

00:00 I want to somehow give a way for the user to know if the animal is hungry.

00:09 And I’m thinking just to implement that with another instance method here. So I’m going to define a method here that I’ll call .is_hungry().

00:17 .is_hungry(), and it’ll have access to self because we need access to the non-public attribute. I can just now define when is an animal hungry.

00:29 So I’ll say if self._stuff_in_belly is bigger than 2, then I’ll return False.

00:42 These animals are not hungry if they have more than two items in their belly, basically. And otherwise, I’m going to return True.

00:52 You can see, you could also put in an else clause here, but I’m just skipping it because this if clause is going to catch if it’s bigger than 2, and then the function will return with False.

01:01 So if the ._stuff_in_belly is bigger than 2, it’ll never get to the code in line 22 where I’m returning True, and it’ll jump to that line if the conditional expression on line 20 is going to evaluate to False, then it’ll just skip the indented block and then return True.

01:21 So that’s just a shortcut of saying else return True. If you write that, it’s just as good. Okay, so this is a way for me to check is the animal hungry. We’ll try it out.

01:38 I will create the Lizzy Sheep instance again, and then I will now say

01:47 sheep.is_hungry(). So I’m calling the instance method .is_hungry() on it, and it returns True. All right. And then if I’m going to go ahead and feed Lizzy one, two, three times, then it should have more than 2 in the belly. Let’s check it out.

02:04 It should have the number 3 in its belly. I hope 3 tastes good. Okay. And now it does have 3. So now if I call .is_hungry() on sheep, it’s going to return False.

02:18 All right, so this is the public interface that I’m giving for users of these Animal classes to figure out whether the animals are hungry or not.

02:28 So they can call .is_hungry(). They don’t have to deal with ._stuff_in_belly. They don’t necessarily need to know how much is in the animal’s belly, but they can figure out whether the animal is hungry or not, and then decide whether they need to be fed or not. And that works for me.

02:43 But there’s one little thing that I think I want to improve on, that’s something that you didn’t cover in the course, but well, let’s learn something new, which is that I want this method to kind of look like an attribute instead.

02:56 So I don’t want my users of my Animal class to need to call .is_hungry(), but it kind of feels more like an attribute, you know, it’s just information about the animal. So in the next lesson, I’m going to show you how you can turn basically a method into an attribute kind of. All right, let’s take a look.

Become a Member to join the conversation.