Sunday, September 11, 2016

我应该多读一下评论……

MoonKnight在我一年前的一则post里说:

List, dictionary, set, tuple etc. are all objects (classes) in python. When copying an object in python, the default way is to do a "shallow copy". 
Basically x=y just copy the reference (or pointer if you are more familiar with C) of x to y, so now they point to the same address in your memory. Thus change one will automatically change the other.If you want a "deep copy" (a separate copy with a different memory address) for a list, you can do x=list(y). There is also a package called copy, with a function called deepcopy, so x=copy.deepcopy(y) should do the same.On the other hand, the difference of "shallow copy" and "deep copy" only exists for objects. If you are copying an integer, string, boolean etc. It can only create a deep copy.

我要是早点看到就没今天这个问题了……

No comments: