给出以下示例代码:
class Parent:
some_list = [] # type: list[int]
def __init__(self, id):
self.id = id
if __name__ == '__main__':
list_of_parents = [] # type: list[Parent]
# Create 10 parents
for i in range(10):
list_of_parents.append(Parent(i))
# Go over each parent and add 10 integers to each of their lists
for i, p in enumerate(list_of_parents):
for j in range(10):
list_of_parents[i].some_list.append(1)
执行该代码后,将会有一个包含10个Parent
个对象的列表,每个对象都有一个包含100个条目的列表。 (包含10次整数0
- 9
),即使这些j
元素仅明确添加到list_of_parents[i]
。
原因是什么?
答案 0 :(得分:1)
这就是Moses Koledoye已经提到过的。尝试这样的事情,你会看到差异:
class Parent:
def __init__(self, id):
self.id = id
self.some_list = []# type: list[int]
if __name__ == '__main__':
list_of_parents = [] # type: list[Parent]
# Create 10 parents
for i in range(10):
list_of_parents.append(Parent(i))
# Go over each parent and add 10 integers to each of their lists
for i, p in enumerate(list_of_parents):
for j in range(10):
list_of_parents[i].some_list.append(1)
print(list_of_parents[i].some_list)
print("__________")
输出:
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________