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),id(b)) #The sub objects of variables A and B have the same memory address 2444000323888 2444000323888 >>> print(id(a), id(b)) #The memory addresses of sub objects of variables A and B are the same 2444006007360 2444006007360 >>> b=3 #Through assignment, copy the address reference of 3 to b, and a does not change, because the pointing value of a address is not changed >>> b [3, 2, [3, 4]] # >>> a [1, 2, [3, 4]] >>> b.append(5) #Change the address reference point value of b, so the value in a 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),id(b)) #The sub object addresses of deep copy objects B and a are also different 2444006176448 2444006176832
>>> b.append(5) #Change the memory address pointing value of the sub object b 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 remains unchanged (the value of a remains unchanged)
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.