Empty the Animal Bellies
00:00
So, in the previous lesson, I converted basically this .is_hungry()
method into a property by just adding the @property
decorator on the top. That’s just to make it a little easier to interact with.
00:10
Now, I want to use .is_hungry()
inside of my .eat()
method as well. For now, currently, I could just keep making the animal eat, and it would just continue adding into this counter.
00:21 So, I want to introduce some sort of natural farm type of way to reset this, which is going to be that I’ll give my animals a chance to poop. Let me see how to do that.
00:32
I will define another method here that I’ll call .poop()
, and it’ll also take self
, the instance. And then what I want to do really here is I want to reset the counter here, the self._stuff_in_belly
. I want to just reset it back to zero, which is a representation of what happens after they poop, and then maybe also return a string as we’ve been doing where I give some information on what just happened. In curly braces, I’m going to say f"{self.name} poops, then looks relieved"
.
01:07
Great. So now I need to trigger this .poop()
method somehow, so you can just do it explicitly. I’m fine with this that you could call .eat()
or .poop()
specifically on an animal instance, but I also want to make it trigger if they have too much in the belly, and this is what I’m going to use .is_hungry
for. I’m going to say, if not self.is_hungry
01:35
and you still call the .eat()
method, then I’m going to return a call to self.poop()
01:45
and otherwise, I will increment the counter and then tell you that it’s eating. Let’s look at this code again. Here I’m utilizing this .is_hungry
property that we defined earlier.
01:57
This is essentially a method call that figures out whether self._stuff_in_belly
is bigger than two and returns False
if it is bigger than two, and if it’s not bigger, then True
.
02:09
So if self.is_hungry
returns False
, then I will call the self.poop()
method, and then it’s going to shortcut lines 17 and 18. It’s not going to execute those, which means that if I’m trying to call the .eat()
method on an Animal
instance that isn’t hungry, then instead of eating, the animal’s going to go poop.
02:29
And if it is hungry, then it’s going to eat and increment the counter. And if it goes to poop, then inside of the .poop()
method, it resets the ._stuff_in_belly
counter to zero and returns a string that tells us what’s going on. Now that makes sense.
02:46 Let’s try it out to get a feeling for what it does in the output, in the code, and also test whether it’s actually working.
02:55
Let’s work with a pig this time, pog
. Let’s call the pig pog
pog
the pig,
03:05
"Pog", "pink"
, still "smart"
.
03:11
All right, I have a new Pig
instance called pog
, and I can make Pog eat.
03:18
Pog is eating
. I can check whether Pog is hungry. Still hungry, so I’m going to keep eating. Pog is eating some more. I know it needs to eat once more. Well, actually, let’s check is it’s still hungry. Yes, it’s still hungry currently. Let’s introspect this. ._stuff_in_belly
, the variable’s at 2
. So if I increment it once more, .is_hungry
, it’s going to return False
. And then if I call .eat()
one more time, then it should call the .poop()
method. Try it out.
03:56
pog.eat()
once more, which now points to 3
. Nothing else happened so far, but now if I make it eat once more,
04:05
then instead of incrementing the counter, you can see that the self.poop()
method gets called, and then Pog poops, then looks relieved
, and now it should be hungry again. pog.is_hungry
.
04:20
We’re back to True
because its ._stuff_in_belly
attribute, oops, has been reset to 0
. This is the code that we wrote inside of the .poop()
method. Cool.
04:35
So these three methods that are defined here—.eat()
, .poop()
, and .is_hungry()
, which then I turned into a property—they kind of like modeled this cycle of the farm: animals being hungry, eating something, pooping, being hungry again.
04:50
Of course, this is not an accurate representation of it, but it kind of models it, you know. It gives like a draft of what’s actually happening. They need to eat something. When they’ve eaten enough, they need to poop, and then they’re hungry again. I’m pretty happy with this. Also, it gives us a chance to use the @property
decorator.
05:07
If you’re curious about that, then you can learn a lot more about how to use these types of decorators in your classes in other resources that I’m going to link in the course. And with that done, I think even though we’ve modeled already three methods here, we still have the .move()
method up here.
05:25
That’s going to be an interesting one because in order to make .move()
happen, we’re also going to need locations. And with these locations, I’m going to start off with another set of classes.
Become a Member to join the conversation.