您的問題是由以下事實引起的,即記錄器中字典中的值的許多列表實際上都引用了相同的list_of_objects.將其與此代碼進行比較:x = [1,2,3] # x refers to a listy = x # y is another reference to the same listx.append(4) # modify the list through xprint(y) # prints [1, 2, 3, 4], even though we accessed via y您的代碼正在做同樣的事情,但不是像xand這樣的簡單變量,而是y通過屬性和字典值(Inventory.list_of_objects以及Inventory.logger.dict_of_positions[counter]每個counter值)引用列表。我不完全理解您的代碼應該做什么,但我懷疑您可以通過更改increase_counter為list_of_objects使用list構造函數創建列表的副本來避免此問題:def increase_counter(self): self.logger.add_list_at_counter(self.counter, list(self.list_of_objects)) # new list self.counter += 1
添加回答
舉報
0/150
提交
取消