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