Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Parking lots

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we in our program application.
0:03 Let's break this up a little bit.
0:04 So this part is for creating cars and driving them.
0:08 We also want to park the cars when we're done.
0:10 I want to do that a little bit separately.
0:12 So let's go over here and create a method.
0:15 It's called drive_cars, or let's call it use_cars.
0:20 Then we're going to have another one
0:21 over here called park_cars.
0:24 And we'll pass cars in like so;
0:26 and we'll just go ahead and write it here, I guess.
0:29 And let's be really clear, so it helps us out a lot.
0:32 We're going to take a list of cars.
0:35 Then we'll make sure everything's hanging together.
0:37 Yes, it looks like it is.
0:38 So, the idea is we're going to create another class
0:41 that has some other special behaviors called ParkingLot.
0:50 It's not going to participate in the object
0:51 hierarchy of what we had before
0:53 but it's going to do a couple of interesting things.
0:55 First of all, when I create a ParkingLot
0:57 it's going to have different levels.
0:59 Think of a parking garage;
1:00 there's level A, level B, level C.
1:02 Sometimes they get created like the orange level
1:04 and the green level or, you know
1:06 the carrot level, whatever.
1:07 We're going to go basic; we're going to have level A, B, and C;
1:10 and we're going to have a spot 1, 2, 3, 4, whatever in those.
1:13 We're going to take a constructor.
1:15 We're going to create a __init__, and it's going to take
1:17 a string, a list of strings called spot names.
1:22 And be a list of str, like so.
1:27 And we can use PyCharm's magic to just add this as a field
1:30 but we don't actually want to.
1:31 Because there's just not enough information for us to store.
1:34 We need to know what are the spot names
1:37 and is some car parked in there.
1:39 And if it is, what car is actually parked in it?
1:41 So what I'm going to do is create a dictionary
1:44 called spots, and we can say this is a dictionary like this.
1:48 You technically can also write it like this
1:50 but let's be explicit for a second.
1:52 for n in spot_names.
2:03 We're going to to set, create an entry in the dictionary
2:05 and we're going to currently set it to be None.
2:08 Okay, this is what we need, we, when later on
2:10 we're going to put a car into into that spot.
2:12 So we'll be able to say the free spots are the ones
2:14 where it's None; and the ones where it's not None
2:16 that is, we could pull the cars out and work with them.
2:18 And this is totally fine.
2:20 We could, we could roll with this
2:21 but I want to show you something else
2:23 we can do in Python that's cool.
2:25 We talked about the list comprehension, remember?
2:27 2 n, something like this.
2:34 And it even had an if statement here.
2:35 This is a little bit like LINQ.
2:37 This generates a list.
2:38 We can actually generate a dictionary in a similar way.
2:41 So we can say self.spots
2:42 and we could put a type here if we wanted.
2:44 We could say this is a dict of str, Car.
2:50 Like that, I think this is what we need to say.
2:53 We can say this is equal to not square braces
2:56 but curly braces and put a little expression here.
2:58 So then it, the way it goes you say, key:value.
3:01 So let's say n:None
3:05 and then for n in spot_names, like this, all right.
3:10 So what that's going to do
3:11 it's going to create a, the same thing here.
3:12 So, like, we create the dictionary, then we loop over and we
3:15 put an entry for the name and we're putting None in there.
3:19 And we have self.spot_names, spots of whatever.
3:26 Notice Car, Car, Car, Car, Car, Car.
3:28 Because we told it, it's not just a arbitrary dictionary
3:31 but key is a string and the value is a Car.
3:35 Technically, this should be an optional of car.
3:39 We're going to be as accurate as possible
3:41 but really we'd be able to get away with it, right.
3:43 Obviously this is, something we want to indicate.
3:47 So here is our parking lot, and we're going to
3:49 be able to store cars into it.