Assignment, deep and shallow copy copy

Assignment:

20 is = A 
B = A
 Print (A, ID (A)) # assignment completely copied, the same memory address, if the original changes, variations will follow behind 
<<<< 20 is 140,728,211,236,080 Print (B, ID (B) )
 <<<< 20 140,728,211,236,080

Shallow copy: can copy () or [:]

= Li [l, 2,3, [6,7, 8 ]] 
C = li.copy ()   # list if the original inside shallow copy, the copy will be appreciated that only the surface layer of a list, which list is not copied, changed, will change along with copies of 
Li [0] =. 4    
Li [ . 3] [0] =. 9
 Print (Li)
 <<<< [. 4, 2,. 3, [. 9,. 7,. 8 ]]   
 Print (C)
 <<<< [1, 2, 3, [9, 7, 8]]

Deep copy: after the introduction of import copy module, copy.deepcopy ()

= li [, 2, 3, [6,7,8 ]]
 Import Copy 
A = copy.deepcopy (li)   # deep copy, can be understood as an original copy of all the side, nothing to do with the original, if changed the original, the copy will not change 
Li [0] =. 4 
Li [ . 3] [0] =. 9
 Print (Li)
 <<<< [. 4, 2,. 3, [. 9,. 7,. 8 ]]
 Print (A)
 <<<< [. 1, 2,. 3, [. 6,. 7,. 8]]

 

Guess you like

Origin www.cnblogs.com/whileke/p/11305392.html