I have below Car class. I would like to be able to reset the mileage for all car class instances to 0. How can I do this?
class Car():
carpark = []
# initialising instance
def __init__(self, brand, color, fuel, mileage):
self.brand = brand
self.color = color
self.fuel = fuel
self.mileage = mileage
self.drives = []
Car.carpark.append(self)
@classmethod
def purchase(cls, brand, color):
cls(brand, color, "Diesel", 0)
Since I have the carpark list, i could do something like:
@classmethod
def reset_mileage(cls):
for car in cls.carpark:
car.mileage = 0
This works but I am wondering if there is a better, cleaner way to do this?
reset_mileagefunction in theCarclass which sets its mileage to 0. And then call that for each car in the collection. It's usually not a good idea for a class instance to control/modify the collection it is a part of.