python中的变量是引用。很少有引用指向不可变对象(例如字符串、整数等),而列表和集合是可变的。
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# Consider two variables x & y with integers 23 and 46. They have immutable references
# Meaning, if y=x, it doesn't mean y will change whenever x is updated.
>>> x=23
>>> print (x)
23
>>> y=46
>>> print (x)
23
>>> print (y)
46
>>> y=x
>>> print (x)
23
>>> print (y)
23
# Let's change the value of x after the assignment
>>> x=99
>>> print (x)
99
# Since it is immutable, the value wont change.
>>> print (y)
23
#Let's consider the mutable reference scenario. a & b are two lists which have mutable references
# Meaning, if b=a, it means b will change whenever a is changed
>>> a = list([11,22,33,87])
>>> print (a)
[11, 22, 33, 87]
>>> b = list([87,53,98,2])
>>> print (b)
[87, 53, 98, 2]
>>> a=b
>>> print (a)
[87, 53, 98, 2]
>>> print (b)
[87, 53, 98, 2]
# Popping the list would alter b
>>> b.pop()
2
>>> print (b)
[87, 53, 98]
# Notice the change in value of a
>>> print (a)
[87, 53, 98]
>>>