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

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.

00:18 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.

00:36 I will say if the length of self.animals is bigger or equals to self.spaces, then I will return True.

00:52 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.

01:14 So this >= 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.

01:29 So line 14 is never going to run if it is actually already full. But if the conditional expression on line 12 evaluates to False, then return False on line 14 is going to run.

01:43 This looks similar to .is_hungry() down here,

01:49 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.

02:03 Again, this doesn’t change much except that now you can access it without actually calling the method and just pretend that it’s an attribute on the instance.

02:12 Let’s give it a try.

02:16 Make another barn

02:20 with just one space to make it easier for us. barn.

02:25 barn.is_full gives me an AttributeError. So we see that I made a mistake here. It’s pretty descriptive. It tells me barn object has no attribute .animal. Did you mean .animals?

02:39 This points me to line 13 in the definition of .is_full where I accidentally typed self.animal instead of self.animals.

02:49 So now I’m going back in here and add in the s, and I should hopefully fix it. I’m going to run it again.

02:58 Make another barn with one space.

03:04 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.

03:16 Just make sure that you read the error messages and see what they point you to. They’re often very descriptive. This one even tells me there is no attribute .animal. Did you mean .animals?

03:25 So I can just go over here and figure out where did I accidentally write .animal instead of .animals and fix that. Cool. My barn is not full at the moment.

03:33 It’s returning False because there’s one space, but there’s no animal in there. If I do barn.animals—don’t forget the syou 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 "sheep".

03:57 And now barn.animals has one item in there. So if I say what’s the length of barn.animals, it’ll return 1. This means that the barn should be full.

04:11 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.

04:37 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 .enter() and .exit() that you’re going to define next.

Become a Member to join the conversation.