迷茫2017-04-18 09:25:15
You call it
b.append(6)
print (a, b)
You can see the difference:
The former passes the reference
The latter is a copy
大家讲道理2017-04-18 09:25:15
b=a
會讓 b
參考到 a
Reference object
a[:]
會製造一個 a 的副本, 所以 b=a[:]
會讓 b
參考到這個副本, 也就是說 b
跟 a
Now refer to different objects, but these two objects are equal ( not the same but equivalent )
This is why on the surface there seems to be no difference between the two, but if it is the former, we have changed b
或 a
Both will be affected because they refer to the same object, but the latter does not affect each other because they refer to different objects. . @hsfzxjy's example is to point this out, and you can learn the difference by doing experiments.
Questions I answered: Python-QA
PHP中文网2017-04-18 09:25:15
Python has a function called id that can get the address of an object. It will be clear if you print it out. Direct = is reference assignment, referencing the original object; and [:] is re-derivation, which will generate a new object
a=[1,2,3,4,5]
b=a
c=a[:]
print id(a),id(b),id(c)