Home >Backend Development >Python Tutorial >python learning diary
1. Numeric copy
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, string
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
For numbers and strings, assignment, shallow copy and deep copy are meaningless because they always point to the same memory address .
Shallow copy, only create the first layer of data in the memory
Deep copy, recreate all the data in the memory copies (excluding the last layer, that is: Python's internal optimization of strings and numbers
<br><br>
The above is the detailed content of python learning diary. For more information, please follow other related articles on the PHP Chinese website!