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

Prevent Overpopulation

00:00 The next step here is to update self._location

00:06 equals new_location. So in that case, I’m replacing the location object that’s currently saved at the ._location attribute to the new location object that I’m passing in here as an argument to .move().

00:22 So now I’ve updated self._location with the new location. Well actually, I should check first whether it’s full before updating it. So I need to make a check in here, if new_location.is_full(),

00:40 then I don’t want to add it. Return again with a message. Now I’m accessing new_location, the location object that has been passed in. new_location.location_type.

00:55 So this is the instance attribute on a location object, .location_type, which is going to be barn or field in the cases that I’ve defined so far.

01:04 So I’m going to say f"The {location_type} is full" and then I don’t want to move the animal. So I actually want to do the check still before updating.

01:22 And now I’m at the point of being ready to update self._location is the new_location. And after updating, now I need to make sure everything works.

01:33 Update info on the animal object and in the location object.

01:42 Enter into a location. Start with that. So now I can access the location object either through self._location or new_location. I’m going to stick with new_location because it seems a bit more descriptive for me. new_location.animals, again accessing the animals list.

02:02 And then I want to append the animal instance here, which I can access through self. new_location.animals.append(self) should add the animal to the new location and I’ve already updated self._location for the animal as well.

02:22 So I actually think that might be it. I think I’ve updated the information both on the animal object and the location object. Then we can also return a string, f-string, where I’m going to say in curly braces, f"{self.name} moved to the" and then I can access again either through a "{self._location}" or through "{new_location}". new_location is a little less verbose. Location,

02:55 .location_type. Okay, that’s a big function with a lot of little messages in between that I put there to help me develop it and think it through. Now, is it correct? I’m not sure for now. I was just thinking it out.

03:12 So in the next lesson we’re going to go ahead and test the new .move() method and see whether it works as expected.

Become a Member to join the conversation.