The difference between assignment, shallow copy and deep copy in python

I assignment

Object assignment is actually a reference to an object. When you create an object and assign it to another variable, python does not copy the object, but only the reference of the object

>>> import copy
>>> a=1 >>> b=a >>> id(a) 2444000323888 >>> id(b) 2444000323888 >>> id(1) 2444000323888 #The address reference of the immutable variable 1 (number) is copied to b, and a and b point to the same object 1 in memory >>> b=3 >>> id(b) 2444000323952 #Assign the address reference of immutable variable 3 (number) to b, b points to object 3 in memory, and the address reference of a remains unchanged or points to object 1 >>>

II Shallow copy

Copy the external space without copying the shell sub object, and all of them refer to the same address internally.

Shallow copy function: [:] of list(), list(), set(), map(), and corresponding copy function, copy function in copy module.

>>> import copy
>>> a=[1,2,[3,4]] >>> b=copy.copy(a) #Copy light copy a >>> print(id(a),id(b)) #After copying the external space, the memory addresses of variables A and B are different 2444005715136 2444006007872 >>> b [1, 2, [3, 4]] >>> print(id(a[0]),id(b[0])) #The sub objects of variables A and B have the same memory address 2444000323888 2444000323888 >>> print(id(a[2]), id(b[2])) #The memory addresses of sub objects of variables A and B are the same 2444006007360 2444006007360 >>> b[0]=3          #Through assignment, copy the address reference of 3 to b[0], and a[0] does not change, because the pointing value of a[0] address is not changed >>> b [3, 2, [3, 4]] # >>> a [1, 2, [3, 4]] >>> b[2].append(5) #Change the address reference point value of b[2], so the value in a[2] will change accordingly >>> >>> b [3, 2, [3, 4, 5]] >>> a [1, 2, [3, 4, 5]]

III Deep copy

The so-called "deep copy" refers to creating a new object and then recursively copying the sub objects contained in the original object. The deep copy object has no association with the original object.

There is only one way of deep copy: the deepcopy function in the copy module.

>>> import copy
>>> a=[1,2,[3,4]] >>> b=copy.deepcopy(a) >>> print(id(a),id(b)) #The addresses of deep copy objects a and B are different 2444006177024 2444006176512 >>> print(id(a[2]),id(b[2])) #The sub object addresses of deep copy objects B and a are also different 2444006176448 2444006176832
>>> b[2].append(5) #Change the memory address pointing value of the sub object b[2] of the deep copy object b. because the memory addresses of the two deep copy objects and their respective sub objects are different, the memory address pointing value of the object a[2] remains unchanged (the value of a remains unchanged)
>>>b
[1,2,[3,4,5]]
>>>a
[1,2,[3,4]]

IV summary

1. Assignment: simply copy the reference of the object. The memory address of the two objects is the same, that is, the id is the same.
2. Shallow copy: creates a new composite object that shares sub objects in memory with the original object. (the memory addresses of the two objects are different, but the memory addresses of their child objects are the same)
3. Deep copy: create a new composite object and copy all sub objects recursively at the same time. The new composite object has no association with the original object. Although immutable sub objects are actually shared, their mutual independence is not affected.

The difference between shallow copy and deep copy is only for composite objects. The so-called composite objects are objects containing other objects, such as lists and class instances. For numbers, strings and other "atomic" types, there is no copy, but the reference of the original object.

 

Posted by as22607 on Thu, 12 May 2022 03:51:25 +0300