1.浅拷贝
>>> import copy
>>> a=[{'x':1}]
>>> a
[{'x': 1}]
>>> b=copy.copy(a)
>>> id(a)
29189864
>>> id(b)
29186288
>>> a[0]['x']=2 #Change a
>>> a
[{'x': 2}]
>>> b #b was changed
[{'x': 2}]
##################################
>>> a={'x':1}
>>> import copy
>>> a
{'x': 1}
>>> b=copy.copy(a)
>>> id(a)
29203744
>>> id(b)
29012304
>>> a
{'x': 1}
>>> b
{'x': 1}
>>>
>>> a['x']=2
>>> a
{'x': 2}
>>> b #No changed
{'x': 1}
2.
>>> a=[[1,2,3],{'x':1}]
>>> b=copy.copy(a)
>>> id(a)
29191704
>>> id(b)
29186248
>>>
>>> a
[[1, 2, 3], {'x': 1}]
>>> b
[[1, 2, 3], {'x': 1}]
>>> a[0][0]=3
>>> a
[[3, 2, 3], {'x': 1}]
>>> b
[[3, 2, 3], {'x': 1}]
>>> a[1]['x']=2
>>> a
[[3, 2, 3], {'x': 2}]
>>> b
[[3, 2, 3], {'x': 2}]
###############################
>>> a=[1,2,3]
>>> b=copy.copy(a)
>>> id(a)
29191064
>>> id(b)
29011800
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a[0]=3
>>> a
[3, 2, 3]
>>> b
[1, 2, 3]
3.Deepcopy
>>> a=[1,2,3]
>>> b=[]
>>> for i in range(2):
b.append(a)
>>> b
[[1, 2, 3], [1, 2, 3]]
>>> a[0]=3
>>> b
[[3, 2, 3], [3, 2, 3]] #浅拷贝
#######################
>>> for i in range(2):
b.append(deep.copy(a))
>>> b
[[3, 2, 3], [3, 2, 3], [3, 2, 3], [3, 2, 3]]
>>> a
[3, 2, 3]
>>> a[0]=4
>>> b
[[4, 2, 3], [4, 2, 3], [3, 2, 3], [3, 2, 3]]##########b[3],b[4]是深拷贝
第一,非容器类型(比如数字,字符串和其他"原子"类型的对象,像代码,类型和 xrange 对象等)没有被拷贝一说,浅拷贝是用完全切片操作来完成的.
第二,如果元组变量只包含原子类型对象,对它的深拷贝将不会进行.如果我们把账户信息改成元组类型,那么即便按我们的要求使用深拷贝操作也只能得到一个浅拷贝: