这是因为整数是不可变的,而列表是可变的。
>>> i = 0
>>> t = (i,)
>>> t[0] is i # both of them point to the same immutable object
True
>>> i += 1 # We can't modify an immutable object, changing `i` simply
# makes it point to a new object 2.
# All other references to the original object(0) are still intact.
>>> i
1
>>> t # t still points to the same 0
(0,)
>>> x = y = 1
>>> id(x),id(y)
(137793280, 137793280)
>>> x += 1
>>> id(x),id(y) #y still points to the same object
(137793296, 137793280)
对于列表:
>>> l = [0]
>>> t = (l,)
>>> t[0] is l #both t[0] and l point to the same object [0]
True
>>> l[0] += 1 # modify [0] in-place
>>> t
([1],)
>>> l
[1]
#another exmple
>>> x = y =[] # x, y point to the same object
>>> x.append(1) # list.append modifies the list in-place
>>> x, y
([1], [1])
>>> x = x + [2] # only changes x, x now points to a new object
>>> x, y
([1, 2], [1])