Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Car methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, our car is interesting.
0:01 It has these properties, right.
0:03 We saw that it has base price models
0:05 cylinder and engine type.
0:07 But you can't really do anything with it.
0:08 We can construct one through its initializer
0:11 but classes typically are valuable
0:13 because they bundle data and behavior together.
0:17 Right now we just have data.
0:18 So let's go and add a method here.
0:20 So again, we're going to make the method part of the class
0:23 by just indenting it within the class.
0:27 And then we're going to say def
0:28 and we have two methods we want to define
0:30 we want to define drive, and we're going to define refuel.
0:32 Now watch what happens when I close the parenthesis here
0:35 or open it to start defining the parameters.
0:38 As I open, notice it, PyCharm, automatically types self.
0:42 Because this is an instance method
0:45 it can only be called
0:46 if it has this explicit self parameter.
0:49 That takes a little bit getting used to
0:51 but it's not hard, it's just different, okay.
0:53 Then down here we're going to have a little print statement.
0:56 We're going to say the car, say this is from the car
0:59 the whatever, self.model_name
1:03 goes vroom and let's tell PyCharm that is not misspelled.
1:06 We're going to have another one refuel.
1:08 Same thing we have our self here
1:10 and we have a print statement.
1:11 Actually on this one, let's just pass for now.
1:13 Remember this one is going to be the abstract
1:15 one that we don't know how to deal with.
1:17 Say it's blank, this is like
1:19 the equivalent of just empty
1:21 so I use the word pass.
1:23 I know something has to go here for this syntax to work
1:25 but I don't care what it is, ignore it.
1:28 Go over here and instead of doing this
1:30 let's say car.drive
1:33 and notice it has this function here.
1:35 And car.refuel, okay. Run it again.
1:41 Yay, the car, the Corvette goes vroom
1:43 the Windstar goes vroom, the Tesla goes vroom
1:45 the Volt goes vroom. Not super accurate, or really interesting
1:48 but it does work, right.
1:50 Our cars are driving and they're refueling
1:53 and let's put a little new line between them.
1:56 Cool, so we've got these methods
1:58 that we've created over here
2:00 and, right, we saw that they're super simple.
2:03 just define a regular, like a function
2:05 has the self as the first parameter.
2:07 If you don't put this there, if you put a, b, c
2:10 notice it's yellow and the others are not
2:13 and this is usually the first parameter is self.
2:16 You might be forgetting that you've
2:18 you might want to put something there.
2:19 So the tooling does help
2:21 but you got to remember to do it.