python 8_ Deep and shallow copies

1. Reference of value

# 1. Reference of value
nums1 = [1, 2, 3]
nums2 = nums1
print(id(nums1), id(nums2)) # Same address space, 2044032004608 2044032004608
nums1.append(4)
print(nums2)    # [1, 2, 3, 4]
# Nums1 and nums2 point to the same memory space,
#Therefore, when nums1 is modified, nums2 will also be modified

2. Immutable data type

  • Immutable data types: integer int, string str, metagroup tuple, named tuple
  • If you modify the value of an immutable data type, its memory address will be changed. Therefore, if you want to keep the original value, you cannot add, delete or modify such data.
>>> a = 5
>>> id(a)
1982685866416
>>> a = 6
>>> id(a)
1982685866448

3. Variable data type

  • Variable data types: list type, set type, dictionary type, dict
  • If the value of the immutable data type is modified, its memory address will not change. Therefore, such data can be added, deleted or modified.
>>> li = [1, 2, 3]
>>> id(li)
1982688680960
>>> li
[1, 2, 3]
>>> li.append(4)
>>> li
[1, 2, 3, 4]
>>> id(li)
1982688680960

4. Copy

nums1 and nums2 are required not to affect each other

4.1 shallow copy

  • Nums1 and nums2 have different address spaces. Therefore, modifying nums1 will not affect nums2
  • Not all shallow copies can affect each other. There is a special case: when a list is nested or the list contains variable data types (see 4.2 deep copy for this analysis)
# 2. Copy: shallow copy and deep copy (nums1 and nums2 shall not affect each other)
# 2-1). Light copy
nums1 = [1, 2, 3]
nums2 = nums1.copy()        # n. Both copy () and n [:] can copy
print(id(nums1), id(nums2)) # Different address spaces, 24975577408 249755777728
nums1.append(4)
print(nums2)        # [1, 2, 3]
# nums1 nums2 points to different memory spaces,
#Therefore, when nums1 is modified, nums2 will not be modified

4.2 deep copy

  • Suppose a situation: (shallow copy fails)
    When a list is nested or contains variable data types,
    Deep copy must be selected for copying

  • If you use a shallow copy, the following occurs:

# 2-2). Deep copy
# Suppose a situation:
#       When a list is nested or contains variable data types,
#       Deep copy must be selected for copying
# If you use a shallow copy, the following occurs:
nums1 = [1, 2, [1, 2]]
nums2 = nums1.copy()
nums1[-1].append(4)
print(id(nums1), id(nums2))         # Different address spaces, 2247109718848 2247109718528
print(id(nums1[-1]),id(nums2[-1]))  # Same address space, 2247109718400 2247109718400
print(nums2)    # [1, 2, [1, 2, 4]]
# At this time, the shallow copy method n.copy() cannot guarantee that the data will not affect each other
# Therefore, when you nest a list in a list, you need to use a deep copy

  • Correct approach:

When a list is nested or contains variable data types
The fundamental difference between deep copy and shallow copy is whether to really obtain the replication entity of an object rather than a simple reference;
If B copies A, check whether B changes when modifying A;
If B also changes, it means that it is a shallow copy, and the hand is short! (modify the same value in heap memory)
If B doesn't change, it means it's a deep copy and independent! (modify different values in heap memory)

# Correct practice: (use deep copy)
# If the elements of the list contain variable data types, be sure to use a deep copy
"""
The fundamental difference between deep copy and shallow copy is whether to really obtain the replication entity of an object rather than a simple reference;
If B Copied A´╝îmodify A When, check B Whether there is any change;
If B It also changed. It means it's a shallow copy. It's short! (modify the same value in heap memory)
If B No change, it means it's a deep copy and live on its own! (modify different values in heap memory)
"""
# 2-3). Realize deep copy, copy.depcopy
import copy
nums1 = [1, 2, [1, 2]]
nums2 = copy.deepcopy(nums1)
print(id(nums1),id(nums2))          # 2381062438272 2381062157632
print(id(nums1[-1]),id(nums2[-1]))  # 2381062477952 2381062438720
nums1[-1].append(4)
print(nums2)        # [1, 2, [1, 2]]
# After deep copy, nums1 and nums2 do not affect each other completely

4.3 difference between shallow copy and deep copy


Tags: Python Back-end

Posted on Sat, 04 Dec 2021 15:08:41 -0500 by Soumen