How add add class instances to a list

Clash Royale CLAN TAG#URR8PPP
How add add class instances to a list
I'm trying to get a bit better in classes. I want to add multiple cars (with attributes like plate number and time (it is for a security cam)) to a list. I had it working actually, but now it suddenly doesn't and I have no idea what is going on.
class CarID:
def __init__(self, camera, cartime, plate):
self.camera = camera
self.plate = plate
self.cartime = cartime
class Cars:
def __init__(self):
self.cars =
def addCar(self, car):
self.cars.append(car)
Main Program:
def make_lists_cars(data):
car_list = Cars()
with open(data, newline='') as f:
f = f.readlines()
for line in f[2:-2]:
car = line.split("t")
camera = car[0]
cartime = car[1]
plate = car[2]
car_list.addCar(CarID(camera, cartime, plate))
return car_list
data = 'verkeer.txt'
car_list = make_lists_cars(data)
print(Cars().cars)
This gives me just an empty list. The car variables all look like ['1', '09:01:53', '2-ABC-32n']. I actually want to have a list with cars with attributes.
I had it working a couple of hours ago, but then I wanted to add a second list and now it stopepd working :(.
Also do I have to return the car_list, or is this not necessary?
Thanks
1 Answer
1
Cars().cars is accessing the attribute cars from a new class Cars(). Cars() in the last line of your code has just been initialized, and no CarId objects have been added to the cars attribute of Cars(). Instead, lookup cars from the returned result of make_lists_cars:
Cars().cars
cars
Cars()
Cars()
CarId
cars
Cars()
cars
make_lists_cars
print(car_list.cars)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I would suspect the file parsing.
– Stephen Rauch
2 mins ago