我尝试使用python列表
a = [1,2,3]
id(a)
3072380812L
a += [1]
print id(a)
3072380812L # Same id, which means original list is modified
a = a + [1]
print id(a)
146238764 # Different id, which means new list is allocated and assigned to a
为什么python列表的“var + = value”和“var = var + value”之间存在差异?
答案 0 :(得分:3)
+=
修改(如果可变)......正如您所看到的那样,=
也按照您的看法进行了分配......
这两个运算符在类中都是可以覆盖的,并且它们的行为也可以在开发人员的心血来潮中改变...如果你想...你可以做=求和...