浅拷贝可以拿丈夫与媳妇公用一张银行卡来举例
# python >>> husband = ['liang',123,[10000,6000]] #丈夫的银行卡信息(姓名,id,额度,余额) >>> wife = husband.copy() #媳妇和丈夫公用一张银行卡(姓名与id不同,但是额度与余额相同) >>> wife[0] = 'yue' >>> wife[1] = 456 >>> wife[2][1] -= 3000 #媳妇花费3000 >>> wife ['yue', 456, [10000, 3000]] #显示媳妇余额 >>> husband ['liang', 123, [10000, 3000]] #显示丈夫余额
浅拷贝只是拷贝第一层,深拷贝相当于克隆,深拷贝如下:
引入了小三,小三花费的钱媳妇不知道: >>> liang = ['xin',111,[15000,10000]] >>> yue = liang.copy() >>> yue ['xin', 111, [15000, 10000]] >>> yue[0] = 'mumu' >>> yue[1] = 222 >>> yue[2][1] -= 5000 >>> yue ['mumu', 222, [15000, 5000]] >>> liang ['xin', 111, [15000, 5000]] 以上是浅拷贝; 以下是深拷贝: >>> import copy >>> xiaosan = copy.deepcopy(liang) >>> xiaosan[0] ="**" >>> xiaosan[1] = 333 >>> xiaosan[2][1] -= 1999 >>> xiaosan ['**', 333, [15000, 3001]] #已经减少啦 >>> liang ['xin', 111, [15000, 5000]] >>> yue ['mumu', 222, [15000, 5000]] #媳妇不知道