1. Numerical copy
Import copy# ##### #数字, string ##### #n1 = 123print (ID (n1)) N2 = N1print (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# ##### #数字, string ##### #n1 = "My name is Hellworld" Print (ID (n1)) N2 = N1print (id (n2)) # # #浅拷贝 # # #n2 = copy.copy (n1) PRI NT (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# ##### #数字, string ##### #n1 = ["HelloWorld", "Hapen"]print (ID (n1)) N2 = N1print (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, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address.
Shallow copy , creating only the first layer of data in memory
deep Copy , re-create all the data in memory (exclude the last layer, that is: Python internal optimization of strings and numbers