[Python] attributes in a class

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on an example of a class, called Car. Can you help clarify the difference between the attributes after the initialize 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?

    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.")```

    Continue reading...

Compartilhe esta Página