python深浅拷贝
1.对于字符串、数字:
不管深浅拷贝,拷贝、赋值,他们的地址都是一样的(因为python的优化机制)
import copy n1 = [11,22,33] n2 = n1 n3 = copy.copy(n1) print(id(n1)) print(id(n2)) print(id(n3)) #13053256 #13053256 #13053256
2.对于列表、字典、元祖:
浅拷贝:只拷贝最外面一层
深拷贝:除了最内层,其他都拷贝一遍
import copy n1 = {"k1":11,"k2":22} n2 = copy.copy(n1) n3 = copy.deepcopy(n1) print(id(n1)) #15779336 print(id(n2)) #18230280 print(id(n3)) #18200328 print(id(n1["k1"])) #1546544256 print(id(n2["k1"])) #1546544256 print(id(n3["k1"])) #1546544256