对于数字和字符串来说,无论是‘’赋值‘’还是‘’深拷贝‘’还是‘’浅拷贝‘’都是指向的同一个地址
深浅拷贝是copy类下的方法,创建方式为
import copy copy.copy() #浅拷贝 copy.deepcopy() #深拷贝
浅拷贝:
只拷贝一层
import copy dic = {'k1' : 'nihao','k2' : 23,'k3' : ['hello', 89, ]} print(id(dic)) dic1 = dic print(id(dic1)) dic2 = copy.copy(dic) print(id(dic2)) print(id(dic['k3'])) #查看字典第二层的地址 print(id(dic2['k3']))
结果:
5012504 5012504 5012576 33180616 33180616
深拷贝:
深层全部都拷贝
import copy dic = {'k1' : 'nihao','k2' : 23,'k3' : ['hello', 89, ]} print(id(dic)) dic1 = dic print(id(dic1)) dic2 = copy.deepcopy(dic) #深拷贝 print(id(dic2)) print(id(dic['k3'])) print(id(dic2['k3']))
结果:
31095832 31095832 31889304 32328648 32328840
浅拷贝更加节省内存