Python 更新类中的列表,在列表中

Python 更新类中的列表,在列表中,python,list,class,python-3.x,Python,List,Class,Python 3.x,在Python3中,我有一个类列表,每个类中都有一个列表。我很难更新那些列表。因此: 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 = SomeFunctio

在Python3中,我有一个类列表,每个类中都有一个列表。我很难更新那些列表。因此:

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)
函数
SomeFunction()
是一个复杂的函数,它为每个
实例检索不同的值

出于某种原因,
项目
列表中的每个
项目
实例对于
价格
具有相同的值,该列表包含每个
项目
实例的
新价格
值。我希望这足够清楚


我缺少什么?

您应该将价格定义为实例属性,而不是类属性,因为类属性在所有实例之间共享:

class Item():

    def __init__(self, _prices):
        self.newPrice = 0
        self.price = _prices

您应该将价格定义为实例属性,而不是类属性,因为类属性在所有实例之间共享:

class Item():

    def __init__(self, _prices):
        self.newPrice = 0
        self.price = _prices

你知道,一开始我不这么认为,因为我不太明白那张海报在问什么,但在读了尤金的答案后,它变得更有意义了。你说得对。谢谢。你知道,一开始我不这么认为,因为我不太明白那张海报在问什么,但在读了尤金的答案后,它变得更有意义了。你说得对。谢谢。我真不敢相信事情会这么简单。我知道我错过了什么。谢谢我不敢相信事情竟那么简单。我知道我错过了什么。谢谢