Identify the Bug
00:00
Here we are in the bugfix lesson. You may or may not have decided to go on this side quest to identify and fix the bug in the .move()
method. Irrespective of whether you did or not, here’s my quick walk-through of how I ended up fixing this myself.
00:18 Before being able to fix it, we first need to identify what the bug actually is. And like I mentioned earlier, it’s a bit subtle. It only happens in a specific situation. So I’ll try to reproduce this.
00:32
Run the code, and then over here in the interactive shell, I’m going to reproduce the code so that you see what’s going wrong in this .move()
method currently. We’ll need a field
.
00:47
I give it two spaces. And we need a barn
. And I give this one one space. The bug is related to trying to move animals into a location that’s currently full. And it works if the animal doesn’t have a location, but it has a strange result. If the animal does have a current location, let me show you. We’ll need two animals. I’ll start with a dog
01:13
that I’ll call "Fox"
and it is "brown"
and "quick"
. What a confusing dog, but that’s not the bug. And then a second animal that’ll be a pig
. And I’ll work with "Lizzy"
again.
01:35 It was pink, and in this case, kind of confused.
01:43
Okay, so I have two locations. I have a Field
and the Barn
, and I have two animals, a pig
and a dog
.
01:50
Now I’m going to start by filling up the barn
by moving the dog
there. I’m going to say dog.move(barn)
dog
moved into the barn
and now barn.is_full()
returns True
.
02:06
Now we have two cases. Currently, the pig’s location is the void, and if I now attempt to move the pig
into the barn
, everything works still as expected.
02:18
So if I say pig.move(barn)
, my code tells me the barn
is full. The pig’s location is still the void, and barn.animals
still only contains the Dog
object. So that’s fine. However, the bug starts if the pig
moves from somewhere else.
02:39
So let’s put it into the field
. Now the pig
is in the field
, and if I now attempt to move it into the barn
, which is full,
02:54
it still tells me that the barn
is full and it didn’t move to the barn
. So barn.animals
still only contains the Dog
object and the pig’s location is still the field
. So that’s all good.
03:08
However, if you now take a look at the field.animals
, then you can see that the field
is empty, which means that the pig
moved out of the field
and is not part of the field
object anymore.
03:24
But its ._location
still points to the field
. So the pig
thinks it’s on the field
, but the reality on the field
tells us otherwise.
03:34
So this is the bug that I’ve introduced in here by kind of messily constructing my conditional logic in the .move()
method. So after establishing what the problem is, in the next lesson, let’s reset up the code that I wrote in the interactive shell here in a way so that we can run this more often without having to type all of these lines of code every time.
Become a Member to join the conversation.