Discover Issue Referencing Location Objects
So you’ve finished implementing the
.move() method, which makes it more than three instance methods already, but this is an interesting one because you’re interacting with other classes as well. So you’re interacting with a location object from your
I can print a dog.
The happy black dog Puppy jumps over the lazy dog. So currently, one thing that jumps to mind for me now here is that I’ve made
._location a non-public attribute, which means that the user of my
Farm module basically is not really expected to do anything with
01:01 But previously when I was testing, I did want to look where is the dog at at the moment, right? So it would be nice to have an actually publicly accessible location that is meant to be accessed by the user so that they can check where’s an animal at. And when I see the string, I might include it into the output here as well. Let’s try that out.
I just kind of want the output of the location type. But the problem if I do
.location_type here is that if I want to print an
Animal before it has an object assigned to that attribute, this is going to fail.
then I run into to an
AttributeError: 'NoneType' object has no attribute 'location_type'. This is because
self._location at the beginning is
None, and then this f-string can’t get properly constructed here on line 43 if it doesn’t have a location.
But it only works if
._location references a location object. Now what if I just take away
.location_type and just print out
._location, and it would be
None at first, but then it would point to an object.
And for the
Field object, that’s the string representation that we defined earlier: The field has so and so many spaces out of so and so many spaces filled. This is why it’s more readable if you actually just get the
.location_type, but then we have an issue with
There’s lots of ways that you could solve this, of course. I think I’m going to do it by introducing a new property. Probably that’s going to be the location, and that just gives back the
.location_type if it references a location object. Sounds probably more complicated than it is. Let’s go do it in the next lesson.
Become a Member to join the conversation.