【发布时间】:2022-11-05 07:10:07
【问题描述】:
我正在尝试在 python 中创建一个新对象列表(如在单独的内存位置,而不是引用)
class example:
_name = ""
_words = []
def __init__(self,name):
_name = name
def add_words(self,new_words):
for i in new_words:
self._words.append(i)
examples = [example("example_1"),example("example_2"),example("example_3")]
examples[0].add_words(["test1","test2"])
print(examples[2]._words)
print(examples[2]._words) 输出 ["test1","test2"]
我期待[],因为我只在examples[0] 中添加了单词
【问题讨论】:
标签: python oop memory reference