Python append() and Deep Copy, Shallow Copy
Category Programming Techniques
Shallow and Deep Copy
In Python, object assignment is essentially a reference to the object. When an object is created and then assigned to another variable, Python does not copy the object itself, but only copies the reference to the object, which we call a shallow copy.
In Python, to ensure that two variables do not affect each other during assignment operations, you can use the deepcopy method from the copy module, which is called a deep copy.
append() Function
When an object of the list type performs an append operation, what is actually appended is the reference to that object.
id() Function: Returns the unique identifier of an object, which can be likened to the address of the object in memory.
>>> alist = []
>>> num = [2]
>>> alist.append(num)
>>> id(num) == id(alist[0])
True
alist.append(copy.deepcopy(num))
>
Original article link: https://blog.csdn.net/u010099495/article/details/50276833
** Click to Share Notes
-
-
-