Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果。
首先,对赋值操作我们要有以下认识:
- 赋值是将一个对象的地址赋值给一个变量,让变量指向该地址( 旧瓶装旧酒 )。
- 修改不可变对象(
str
、tuple
)需要开辟新的空间 - 修改可变对象(
list
等)不需要开辟新的空间
浅拷贝
? 浅拷贝仅仅复制了容器中元素的地址,就是拷贝了引用,并没有拷贝内容。
In [1]: a = [11,22,33]
In [2]: id(a)
Out[2]: 22132096
In [3]: b = a
In [4]: id(b)
Out[4]: 22132096 # 引用相同
In [5]: a.append(44)
In [6]: a
Out[6]: [11, 22, 33, 44]
In [7]: b
Out[7]: [11, 22, 33, 44] # b的值也变化
深拷贝
深拷贝是对于一个对象所有层次的拷贝(递归)
In [8]: import copy
In [9]: a = [11, 22, 33]
In [10]: id(a)
Out[10]: 74598640
In [11]: b = copy.deepcopy(b)
In [12]: id(b)
Out[12]: 79142624
In [13]: a.append(44)
In [14]: a
Out[14]: [11, 22, 33, 44]
In [15]: b
Out[15]: [11, 22, 33, 44]
进一步理解
In [23]: a = [11,22,33]
In [24]: b = [44,55,66]
In [25]: c = (a,b)
In [26]: e = copy.deepcopy(c)
In [27]: a.append(77)
In [28]: a
Out[28]: [11, 22, 33, 77]
In [29]: b
Out[29]: [44, 55, 66]
In [30]: c
Out[30]: ([11, 22, 33, 77], [44, 55, 66])
In [31]: e
Out[31]: ([11, 22, 33], [44, 55, 66])
# copy.copy()方法
In [32]: f = copy.copy(c)
In [33]: a.append(88)
In [34]: a
Out[34]: [11, 22, 33, 77, 88]
In [35]: b
Out[35]: [44, 55, 66]
In [36]: c
Out[36]: ([11, 22, 33, 77, 88], [44, 55, 66])
In [37]: e
Out[37]: ([11, 22, 33], [44, 55, 66])
In [38]: f
Out[38]: ([11, 22, 33, 77, 88], [44, 55, 66])
拷贝的其他方式
浅拷贝对不可变类型和可变类型的copy不同
In [88]: a = [11,22,33]
In [89]: b = copy.copy(a)
In [90]: id(a)
Out[90]: 59275144
In [91]: id(b)
Out[91]: 59525600
In [92]: a.append(44)
In [93]: a
Out[93]: [11, 22, 33, 44]
In [94]: b
Out[94]: [11, 22, 33]
In [95]: a = (11,22,33)
In [96]: b = copy.copy(a)
In [97]: id(a)
Out[97]: 58890680
In [98]: id(b)
Out[98]: 58890680
- 分片表达式可以赋值一个序列
a = "abc"
b = a[:]
- 字典的copy方法可以拷贝一个字典
d = dict(name="zhangsan", age=27)
co = d.copy()
- 有些内置函数可以生成拷贝(list)
a = list(range(10))
b = list(a)
- copy模块中的copy函数
import copy
a = (1,2,3)
b = copy.copy(a)