<> Depth copy
first , We know Python3 in , Yes 6 Standard data types , They are divided into variable and immutable .
Immutable data (3 individual ):
* Number( number )
* String( character string )
* Tuple( tuple )
Variable data (3 individual ):
* List( list )
* Dictionary( Dictionaries )
* Set( aggregate )
<>copy In the module copy method ( Light copy )
#1, about must not Variant type Number String Tuple, Shallow copy is just address pointing , Will not open up new space . #2, about can Variant type
List,Dictionary,Set, Shallow copy will open up new spatial addresses ( It's just the top that opens up new space , The element address of the inner layer is still the same ), Make a shallow copy
#3, After shallow copy , Change the value of the variable type element in the original object , Will also affect the copy object's ; Changes the original type of the object in the , Only the original type is affected .`( The operation of copy objects is the same with the original objects )
<> Demo code
import copy # Immutable data type # numerical value num a=0 b=copy.copy(a) print(a is b) #True #is
Content and address are equal # character string str a='yyyy' b=copy.copy(a) print(a is b) #True # tuple tuple a=(1,2,3
,[1,2]) b=copy.copy(a) print(a is b) #True # Variable data type # aggregate set a={1,2,3,4} b=copy.
copy(a) print(a is b) #False # Dictionaries dict a={1:2,3:4} b=copy.copy(a) print(a is b)
#False # list list a=[1,2,3,4] b=copy.copy(a) print(a is b) #False # Variable type
# Shallow copy will open up new spatial addresses , It's just the top that opens up new space , The element address of the inner layer is still the same , therefore a[3] When changes occur ,b Yes # Follow the change . a=(1,2,3,[4,5,6]
) b=copy.copy(a) print(id(a)==id(b)) #True Before modification a[3].append(7) print(id(a)==id(b
)) #True After modification
<>copy In the module deepcopy Method implementation
#1, Deep copy , Except for the top-level copy , The child elements are also copied ( Essentially recursive shallow copy ) #2, After deep copy , All element addresses of the original object and the copy object are not the same
<> Code demonstration
import copy # Immutable data type # numerical value num a=0 b=copy.deepcopy(a) print(a is b) #True #is
Content and address are equal # character string str a='yyyy' b=copy.deepcopy(a) print(a is b) #True # tuple tuple a=(1
,2,3,[1,2]) b=copy.deepcopy(a) print(a is b) #True # Variable data type # aggregate set a={1,2,3,4} b
=copy.deepcopy(a) print(a is b) #False # Dictionaries dict a={1:2,3:4} b=copy.deepcopy(a)
print(a is b) #False # list list a=[1,2,3,4] b=copy.deepcopy(a) print(a is b)
#False # Variable type # Deep copy , Except for the top-level copy , The child elements are also copied ( Essentially recursive shallow copy ) a=(1,2,3,[4,5,6]) b=copy.
deepcopy(a) print(id(a)==id(b)) #False Before modification , Here, the list is changed to immutable type ( character string , tuple ), The result is True. a[3]
.append(7) print(id(a)==id(b)) #False After modification
<> summary
Because of the deep copy vs. the shallow copy , It is emphasized that recursion , The emphasis is on resource factors . Right, the top-level operation , No difference in depth and depth .
So deep copy and shallow copy , The biggest difference is in the second layer and after .
For shallow copy , The second layer is whether it is a variable data type or an immutable data type ,id The address is the same , therefore a( Variable type ) Make changes ,b Changes will follow .
For deep copy , The second layer is whether it's a variable data type ,id The address is the same . But when the data type is immutable ,id The address is different , therefore a Make changes ,b It won't change .
Technology