浅拷贝
person = ['name',['savings',100000.00]] hubby = person[:] wifey = list(person) hubby[0] = 'ljh' wifey[0] = 'yy' print hubby,wifey >>['ljh', ['savings', 100000.0]] ['yy', ['savings', 100000.0]] hubby[1][1] = 50000.00 print hubby,wifey
>>['ljh', ['savings', 50000.0]] ['yy', ['savings', 50000.0]]
深拷贝
import copy person = ['name',['savings',100000.00]] hubby = person wifey = copy.deepcopy(person) hubby[0] = 'ljh' wifey[0] = 'yy' print hubby,wifey hubby[1][1] = 50000.00 print hubby,wifey
ps:
1、非容器类型(数字、字符串、原子类对象,比如代码、类型和xrange对象等)没有被拷贝这么一说,浅拷贝是用完全切片操作来完成的。
2、如果元组只包含原子类对象,那么对它的深拷贝将不会进行,及时操作使用了深拷贝,也只能得到一个浅拷贝。
import copy person = ['name',('savings',100000.00)] newPerson = copy.deepcopy(person) [id(x) for x in person,newPerson] [id(x) for x in person] [id(x) for x in newPerson]