Python for .NET Developers Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Python car base class, pass 1

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw the car class in C#. Let's go create the same thing in Python. Over here, I'm going to have a program just going to start with that.
0:12 And then in the C# world, we had a directory called models. And over here, we had a class called cars, car. I think that's what we called it.
0:22 We're going to run this so we can get that set up up here and it's going to work with whatever we define in this file here.
0:32 We can break the classes up into one class per file. We can have it all crammed into a single .py file if you really wanted to. That's sometimes done.
0:41 I'm not a big fan of it but there's nothing in the language that says one class per file or anything like that, right?
0:48 It's very free form. Remember in C# we had a class and it was a car like this. So in Python, we write literally exactly the same thing.
0:57 The naming convention is also similar capital C, camel casing. We had electricCar earlier, like this.
1:04 So also similar convention, when you derive from something you would have the base class like that as opposed to in C# it's like this, okay?
1:13 But pretty similar and if you don't put that you derive from object use still derive from object, also like C#.
1:20 This is how we define that there is a class and instead of these curly braces, as you would expect we use colons.
1:26 Here's where you're going to see some differences in how you define types. In C# if you have a field or a property it always gets defined up here.
1:35 Like you would have an int cylinders something like that, right? And then down here, you'd have your constructor.
1:41 In Python, it's a little bit different. You can define the fields or properties up here but it's typical if you want just instance fields
1:51 and you don't want them in the static aspect it's a little complicated. You define them actually in the constructor.
1:57 So we're going to define a constructor and just like methods or functions outside of classes methods inside classes you start with def
2:04 and then the built-in stuff always starts with double underscore. So we have double underscore init or add if you want it to act like a list
2:12 or asynchronous iteration if you want to work with async and await and do iteration in there. So the one we want
2:20 the constructor is __init__ for initializer. And what did we have before? We had something like model name, engine type cylinders, and base price.
2:32 So that's what we're going to have and now what we need to do is we need to create a local field. These are just this method.
2:38 What we need to do is create a field in this class. So we don't have this, we have self in Python and it's explicitly passed, right?
2:46 C# implicitly has these arguments pass for this. Python is very explicit. We're going to go over here and say I'm having a model name
2:54 and that's going to be equal to model name. Obviously it could just be model. Those don't have to match, but there they go.
3:01 We're going to go ahead and make them match 'cause it seems to make the most sense. Now, PyCharm is nice. It'll say, hey, you're not using this.
3:07 Do you want to add a field to this class? Yes, I do. And it'll give a chance to rename it. We'll do that for the others.
3:15 We also saw Python supports types. So we could come over here and say this is a string. This is also a string.
3:22 This is an integer and this is a float, for example. And probably even more valuable is to do that here on these
3:33 this is like defining the types for the field. So now later if we say self dot, you know, baseprice dot notice all the operations are coming from float
3:42 'cause hey, we know it's a float. So here we've defined a class. It's called car. And we can create it with these things.
3:49 Let's go over to our program really quick. We'll just say cars = create_cars(). We'll write a little function down here that's going to do that for us.
4:02 And it can return a list and we can come over here and say I want to create a car. Now in C# you would say new car, like this, right?
4:09 In Python, the new keyword is omitted. We just say car and call it. Now we have to import it up there. And I noticed first of all
4:17 it's saying chapter four models car car. I want to create the stuff that's contained inside this chapter as if it was just its own program
4:26 so we don't have to say the full name. I can go over here and say mark directory as sources root and then it says okay. That's like the top level.
4:36 We're not going to talk about it. We just have models car. PyCharm will write up the top for us from models.car import Car.
4:43 And that let's us write some things here but notice it says you are omitting some stuff. So we can ask, what is the model name?
4:51 Let's go over here and say Corvette. What is the, what's next? Engine type is gas. Number of cylinders? Let's say it has 8.
5:02 And its base price is $50,000. Now, we could write it like this but Python lets you put a little comma digit grouping thing in here
5:10 and that's really nice. And let's put a few more. Alright now, we're getting our cars here. Let's just loop over to them real quick.
5:20 Just print out car. If we just print it right away it probably won't give you the outcome that you're hoping for.
5:25 Oh look, there's a car object at this address. Let's print out something like the model name and car.price, base price, or something like that.
5:34 So there we go. We have Corvette 50,000, Winstar 20,000, and so on. All right, so that let us define a basic class in Python.
5:42 We just defined class like this create a constructor either pass in values or just compute them from somewhere else
5:50 and we create fields by saying self.whatever in the initializer. Notice back over here, when I say car dot it knows it has exactly those four fields
6:02 car, car, car, car, right? It has those four fields because that is the convention in Python over here to create a private instance field
6:12 over here exactly like that. So the tooling obviously knows how to surface that back to us.


Talk Python's Mastodon Michael Kennedy's Mastodon