Python Whether the data type in is variable depends on whether you modify the memory pointer or the value in the memory address when you modify the variable .
When an immutable data type modifies the value of a variable , In fact, it just changed the pointer of this variable . The variable data type variable is the memory address unchanged , Changed the value of the address .
Pay extra attention to this when using and replicating variable data types .
100, 200, 300] > numbs2 = numbs1 > numbs1 = 1 > print(numbs2) [1, 200, 300] > id(numbs1) 140071119138240 > id(numbs2) 140071119138240> numbs1 = [
The difference between depth and depth of replication mainly applies to lists in variable data types 、 Dictionaries 、 aggregate .
Shallow copy when copying, the new object points to a different memory address than the original object , But the elements in the object point to the same memory address .
100, 200, 300] > numbs2 = numbs1 > numbs1 = 1 > print("%X %X" %(id(numbs2),id(numbs2))) 7F465477ED00 7F465477ED00 > numbs3 = numbs1.copy > print(" The memory address of the list %X %X %X" %(id(numbs1),id(numbs2),id(numbs3))) The memory address of the list 7FAB8520E180 7FAB8520E180 7FAB8520E380 > print(" The memory address of the first element in the list %X %X %X" %(id(numbs1),id(numbs2),id(numbs3))) The memory address of the first element in the list 558984E25D40 558984E25D40 558984E25D40> numbs1 = [
Deep copy will copy all the elements in the object to other addresses . It's a full copy .# import copy ; z = copy.deepcopy(x)