python - Python3, create multiple objects of a class at once -
i want create multiple objects diferent names @ once.
for example
class car: def __init__(self, color): self.color = color
and need create n objects when program first run
car_1 = car("blue") car_2 = car("red") #... car_n = car("color_n")
is there way in python3? things i've tried create 1 object , change name or overwhite objects information o fails @ running. can't use exec() or eval()
thanks
extra: need thing before because need store n client's information whenever run program work (that info stored in .csv file). need thing mentioned before or there way deal kind of data management?
you can create list of cars objects, list comprehension
class car: def __init__(self, color): self.color = color def __repr__(self): return '{}({!r})'.format(self.__class__.__name__, self.color) colors=['red', 'blue', 'green',] cars_objects = [car(color) color in colors] print(cars_objects)
return
[car('red'), car('blue'), car('green')]
__repr__
function in car class creates "more readable" (and parseable) representation of car object instead of "<__main__.car object @ 0x7f5b38a2c400>
"
Comments
Post a Comment