-5

I am working on an example of a class called Car. Can you help clarify the difference between the attributes after the initialization statement and those within the indentation? How is odometer_reading different from the make, model, and year attributes in terms of how it can be modified?

class Car: 
    def __init__(self, make, model, year): 
        """Initialize attributes to describe a car."""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0 

    def get_descriptive_name(self): 
        """Return a neatly formatted descriptive name."""
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self): 
        """Print a statement showing the car's mileage."""
        print(f"This car has {self.odometer_reading} miles on it.")
1
  • 1
    I don't see any difference. Both are instance properties, both are initialized, both have the same access. The initialization to an immediate constant value (0) makes no difference. Commented Aug 25 at 2:50

2 Answers 2

2

The quick answer: there is no difference between self.odometer_reading and self.make, self.model, or self.year. They are all attributes that are being assigned on the self instance of the class, and are specific to that instance, not shared between instances. For example, if you had a method

    def drive(self, miles):
        """Add distance to the odometer"""
        self.odometer_reading += miles

then running the following code...

    family_car = Car('Wagon Queen', 'Family Truckster', 1979)
    teaser = Car('Ferarri', '308 GTS', 1982)
    family_car.drive(2000)

then family_car would have an odometer_reading of 2000 while teaser would have an odometer_reading of 0.

Sign up to request clarification or add additional context in comments.

Comments

2

In your Car , they are all instance attributes, belonging to each object created from the class.

There isn't actually any difference among them, but they differ only in terms of where their values come from initially.

The instance attributes make, model, yearare initialized using arguments passed in when you create a Car object.

for example:

car_obj = Car("Kia", "Carnival", 2024)
print(car_obj.make)   # Kia
print(car_obj.year)   # 2024
print(car_obj.odometer_reading) # 0

So each car can have its own make, model, and year set at object creation time.

On the other hand, the instance attribute odometer_reading isn’t passed as an argument when you create a Car object. Instead, every new Car object starts with the default odometer reading as 0 which can be later updated.

car_obj.odometer_reading = 500
print(car_obj.odometer_reading) # 500

So, in a nutshell, think of it like this:

  • make, model, and year describe the car’s identity and its values are supplied when the Car is created.

  • while odometer_reading describes the car’s state, which starts with a starting default value as 0 and changes over the car’s lifetime.

make, model, year are required because they’re parameters of the __init__ method and don’t have default values. That means you must pass them when creating a Car object.

car_obj = Car("Kia", "Carnival", 2024)  # works
car_obj = Car()  # TypeError: missing required arguments

odometer_reading is not an argument at all, it’s an instance attribute that’s always initialized to 0 inside __init__.So, you don’t need to pass it when you create the object.

1 Comment

Thank you this is such a helpful description!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.