I have an object
class Car:
def __init__(self):
price = float(0)
Then another
class Day:
def __init__(self):
self.carList = [Car() for each in range(100)]
self.createPriceList()
def createPriceList(self):
tempCar = Car()
for i in range(100):
tempCar.price = function_giving_a_value() # 10 last cars have 0.0 as value
self.carList[i] = tempCar
print i, self.carList[i].price
# prints the correct list : each line contains a correct price
#edited after answers : in fact it's just misleading, cf answers
def showPriceList(self):
for i in range(len(self.carList)):
print i, self.carList[i].price
# prints i (correct) but each self.carList[i].price as 0.0
# so len(self.carList) gives correct value,
# but self.carList[i].price a wrong result
My question is :
- Why in
showPriceList()
,self.carList
is correctly recognized (len
gives the correct number in looping) butself.carList[i].price
gives only zeros? (when it seems correctly filled in methodcreatePriceList()
)