Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Create Location Classes

00:00 Let’s begin with making a FarmLocation class. I’m going to do that at the top of the script.

00:08 We’ll figure out what it has later on. I know that I’m going to want to have two child classes, a farm location that I’ll call Field, and it’ll inherit from FarmLocation.

00:22 And another one called Barn that does the same.

00:30 This is my quick draft. I want a FarmLocation parent class, and then two child classes, one called Field and one called Barn.

00:38 Let’s think about FarmLocation. I had a couple of attributes that I want on here, so I’ll add them in an .__init__() method, def __init__().

00:49 Take self to create an object, and then at least I want to be able to say, how many spaces does one of these farm locations have for animals?

00:59 So that’s something that should be defined on the instance, because I could have a bigger Field and a smaller Field, or a bigger Barn and a smaller Barn.

01:07 So I’ll assign self.spaces to the value of the argument that you’ll have to pass in when creating a FarmLocation instance. Then it should have a space to hold animals, but this is not really something that we need to pass as an argument.

01:24 So this again, is going to be an instance attribute self.animals, but I don’t need to pass it in. So I’m just going to make this a list, an empty list object here, so that each instance of FarmLocation is going to have its own list that then I can somehow fill up with animal objects up to a certain amount until it hits the limit of spaces that this specific instance has.

01:52 In the Animal parent class, I kind of defined the .animal_type, started it off with None, and I modeled it as a class attribute and also talked a little bit about potential issues with that.

02:04 And just for the fun of it, I’m thinking it could be nice to have some sort of .location_type also for farm locations, but to show you like how you can do things differently in OOP, I’m going to model it a bit differently for the farm locations.

02:19 Let’s look at that in the next lesson.

Become a Member to join the conversation.