可能重複:
What does plus equals (+=) do in Python?l1是一個列表,l2 = l1;爲什麼「l2 = l2 + [item]」會影響l1而「l2 + = [item]」呢?
我注意到一個奇怪的問題:
l1 = ['1', '2', '3']
l2 = l1
item = l2.pop(0)
# the pop operation will effect l1
print l1
l2 = l2 + [item]
# why "l2 = l2 + [item]" does't effect l1 while "l2 += [item]" does.
print l2
print l1
輸出是:
['2', '3']
['2', '3', '1']
['2', '3']
但是如果我改變l2 = l2 + [item]
爲l2 += [item]
,輸出將是:
['2', '3']
['2', '3', '1']
['2', '3', '1']
這是怎麼回事?這只是它的方式。 – phant0m