In Python 3, I have a list of classes, and each class has a list in it. I'm having difficulties updating those lists. As so:
class Item():
newPrice = 0
prices = [0]
def __init__(self, _prices):
self.prices = _prices
items = [Item([10]), Item([20]), Item([30])]
for item in items:
item.newPrice = SomeFunction()
item.prices.append(item.newPrice)
The function SomeFunction()
is an elaborate function that retrieves a different value for each Item
instance.
For some reason, each Item
instance in the items
list has the same value for prices
, which is a list that contains the newPrice
value for each Item
instance. I hope this is clear enough.
What am I missing?