I have created an object type.. and after initilializing I am pushing that into a list.
but for some reason the behavior is not as expected.
let me put in the sample code.. and then the output.
def allignDatesToWhenWasItemSold(pilotInstance):
unitsSoldPerDay = pilotInstance._units_sold_per_day
productPart = pilotInstance._product
date = productPart._date
quantity = pilotInstance._product._quantity
listOfPilotInstance = []
for i in range(len(unitsSoldPerDay)):
perDayQuantity = unitsSoldPerDay[i]
#modDate = date
#print perDayQuantity
modDate = modifyDate(date, i)
productPart._date = modDate
#print "pro ", productPart._date
newPilotInstance = PilotTest(productPart, pilotInstance._name,perDayQuantity)
print "here ",newPilotInstance._product._date._date, ' ',newPilotInstance._product._date._month, ' ', newPilotInstance._units_sold_per_day
#newPilotInstance.setDate(modDate)
listOfPilotInstance.append(newPilotInstance) #note this line.. this is where the trouble is
for k in listOfPilotInstance:
print k._product._date._date
for ele in listOfPilotInstance:
print "there " ,ele._product._date._date, ' ',ele._product._date._month, ' ',ele._units_sold_per_day
return listOfPilotInstance
The out put are as following
here 30 7 1
30
here 31 7 0
31<--- now this shouldnt be like this.. as I am doing append.. teh first ele shoulnt be overwrited??
31
here 1 8 2
1
1
1
there 1 8 1
there 1 8 0
there 1 8 2
So my query is that since I am doing the append.. why is the date element getting overwrited?
Any clue as in what I am doing wrong?
Thanks
You are using the same productpart instance, and just mutating it:
Since all the
PilotTestobjects have a reference to the sameproductPartinstance, all of them see the mutation.You need to create a new instance of your class on each iteration through the loop and assign this new instance to
productPart.