I would like to loop through a dataclass in python, to find all cars, which fit a certain criteria, however, the code never works, because the variable gets interpreted as an attribute, and not a variable.
from dataclasses import dataclass
@dataclass
class cars:
year: int = 0
model: str = "unknown"
PS: int = 0
colour: str = "unknown"
car_1 = cars(year = 1980, colour = "brown")
car_2 = cars(year = 1999, colour = "black", PS = 82)
owned_cars = [car_1, car_2]
criteria = input("Which criteria to search for? year, model, PS, colour")
for car in owned_cars:
value = car.criteria
print(value, car)
AttributeError: 'cars' object has no attribute 'criteria'
When using:
value = car.year
the code runs fine. How can I tell python, it should interpret criteria as the variable and use its content, instead of its name?