Python for .NET Developers 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. It has these properties, right. We saw that it has base price models cylinder and engine type.
0:08
But you can't really do anything with it. We can construct one through its initializer but classes typically are valuable
0:14
because they bundle data and behavior together. Right now we just have data. So let's go and add a method here.
0:21
So again, we're going to make the method part of the class by just indenting it within the class. And then we're going to say def
0:29
and we have two methods we want to define we want to define drive, and we're going to define refuel.
0:33
Now watch what happens when I close the parenthesis here or open it to start defining the parameters.
0:39
As I open, notice it, PyCharm, automatically types self. Because this is an instance method it can only be called
0:47
if it has this explicit self parameter. That takes a little bit getting used to but it's not hard, it's just different, okay.
0:54
Then down here we're going to have a little print statement. We're going to say the car, say this is from the car the whatever, self.model_name
1:04
goes vroom and let's tell PyCharm that is not misspelled. We're going to have another one refuel. Same thing we have our self here
1:11
and we have a print statement. Actually on this one, let's just pass for now. Remember this one is going to be the abstract
1:16
one that we don't know how to deal with. Say it's blank, this is like the equivalent of just empty so I use the word pass.
1:24
I know something has to go here for this syntax to work but I don't care what it is, ignore it. Go over here and instead of doing this
1:31
let's say car.drive and notice it has this function here. And car.refuel, okay. Run it again. Yay, the car, the Corvette goes vroom
1:44
the Windstar goes vroom, the Tesla goes vroom the Volt goes vroom. Not super accurate, or really interesting but it does work, right.
1:51
Our cars are driving and they're refueling and let's put a little new line between them. Cool, so we've got these methods that we've created over here
2:01
and, right, we saw that they're super simple. just define a regular, like a function has the self as the first parameter.
2:08
If you don't put this there, if you put a, b, c notice it's yellow and the others are not and this is usually the first parameter is self.
2:17
You might be forgetting that you've you might want to put something there. So the tooling does help but you got to remember to do it.