时间:2021-07-01 10:21:17 帮助过:40人阅读
import copy # ######数字、字符串###### n1 = 123 print(id(n1)) n2 = n1 print(id(n2)) ###浅拷贝### n2 = copy.copy(n1) print(id(n2)) ###深拷贝### n3 = copy.deepcopy(n1) print(id(n3))
C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe C:/homework/day3/1.py
492322480
492322480
492322480
492322480
Process finished with exit code 0
2、字符串
import copy # ######数字、字符串###### n1 = "my name is hellworld" print(id(n1)) n2 = n1 print(id(n2)) ###浅拷贝### n2 = copy.copy(n1) print(id(n2)) ###深拷贝### n3 = copy.deepcopy(n1) print(id(n3))
C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe C:/homework/day3/1.py
5973920
5973920
5973920
5973920
Process finished with exit code 0
import copy # ######数字、字符串###### n1 = ["helloworld","hapen"] print(id(n1)) n2 = n1 print(id(n2)) ###浅拷贝### n2 = copy.copy(n1) print(id(n2)) ###深拷贝### n3 = copy.deepcopy(n1) print(id(n3))
C:\Users\811314\AppData\Local\Programs\Python\Python35-32\python.exe C:/homework/day3/1.py
12817912
12817912
10370784
12816672
Process finished with exit code 0
对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。
浅拷贝,在内存中只额外创建第一层数据
深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化
以上就是python学习日记的详细内容,更多请关注Gxl网其它相关文章!