python浅拷贝与深拷贝

import copy
husband=['xiaohu',123,[15000,9000]]

wife=husband.copy()   #浅拷贝,wife=copy.copy(husband),只复制第一层,修改第一层不会互相影响,但是第二层会有影响
wife[0]="xiaopang"
wife[1]=345
husband[2][1]=3000 #老公花了6000
print(wife,husband)

xiaosan=copy.deepcopy(husband)  #深拷贝,重新开辟了一段新的内存空间,相当于克隆,修改都不影响,
xiaosan[0]='jinxing'
xiaosan[1]=666
xiaosan[2][1] =7000

xiaomei=wife   #两边改都会影响,内存共享了
xiaomei[0]='meimei'
xiaomei[2][1]=100
print(xiaomei,wife)

 

你可能感兴趣的:(编程)