Advanced python (d)

Eight, copy depth

Copy \ shallow copy \ deep copy

8.1 copy (assignment)

Copy: when the copy of the object is lt2 lt variably changes within lt type, lt2 change; a change in the type immutable lt, lt2 change

= lt [ . 1, 2, . 3] 
lt2 = lt lt. the append ( . 4) Print ( lt) # because the list is a variable type, the value lt change, the change will follow the value lt2 Print ( lt2)




8.2 shallow copy

Shallow copy: when shallow copy of the object lt2 lt variably changes within lt type, lt2 change; immutable type lt variations within, does not change lt2

import copy
lt = [1,2,3]
lt2 = copy.copy(lt)
lt.append(4)
print(lt)#[1,2,3,4]
print(lt2)#[1,2,3]

Deep copy 8.3

Definitions: When the deep copy of the object lt2 lt variably changes within lt type, lt2 not change; a change in the type immutable lt, lt2 unchanged

Guess you like

Origin www.cnblogs.com/hanyi12/p/11311634.html