Python (Fall 2026) Module 14-3: Class Methods
Overview
Bill Moseley demonstrates how a Python class method uses `self` to update state belonging to a particular object: `driveCar(intDistance)` adds miles to that car's `intMiles` property. Two calls driving 100 and 38 miles show the odometer accumulating to 138, while separate `clCar` instances maintain independent mileage; Moseley also explains that `pickle` can preserve object data between program runs.
Key takeaways
- In `driveCar(self, intDistance)`, Python supplies `self` automatically when the method is called as `obCar1.driveCar(100)`, so the caller passes only `100`.
- The expression `self.intMiles += intDistance` updates the mileage stored on the current car instance rather than a local or global variable.
- Calling `driveCar` with 100 and then 38 adds the distances cumulatively, leaving the car's mileage at 138 for the duration of the program.
- Two objects created from `clCar()` keep independent `intMiles` values; changing one car's odometer does not change the other's.
- Object state normally disappears when the program ends, but Python's `pickle` can serialize an object to a file and reload its data later.
Chapters
- Moseley introduces `clCar.driveCar(self, intHowFar)`, where `self` identifies the specific car instance.
- The method increases that object's `self.intMiles` by the distance supplied as its other parameter.
- Python passes `self` automatically when calling a method through an object, so callers provide only the driving distance.
- The class initializes `intMiles` to 0 and defines `driveCar(self, intDistance)` with the method indented inside the class.
- Using `self.intMiles += intDistance` updates the object's property; `self` distinguishes it from a local or global variable.
- Driving `obCar1` 100 miles and then 38 miles prints totals of 100 and 138, demonstrating persistent instance state while the program runs.
- Moseley notes that `pickle` can save an object and its data to a file for reloading in a later program run.
- Each call to `clCar()` creates a car with its own odometer, so driving `obCar1` does not change a second car's mileage.
- `self.intMiles` refers to the current instance's property, while `intDistance` refers directly to the method parameter.
- Names such as `myCar` and `obCar` are programmer choices; the object is an instance created from the `clCar` class.
Transcript excerpt
0:01 Classes also define methods. And just going with our car example here, here's another car class. And it's got an integer property called miles. intMiles, and it, it starts out with 100 miles. It also has, you can see now, inside. So this method, or this function that I'm creating, is a method because it's part of a class. So when I say int, or when I say clCar.driveCar, I'm going to give it two arguments. Every method has self in it. And the, what that means is, when we pass self to a method, what it does is it allows us to refer to that instance so that
1:02 we can access the properties of that instance. The other parameter here is intHowFar. So it's, how far do I want to drive? So I'm going to tell it to drive a certain distance. And this is passed invisibly. So that's just going to be there. But you have to put it when you define the function. But you can see down here, I don't have to give that when I call the function or call that method. So inside this method, I've got self.intMiles, and I'm adding that argument. So whatever I put in there and goes to that property gets added to the intMiles, self.intMiles. So it will increase that by however much I put here, and then it, all it does is just
1:55 print some stuff. So I, if I create a instance of that car and then I drive that car 50 miles, the the property intMiles will be increased by 50. So let's come back to this. I'm going to delete this, and I'm just going to add to this, to this class, and I'm going say intMiles, and I'll start out at zero. And now I'm going to create that driveCar method. So let's say def driveCar.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.