1. For the assignment of characters and numbers, deep copy and shallow copy
Import Copy
S1 = "ABCDEFG"
S2 = S1
Print (ID (S1))
Print (ID (s2))
Results:
24266496
24266496
Conclusion: For assignment operations, memory addresses are consistent.
Import Copy
S1 = "ABCDEFG"
S2 = copy.copy (S1)
Print (ID (S1))
Print (ID (s2))
Results:
5129984
5129984
Conclusion: For shallow copies, memory addresses are consistent.
Import Copy
S1 = "ABCDEFG"
S2 = copy.deepcopy (S1)
Print (ID (S1))
Print (ID (s2))
Results:
24069888
24069888
Conclusion: For deep copies, memory addresses are also consistent
2. For list, Ganso and dictionary assignment, deep copy and shallow copy
Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2":(' v21 ', ' v22 ', ' v23 '), "K3": "V31"}
N2 = N1
Print (ID (N1))
Print (ID (n2))
Print (ID (n1[' k1 '][0]))
Print (ID (n2[' k1 '][0]))
Results:
24617448
24617448
24594208
24594208
Conclusion: For assignment operation, N2 and N1 are the same pointing relation
Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2": [' v21 ', ' v22 ', ' v23 '], "K3": "V31"}
N2 = Copy.copy (n1)
Print (ID (N1))
Print (ID (n2))
Print (ID (n1[' k1 '))
Print (ID (n2[' k1 '))
Results:
30712296
30965472
31193048
31193048
Conclusion: For a shallow copy, only the first layer is a new memory area in memory to save the pointing relationship
Import Copy
N1 = {"K1": [' v11 ', ' V12 ', (' v131 ', ' v132 ')], "K2": [' v21 ', ' v22 ', ' v23 '], "K3": "V31"}
N2 = Copy.deepcopy (n1)
Print (ID (N1))
Print (ID (n2))
Print (ID (n1[' k1 '))
Print (ID (n2[' k1 '))
Print (ID (n1[' k1 '][0]))
Print (ID (n2[' k1 '][0]))
Results:
21144040
21147600
24639488
24638088
21120800
21120800
Conclusion: For deep copy, copy deep internal structure
Applications for deep-shade copies:
Import Copy
DIC = {
"CPU": [80,],
"Mem": [80,],
"Disk": [80,]
}
Print (DIC)
New_dic = Copy.deepcopy (DIC)
new_dic["CPU"][0] = 50
Print (DIC)
Print (New_dic)
Results:
{' Disk ': [+], ' mem ': [+], ' CPU ': [80]}
{' Disk ': [+], ' mem ': [+], ' CPU ': [80]}
{' Disk ': [+], ' mem ': [+], ' CPU ': [50]}
Conclusion: The original valve value is not modified, the newly added server threshold is reset to 50. If a shallow copy is used, the new threshold value will be changed.
Python depth Copy