Check Whether Animal Is Hungry
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
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.
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.
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.