A Brief Introduction to Python's Use of Memory (depth copy)

  • 2020-07-21 08:42:42
  • OfStack

This article focuses on Python's use of memory (depth copy), which is described below.

A shallow copy is a copy of the reference (only the parent object is copied)

A deep copy is a copy of the object's resources


>>> a=[1,2,3,'a','b']
>>> b=a
>>> b
[1, 2, 3, 'a', 'b']
>>> a
[1, 2, 3, 'a', 'b']
>>> id(a)
3021737547592
>>> id(b)
3021737547592
>>> a.append('c')
>>> a
[1, 2, 3, 'a', 'b', 'c']
>>> b
[1, 2, 3, 'a', 'b', 'c']
>>> b.append(4)
>>> b
[1, 2, 3, 'a', 'b', 'c', 4]
>>> a
[1, 2, 3, 'a', 'b', 'c', 4]

As can be seen from the above operation: after assigning a to b, the addresses of a and b are the same. No matter which one changes, the other one will change and remain the same.


>>> import copy
>>> a=[1,2,3,['a','b','c']]
>>> b=a
>>> c=copy.copy(a)
>>> b
[1, 2, 3, ['a', 'b', 'c']]
>>> c
[1, 2, 3, ['a', 'b', 'c']]
>>> id(a)
3021737548104
>>> id(b)
3021737548104
>>> id(c)
3021737494536    # Shallow copy the parent object's address is not 1 sample 
>>> a.append('d')
>>> a
[1, 2, 3, ['a', 'b', 'c'], 'd']
>>> b
[1, 2, 3, ['a', 'b', 'c'], 'd']
>>> c
[1, 2, 3, ['a', 'b', 'c']] #a and c The address is not 1 Sample, so a Change, c Don't change 




>>> id(a[0])
1686357680
>>> id(c[0])
1686357680
>>> id(a[3])
3021737547528
>>> id(c[3])   
3021737547528    # The entire parent object takes up no space 1 Like, but the same interior data occupied space 1 sample 
>>> a[3].append('d')
>>> a
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd']
>>> c
[1, 2, 3, ['a', 'b', 'c', 'd']]# Because of the space taken up by the inner data 1 Sample, so a Change, c Follow change 

The above is the shallow copy: the address of the whole parent object is different, the address of the inner data is the same, if the inner data is operated, 1 is the same; When manipulating an object as a parent, the copy object does not change.


>>> a
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd']
>>> d=copy.deepcopy(a)
>>> d
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd']
>>> id(a)
3021737548104
>>> id(d)
3021737547656  # The address of the deep copy parent object is not 1 sample 

>>> a.append('e')
>>> a
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd', 'e']
>>> d
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd']#a and d The address is not 1 Sample, so a Change, d Don't change 
>>> id(a[0])
1686357680
>>> id(d[0])
1686357680
>>> id(a[3])
3021737547528
>>> id(d[3])
3021737493256  # The address of the inner data is not 1 sample 
>>> a[3].append('x')
>>> a
[1, 2, 3, ['a', 'b', 'c', 'd', 'x'], 'd', 'e']
>>> d
[1, 2, 3, ['a', 'b', 'c', 'd'], 'd']

Above is the deep copy.

The difference between:

The shallow copy is the same as the inner data address of the original object;
The deep copy is completely independent and has no connection to the original object.

conclusion

That's all for Python's use of memory (copy in depth). Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: