Deep Copy and Shallow Copy in Python

Deep Copy and Shallow Copy in Python

Description
In Python, deep copy and shallow copy are two different approaches to handling object duplication. When we need to copy a composite object (such as lists, dictionaries, etc.) that contains references to other objects, understanding the difference between these two copy methods is crucial. A shallow copy only duplicates the object itself, not the child objects it references; a deep copy recursively duplicates the object and all its child objects.

Basic Concepts

  • Direct assignment: Simply creates a new variable that references the original object, both pointing to the same memory address.
  • Shallow copy: Creates a new object but contains references to the contents of the original object.
  • Deep copy: Creates a new object and recursively copies all child objects.

Example Demonstrations

  1. Direct assignment
original_list = [1, 2, [3, 4]]
assigned_list = original_list  # Direct assignment

print(id(original_list) == id(assigned_list))  # True, points to the same object
  1. Implementation of shallow copy
import copy

original_list = [1, 2, [3, 4]]
shallow_copied = copy.copy(original_list)  # Shallow copy

print(id(original_list) == id(shallow_copied))  # False, the outer list is a new object
print(id(original_list[2]) == id(shallow_copied[2]))  # True, the inner list is the same object
  1. Implementation of deep copy
import copy

original_list = [1, 2, [3, 4]]
deep_copied = copy.deepcopy(original_list)  # Deep copy

print(id(original_list) == id(deep_copied))  # False
print(id(original_list[2]) == id(deep_copied[2]))  # False, the inner list is also a new object

Comparison of Modification Operations

  1. Modifying outer elements
original_list = [1, 2, [3, 4]]
shallow_copied = copy.copy(original_list)

original_list[0] = 100
print(original_list)    # [100, 2, [3, 4]]
print(shallow_copied)   # [1, 2, [3, 4]], unaffected
  1. Modifying inner mutable objects
original_list = [1, 2, [3, 4]]
shallow_copied = copy.copy(original_list)

original_list[2].append(5)
print(original_list)    # [1, 2, [3, 4, 5]]
print(shallow_copied)   # [1, 2, [3, 4, 5]], also modified!

Practical Application Scenarios

  1. Situations suitable for shallow copy
  • When the object contains only immutable types.
  • When certain child objects genuinely need to be shared.
  • When performance requirements are high and safety can be ensured.
  1. Situations requiring deep copy
  • When a completely independent copy is needed.
  • When the object contains multi-level nested structures.
  • When modifications to the copy should not affect the original object.

Summary
The key to understanding deep and shallow copy lies in recognizing the reference nature of objects in Python. A shallow copy creates a "surface-level" duplicate, while a deep copy creates a "completely independent" duplicate. In actual programming, the appropriate copy method should be chosen based on specific needs to avoid program errors caused by unintended object sharing.