Python深拷贝与浅拷贝:只会发生在容器里包含可变数据容器类型的情况
浅拷贝可能会造成修改拷贝之后的值,会改变原来的值
>>> a={"name":"sc","score":[80,90,100]}
>>> b=a.copy()
>>> a
{'name': 'sc', 'score': [80, 90, 100]}
>>> b
{'name': 'sc', 'score': [80, 90, 100]}
>>> b["score"].append(110)
>>> a
{'name': 'sc', 'score': [80, 90, 100, 110]}
>>> b
{'name': 'sc', 'score': [80, 90, 100, 110]}
>>> id(a["score"])
140622729707336
>>> id(b["score"])
140622729707336
>>> id(b)
140622729725560
>>> id(a)
140622729724120
浅拷贝只会拷贝第一层对象的地址(引用)
深拷贝就是拷贝每一层的数据(使用copy模块的deepcopy函数)
>>> import copy
>>> b=copy.deepcopy(a) 进行深拷贝
>>> a
{'name': 'sc', 'score': [80, 90, 100, 110]}
>>> b
{'name': 'sc', 'score': [80, 90, 100, 110]}
>>> id(a["score"])
140622729707336
>>> id(b["score"])
140622722255816
>>> id(b["name"]) “sc”进入了字符串驻留区,所以id相同
140622729721424
>>> id(a["name"])
140622729721424
>>> b["score"].append(120)
>>> b
{'name': 'sc', 'score': [80, 90, 100, 110, 120]}
>>> a
{'name': 'sc', 'score': [80, 90, 100, 110]}