Note Whether a Location Is Full
00:00 After adding the string, there’s a way to see how many spaces are filled inside of any of those locations. But now it would be nice to have a meaningful way to check whether it’s full or not. For that, let’s go ahead and define a new method inside of the parent class.
I’ll call this one
.is_full(). That’ll be an instance method as well. This is going to be something like the check that we did up here. We want to figure out how many animals are currently inside the location compared to how many spaces are in there.
So asking the question “is this location full?” is going to return
True if the spaces are filled or if there’s more animals than possible, which shouldn’t trigger, but we still want to make sure that it doesn’t just trigger on exactly the same amount of spaces because maybe there’s a way to add two animals at the same time, and it would pop over the assigned amount of spaces.
>= is just some sort of a safety here, and else I’m going to return
False. Again, I’m not writing the
else because if the conditional expression returns
True, I’m returning the function.
and it also does a pretty small calculation. So this is another good candidate to turn into a property, and I’ll do that right away. I’m going to say
@property at the top of this method declaration.
Okay, and then let’s see if now I can say
barn.is_full. Yep. Now it’s working. So this is not a problem. There’s always typos, there’s always something that you’re going to do wrong while developing this.
False because there’s one space, but there’s no animal in there. If I do
barn.animals—don’t forget the
s—you can see that it’s an empty list. And now just for the fun of it, I’m going to put something in there. I’m going to say
barn.animals.append(), and we’re going to put in a sheep. This is not actually a sheep, but just a string
So if I say
barn.is_full now, then you can see that this property returns
True. Again, this looks like an attribute. It’s actually a method, and that’s just because I used the
@property decorator at the beginning of the method. All right, I like that it’s working as expected. Good that I went ahead and tested it, so it gave me a chance to catch this typo that I had in the method declaration.
We can now figure out relatively easily whether a farm location is full or not, whether it has space for more animals, which we’ll use in the other instance methods
.exit() that you’re going to define next.
Become a Member to join the conversation.