[TIL] 02-Mar-2020
Python arrays.
Copying arrays can be 2 ways. Either a shallow copy or a deep copy. If we did B = A
then B
points to he same object as A
and any changes in A
will be seen in B
. If we did B = list(A)
then any changes in A
will not be seen in B
. Also if our array was a list of objects and we used B = copy.copy(A)
then this creates a shallow copy. If the objects in A
change their properties then objects in B
will also reflect this change since they both refer to the same objects in memory. However, B = copy.deepcopy(A)
completely duplicates all objects in A
and if there is change in properties of objects in A
they will not be reflected in B
. Images make this distinction more clear.
These are from stackoverflow
Shallow:
Deep: