Model Location as Instance Attribute
00:00
In the Animal
parent class, we modeled .animal_type
as a class attribute. And now for .farm_location
, I’m instead going to model it as an instance attribute.
00:09
I’m going to say self.location_type
,
00:14
and I’ll set this one to None
as well for the general .farm_location
. But then inside of my child classes, I want this instance attribute .location_type
to be set every time to "field"
for Field
instances and every time to "barn"
for Barn
instances.
00:32
And what I can do to make that happen is the same that you did earlier with the .talk()
method. You’re just going to have to extend, instead of extending .talk()
, you’re going to extend the .__init__()
method.
00:44
There’s nothing that special about those special methods, right? They’re just methods as well. They just have predefined things that they do. But that doesn’t mean that you can’t work with them like you do with other methods. On line 10, inside of the Field
class, I’m going to define a new .__init__()
for Field
that’s also going to take self
and spaces
.
01:06
And then I will extend the parent .__init__()
, calling super().__init__()
01:15
and then passing spaces
forward to the parent method’s .__init__()
. So now everything that’s in the body of the parent .__init__()
runs again.
01:22 But now I want to modify it a bit.
01:28
And after setting self.location_type
to None
through the parent .__init__()
, I’m going to go in here and say self.location_type
01:40
equals "field"
. So look at that for a second and see if you understand what’s going on here. The short version is basically just that you’re extending the parent’s .__init__()
with a new .__init__()
for the child class, and this child class .__init__()
calls the parent’s .__init__()
.
02:01
It does everything that’s in the body of the parent’s .__init__()
. So it already assigns a .location_type
but assigns it to None
.
02:09
And then afterwards, I’ve added one additional line of code in line 12 overriding self.location_type
, the instance attribute that was set to None
by the parent’s .__init__()
, to the string "field"
for all of the instances of the Field
class.
02:26
So this is a different way of making sure that every time that I create a Field
instance, it’s going to have its .location_type
set to "field"
.
02:33 And I’m kind of circumventing the potential problems that I could get from using a class attribute for that. Again, a trade-off either way. This is a little more expensive too because I need to assign it for each instance, while that class attribute I would only have to assign once.
02:49 So there’s a bit of overhead here, but I just wanted to show you that there are different ways that you can do this, and this is also a viable way of doing it.
02:58
And in our Farm
locations, this is how we’re going to handle it. So also inside of the Barn
, I’m going to do the same thing. And instead of setting the .location_type
attribute to "field"
, I’m going to set it to "barn"
always.
03:11
Let’s give it a space so it’s a bit better readable. There’s no magic going on here. You’re just doing the same that you did earlier in the .talk()
method of the child classes of Animal
. You’re extending a method from the parent class, just in this case it’s the .__init__()
that you’re extending.
03:31
Okay, next up, let’s define a .__str__()
method so that we can test this out and get a nice representation of these new location objects.
Become a Member to join the conversation.