Understanding and research of shallow copy and deep copy in Python

Keywords: Python

Single layer shallow copy

import copy
a = 1  # Immutable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Same memory address

a = [1,2]  # Variable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Different memory addresses

Single layer deep copy

import copy
a = 1  # Immutable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Same memory address

a = [1,2]  # Variable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Different memory addresses

Conclusion 1:

Both the deep copy and the shallow copy refer to the memory address for immutable data types

Whether the deep copy or the shallow copy will recreate new memory space for variable data types

Shallow copy nesting

# -----------Immutable data type---------
# Case 1, embedded variable data type
import copy
a = ([1,2],(3,4))  # Immutable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Same memory address
# Case 2: embedded immutable data type
a = ((1,2),(3,4))  # Immutable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Same memory address

#-----------Variable data type-------------
# Case 1, embedded variable data type
import copy
a = [(1,2),[3,4]]  # Variable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Different memory addresses

# Case 1: variable data type embedded variable data type
import copy
a = [(1,2),(3,4)]  # Variable data type
copy_a = copy.copy(a)
print(id(a),id(copy_a))  # Different memory addresses

Deep copy nesting

# -----------Immutable data type---------
# Case 1, embedded variable data type
import copy
a = ([1,2],(3,4))  # Immutable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Different memory addresses
# Case 2: embedded immutable data type
a = ((1,2),(3,4))  # Immutable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Same memory address

#-----------Variable data type-------------
# Case 1, embedded variable data type
import copy
a = [(1,2),[3,4]]  # Variable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Different memory addresses
print(id(a[0]),id(copy_a[0]))  # Same memory address
print(id(a[1]),id(copy_a[1]))  # Different memory addresses

# Case 1: variable data type embedded variable data type
import copy
a = [(1,2),(3,4)]  # Variable data type
copy_a = copy.deepcopy(a)
print(id(a),id(copy_a))  # Different memory addresses
print(id(a[0]),id(copy_a[0]))  # Same memory address

Conclusion two:

Shallow copy:

  1. The outer layer is an immutable type, and it is a reference copy whether the inner layer is mutable or not
  2. The outer layer is a variable type, and a new memory space will be created no matter whether the inner layer is variable or not

Deep copy:

  1. The outer layer is immutable. It will recursively determine the inner layer data type. If it is mutable, it will create a new memory address. If it is immutable, it is a reference copy
  2. The outer layer is a variable data type. Whether the inner layer is variable or not, a new memory address will be created. However, if the inner layer is variable, it will be recursively created. If it is immutable, it will be a reference address

Conclusion:

Shallow copy:

  1. Shallow copy only judges the top-level data type
  2. Create a new memory space if the top level is a mutable type
  3. If the top level is immutable data type, it is reference copy

Deep copy:

  1. The deep copy is used as a recursive copy, which can copy all the internal nested data recursively
  2. Deep copy recursive copy creates new memory space when it encounters variable type
  3. Deep copy recursive copy encounters immutable data type, which is the reference of copy

Posted by skeppens on Wed, 04 Dec 2019 10:04:55 -0800