=© & deepcopy in Python

  1. = only let two pointers point to the same object
  2. copy only copies the object itself, not the sub-objects in it, so the modification of the sub-objects will also follow the modification.
  3. Deepcopy is a true copy, that is, to open up a new space. The copy we often say is actually deepcopy.
>>> import copy
>>> a=[1,2,3,[2,3],4]
>>> b=a
>>> c=copy.copy(a)
>>> d=copy.deepcopy(a)
>>> a.append(9)
>>> a
[1, 2, 3, [2, 3], 4, 9]
>>> b
[1, 2, 3, [2, 3], 4, 9]
>>> c
[1, 2, 3, [2, 3], 4]
>>> d
[1, 2, 3, [2, 3], 4]
>>> a[3][0]=8
>>> a
[1, 2, 3, [8, 3], 4, 9]
>>> b
[1, 2, 3, [8, 3], 4, 9]
>>> c
[1, 2, 3, [8, 3], 4]
>>> d
[1, 2, 3, [2, 3], 4]
>>> 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325866297&siteId=291194637