copy.copy()vs copy.deepcopy():id(对象)没有变化

时间:2015-04-14 19:53:00

标签: python deep-copy shallow-copy

import copy

a = [12, 13, 15, 15, 14, 10]

d = copy.deepcopy(a)

print(id(d) == id(a))
print(id(d[0]) == id(a[0]))

e = copy.copy(a)

print(id(e) == id(a))
print(id(e[0]) == id(a[0]))

#outputs
False
True
False
True

我正在使用Python 3.4并且根据What exactly is the difference between shallow copy, deepcopy and normal assignment operation?,对于d我应该得到输出FalseFalse,对于e我应该得到输出{ {1}}和False

为什么会发生这种情况?

我将True更改为a,然后选中[12, 13, 15, 15, 14, [10, 12]] vs id(d[5])后ID就不同了。那么,为什么id(a[5])d[0]的ID不同?

0 个答案:

没有答案