Introduction to python replication (deep copy, shallow copy) and python
Blog reference: click here
In python, there are differences between object replication and shallow copy and deep copy. Here we mainly use variable variables for demonstration, immutable variables do not involve assignment/copy issues (which will be explained below). The specific differences are as follows:
1. assignment:
1 a=[1,2,3]2 b=a3 a.append('3')4 print 'b=',b
The instance output result is as follows:
1 b= [1, 2, 3, '3']
Here we only operate List a, but list B also changes. The root cause is that when assigning values, variables a and B point to the same memory address, example:
1 # coding = utf-82 a = [, 3] 3 B = a4 print "variable a memory address:", id () # Get the memory address of the variable 5 print "memory address of variable B:", id (B) 6. append (4) 7 print "memory address of variable a:", id (a) 8 print "memory address of variable B:", id (B)
The output result is as follows:
1. Memory Address of variable a: Memory Address of variable B 390125922: Memory Address of variable a 390125923: Memory Address of variable B 390125924: 39012592
The above results show that the variable is consistent with the memory address of variable B during the value assignment operation. Therefore, when the value of variable a changes, the value of variable B also changes.
However, this is only applicable to variable variables. str is an unchangeable variable. When the value changes, a new address is requested again. The example is as follows:
1 # coding = utf-82 a = '000000' 3 print 'variable a memory address:', id (a) 4 a = '000000' 5 print 'variable a memory address: ', id ()
The output result is as follows:
1. Memory Address of variable a: 387348322. Memory Address of variable a: 38734856
2. copy. copy ())
1 # coding = utf-82 import copy3 a = [1, 2, 3] 4 B = copy. copy (a) 5 print 'memory address of variable a: ', id (a) 6 print 'memory address of variable B:', id (B) 7. append (4) 8 print B
Output result:
1. Memory Address of variable a: 381606642. Memory Address of variable B: 382032243 [1, 2, 3]
Because the memory address of variable a is different from that of variable B, variable B is not changed when variable a is changed, but the element address of the original variable will still be used in the shortest copy, example:
1 # coding = utf-82 import copy3 a = [1, 2, 3] 4 B = copy. copy (a) 5 for I in range (0, len (a): 6 print 'memory address of % d in variable a: % d' % (I, id (a [I]) 7 for I in range (0, len (B): 8 print 'memory address of the % d in variable B: % d' % (I, id (B [I])
Output result:
1. 0th memory address in variable a: 338608402 memory address in variable a: 1st memory address in variable a: 338608283 memory address in variable a: 2nd memory address in variable B: 338608405 of the memory address in variable B: 1st of the memory address in variable B: 338608286
The output results show that the memory addresses of elements in variable a and variable B are the same. When variable a contains a variable element, change the variable element, variable B is changed at the same time. The example is as follows:
1 # coding = utf-82 import copy3 a = [1, 2, 3, [11, 2, 3] 4 B = copy. copy (a) 5 print 'content of B before variable a is modified: ', b6 a [3]. append (4) 7 print 'After variable a is modified, content of variable B:', B
The output is as follows:
1 before variable a is modified, the content of B: [1, 2, 3, [11, 2, 3] After variable a is modified, the content of B: [1, 2, 3, [11, 2, 3, 4]
3. Deep copy (copy. deepcopy ())
1 # coding = utf-82 import copy3 a = [1, 2, 3, [11, 2, 3] 4 B = copy. deepcopy (a) 5 for I in range (0, len (a): 6 print 'memory address of % d in variable a: % d' % (I, id (a [I]) 7 for I in range (0, len (B): 8 print 'memory address of the % d in variable B: % d' % (I, id (B [I])
The output is as follows:
1. 0th of the memory address in variable a: 305185042 of the memory address in variable a: 1st of the memory address in variable a: 305184923 of the memory address in variable a: 2nd of the memory address in variable: 385538805 of the memory address in variable B: 0th of the memory address in variable B: 305185046 of the memory address in variable B: 1st of the memory address in variable B: 305184927 of the memory address in variable B: 2nd
The memory address of each element in variable a and variable B is different, so variable B does not change when variable a changes.
Summary:
1. assign values, make a small copy, and make a deep copy only for variable variables, such as list, dict, and tuple.
2. All assignments in python are passed through the memory address.
3. The copy and copy () operations only assign values to the object and re-apply for the memory address. However, the elements in the object are still referenced by the original memory address.
4. To completely copy an object, use copy. deepcopy ()
Written below:
Write a little bit every day. One day, the salted fish will become more salty.