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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Add Public Location Property

00:00 To avoid the strange output, I’m going to introduce a new instance method here that I’ll call .location(). And I’m going to start off right away with saying this should be a property.

00:15 It’ll calculate the location based on self._location, the non-public location attribute that I have on an instance. Essentially, I want it to return self._location.location_type, but only if self._location is not None.

00:43 And else I could return something else, maybe just the string "void" so that it says it jumps in the void. That’s where everyone gets instantiated at first, right?

00:54 So this is a ternary expression here. You could also write this as a conditional statement. I’m just doing it here as a one-liner because that’s the way that I thought about it.

01:05 I’ve also decided to make it a property so that you can then access it on an animal object just like you would an attribute. But it does this, a little bit of logic in there that doesn’t return the location object that I’m storing in self._location, but actually just a string that’s readable.

01:23 And that also allows me to change the f-string inside of .__str__() to just point to self._location, which is now going to be the location() property that I defined here, at least in theory. Let’s go try it out.

01:41 Let’s make another dog and a field,

01:49 move the dog, probably move to the field, and then print dog. The happy black dog Puppy jumps in the field. That sounds good. What if I have a new instance of Dog that hasn’t moved anywhere yet?

02:12 Also black and happy, and then I can say print the dog.

02:18 The happy black dog Fido jumps in the void. Okay, so now whether or not the ._location attribute points to None or a FarmLocation instance, I get a readable output, and I’ve also introduced a new property

02:35 that gives me quick access to figure out where the animal currently is. So dog.move(field) and then dog._location gives me as an output that it’s currently in the field.

02:52 I’m happy with that. So here I’ve improved a bit the string representation, the output, and introduced another property. And that’s not an uncommon way of working with properties, where you have a non-public attribute of the same name basically that you only use internally.

03:09 And that is a little different than the one that you want to show to the users of your module. And in this case, the ._location attribute that I’m using should ideally point to a location object.

03:21 And you don’t really want your users to see the location object, which is why in the public location() property, you do a bit of logic by only exposing the .location_type of that location object.

03:36 And I’ve also introduced a ternary operator that prints "void" instead, if it’s in the uninitialized state of None that every animal gets born into at the beginning when we instantiate it.

03:50 Great. I feel like this farm is getting somewhere now. Is there something else we could do? Of course, there always is.

Become a Member to join the conversation.