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