Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Adding an Instance Method

00:00 The task was to add an instance method called .drive() that takes some miles as an argument. Then that should increment self.mileage for how many miles you passed in and then test it. So I want to define .drive(), and this instance method won’t be a special method like the first two with the double underscores.

00:27 But this is just a method that is public to the Car class and that I’m going to define now. And still, it takes self as its first argument because I want to do something on the instance of the Car, and then it also takes another argument.

00:42 So I put in the parameter miles in the method definition, and that just means that when calling, you have to pass in miles. That’s going to be a number. And then what do you want to do with this?

00:56 You want to increment self.mileage. So because you’re passing self here and because this is a method on an instance of a Car, you have access to this self object.

01:08 So I can say self.mileage and then just increment it. So I’m going to say += miles. So this is going to take the miles that someone passes when calling .drive() and adds it to self.mileage.

01:24 And you can also do just = self.mileage + miles. That’s the same. But I’m going to go for the shorter version. This is a shortcut to saying .self.mileage + miles.

01:42 I think that should be it. Let’s go ahead and test it. So I will press F5, run it, and go into the interactive console. And then I’m just going to call it car.

01:57 Car() and it will be "blue"

02:01 and have 0 miles. Actually, just for fun, this car is going to be "white", 0 miles. Okay, so we have this Car object. If I print(car), The white car has 0 miles. And now I should be able to say

02:20 I’m going to drive this car for a hundred miles, no output. But something happened in here because now if I print(car), it tells me that The white car has 100 miles. So with this instance method here, with .drive(), I accessed an instance attribute, .mileage, and changed it basically with whatever I input here as an argument. So that works.

02:46 I can continue driving the white car, .drive() for, let’s drive it for another a hundred miles, and then it should

02:57 have 200 miles on there. Perfect.

03:02 So good job if you came up with something similar to this, and if you have a different solution, post it in the comments. I’m curious to see.

DimaG on Oct. 19, 2023

This is my version of the Car class written using dataclass syntax and operator.methodcaller():

from dataclasses import dataclass
from operator import methodcaller

@dataclass(slots=True, kw_only=True)
class Car:
    """Class representing car."""

    color: str
    mileage: int

    def __str__(self):
        """Returns string representation of car."""
        return f"The {self.color} car has {self.mileage:,} miles."

    def drive(self, miles: int) -> None:
        self.mileage += miles

blue_car = Car(color="blue", mileage=20_000)
red_car = Car(color="red", mileage=30_000)
white_car = Car(color="white", mileage=0)

cars = [blue_car, red_car, white_car]
get_mileage = methodcaller("drive", 100)
get_more_mileage = methodcaller("drive", 100)

for car in cars:


for car in cars:

Martin Breuss RP Team on Oct. 25, 2023

@DimaG thanks for sharing your code using a dataclass and the operator module! :D

Two small notes:

You don’t need get_more_mileage(), it has the same functionality as get_mileage, so you could skip that and just call get_mileage() another time in your second for loop.

Secondly, I’d say that get_mileage() isn’t a very descriptive name for the functionality that you’re encoding here. You’re creating a callable that’ll call .drive() with an argument of 100. Can you come up with a more descriptive name for it?

Nice work expanding on the topics taught in the course! 🙌

Become a Member to join the conversation.