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.
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
You call it
You can see the difference:
The former passes the reference
The latter is a copy
b=a
會讓b
參考到a
Reference objecta[:]
會製造一個 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
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