Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Iterating the spots

Login or purchase this course to watch this video and the rest of the course contents.
0:00 There were two reasons I added this idea
0:02 of the parking lot. The first one
0:04 was I wanted to talk about
0:05 the staticmethods and how we can do this
0:07 to create a factory type of thing.
0:11 Here's the second. So, what I want to do is
0:13 instead of just printing this out and go
0:14 Oh, this is a super way to look at it.
0:16 I'm sure my users would love to just have a
0:18 flat dictionary dumped here with no data in it.
0:20 There's a couple of improvements.
0:22 First of all notice this is not an incredible
0:26 representation of the SportsCar or the BasicCar.
0:29 Whatever, right?
0:31 It would be nice to have something better.
0:33 So, I can go to the car and I can implement one of the
0:36 what are called, magic methods
0:38 or Python data model methods.
0:40 And those are the dunder methods.
0:42 So, if I go over here and type def
0:44 and double underscore
0:46 I have abs for absolute value, add async
0:50 Enter what ending what something means
0:52 what boolean conversion, deep copy copy
0:55 Notice there are a ton of these things here.
0:58 In .NET, you might override what ToString mean here.
1:02 So we can do the same, like this.
1:04 But it actually turns out there's two
1:06 that get called in different situations.
1:09 You can, if you use it in a string
1:12 this get called if you just dump it out
1:15 like we just did than this one gets called.
1:18 We can actually just return
1:21 one when the other exists.
1:23 So let's just return a string
1:25 that has the type name here.
1:27 And then, maybe it says like the model name
1:29 and the price.
1:37 And if want to have digit grouping
1:39 not too many zeros, things like that
1:41 say put a little format, :x.0f or,0f I think it is :,0f
1:48 There is a comma, There is a .0f
1:50 and over here, we are going to say
1:52 type(self).__name__.
1:57 I think that'll do it. Let's run it again.
2:00 Check this out. Sports car.
2:01 Model. Corvette. That's the price.
2:04 Basic Car.
2:05 Wind Star. Price, notice the digit grouping and
2:07 there is no extra zeros
2:08 even though it's a float.
2:09 Pretty cool right? So this
2:11 these two methods represent part of Python's
2:15 special data model their dunder methods.
2:18 This is how you implement some of the
2:20 core interfaces of Python.
2:22 So for example, what I would like
2:24 to do up here instead
2:26 would say something like this
2:33 for spot, car in lot:
2:34 something like that. I'd like to print out
2:38 a statement about it. Like this.
2:42 And I would also say if car, like so
2:45 we got to make sure there is a car here
2:47 and then we are going to print it out.
2:48 We don't want to print the empty ones.
2:50 I noticed, PyCharm says mmm
2:52 We thought there would be a collections.interval here
2:55 but this is just a random parking lot.
2:58 It's not going to go so well if you try this.
3:00 Let's see. Nope. It didn't go so well.
3:03 Nope, not interval.
3:04 This should be like C# compilation error
3:06 object lot is not IEnumberable<T>
3:08 or something like this right? So, how do we fix this?
3:13 Well, we go back and we use one of these dunder methods.
3:15 These magic methods. Like the constructor is.
3:18 So we go over here. I'll put it below.
3:23 And here is called __iter__. We implement __iter__.
3:27 And what we have to do is return something
3:31 that is the collections we are trying to loop over.
3:36 Let's just say i, yield i. Here we go
3:41 Created a generator that shoots out the items.
3:44 So here we go. Here is the spot.
3:45 It is A1 and it has the car, sports car
3:48 which has this represents this, this
3:50 two string equivalent, the __str__, implementation over here.
3:55 Pretty cool huh?
3:56 This lets us dig into the classes and change
3:58 the way they basically intergrate the Python
4:01 language. Right? We implemented __iter__
4:05 over here to allow us to loop over the lot.
4:07 And then we implemented __repr__ and __str__.
4:13 This one would be the str.
4:14 But if we printed, we just did print car
4:17 like that would be the __repr__ version
4:19 we implemented that here to change basically
4:22 the two string representation for that object.