Avoid Duplicate Animals
I don’t want to enter if the animal is already in the location. So that just means I want to check whether the animal is currently already in the location where I’m trying to move it to and let me update
new_location to make this a bit more descriptive.
So I’m using the identity operator here just to make sure that it’s actually the same objects, as if
self._location points to the same object as
new_location, then I don’t want to move the animal because it’s already there.
So if it has a location object associated to it, in that case I want to exit from that location because it’s not the same location that I’m trying to move it to, and it’s also not
None. In that case, I want to exit
self._location.animals, so I’m accessing the
animals list on that
02:12 Okay. Here I don’t want to return because this is just one of the steps. I first want to remove it from the old location and then I want to add it to the new location. So maybe I’m going to take a note here, maybe print a message. I’m not sure whether we’re going to need that or not, but it’s an option.
But otherwise, I think I’ve tackled these two tasks. That looks good. Before updating
self._location, I want to check whether it’s already there. If it is, end the method call right there. If it isn’t, but it is already in a different location object, then I want to remove it from that other location object before putting it into
new_location. Cool. Sounds good.
Become a Member to join the conversation.