深浅拷贝

#_*_coding:utf-8_*_
#作者:王佃元
#日期:2019/12/20
import copy
#深拷贝 = 克隆一份
#浅拷贝 = 只拷贝第一层
husband = ['zhangshan', 123, [15000, 9000]]
wife = husband.copy()
mistress = copy.deepcopy(husband)
husband[2][1] -= 1000
print(husband)
wife[0] = 'xiaohua'
wife[1] = 456
wife[2][1] -= 2000
print(wife)
mistress[0] = 'xiaosan'
mistress[1] = 666
mistress[2][1] -= 500
print(mistress)
运行结果:

C:\Users\dery\AppData\Local\Programs\Python\Python38\python.exe C:/dery/PythonStudy/SecWeek/day6/shallow_deep_copy.py
['zhangshan', 123, [15000, 8000]]
['xiaohua', 456, [15000, 6000]]
['xiaosan', 666, [15000, 8500]]

Process finished with exit code 0

你可能感兴趣的:(深浅拷贝)