DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Shallow copy & Deep copy in Python (4)

Buy Me a Coffee

*Memo for shallow and deep copy:

*Memo for others:


Different sets are referred to, only shallow-copied.


The 2D set with a 1D tuple is experimented, doing assignment and shallow and deep copy as shown below:

*Memo:

  • The 2D set with a 1D tuple can be shallow-copied but cannot be deep-copied.
  • A set can have the hashable types of elements like str, bytes, int, float, complex, bool, tuple, frozenset, range and iterator but cannot have the unhashable types of elements like bytearray, list, set and dict.
  • The same tuple is referred to even if copied according to the experiments.
  • There are an assignment and 2 kinds of copies, shallow copy and deep copy:
    • An assignment is to create the one or more references to the original top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A shallow copy is to create the one or more references to the new top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A deep copy is to create the two or more references to the new top level object and the new lower levels' objects which you desire but at least the new 2nd level objects, keeping the same values as before:
      • A deep copy is the multiple recursions of a shallow copy so a deep copy can be done with multiple shallow copies.
    • Basically, immutable(hashable) objects aren't copied to save memory like str, bytes, int, float, complex, bool and tuple.

<Assignment>:

*Memo:

  • A and B refer to the same outer set and inner tuple.
  • is keyword can check if A and B refer to the same outer set and/or inner tuple.

The 2D set with a 1D tuple is assigned to a variable without copied as shown below:

    # Outer set
#   ↓         ↓ 
A = {(0, 1, 2)}
   # ↑↑↑↑↑↑↑↑↑ Inner tuple
B = A

print(A) # {(0, 1, 2)}
print(B) # {(0, 1, 2)}

print(A is B)
# True

print(A.pop()) # (0, 1, 2)
print(B.pop()) # KeyError: 'pop from an empty set'
Enter fullscreen mode Exit fullscreen mode

<Shallow copy>:

*Memo:

  • A and B refer to different outer sets and the same inner tuple.

set.copy() can shallow-copy the 2D set with a 1D tuple as shown below:

A = {(0, 1, 2)}
B = A.copy()

print(A) # {(0, 1, 2)}
print(B) # {(0, 1, 2)}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # (0, 1, 2)
print(B) # (0, 1, 2)

print(A is B)
# True
Enter fullscreen mode Exit fullscreen mode

copy.copy() can shallow-copy the 2D set with a 1D tuple as shown below:

import copy

A = {(0, 1, 2)}
B = copy.copy(A)

print(A) # {(0, 1, 2)}
print(B) # {(0, 1, 2)}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # (0, 1, 2)
print(B) # (0, 1, 2)

print(A is B)
# True
Enter fullscreen mode Exit fullscreen mode

set() can shallow-copy the 2D set with a 1D tuple as shown below:

A = {(0, 1, 2)}
B = set(A)

print(A) # {(0, 1, 2)}
print(B) # {(0, 1, 2)}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # (0, 1, 2)
print(B) # (0, 1, 2)

print(A is B)
# True
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • A and B refer to different outer sets and the same inner tuple.

copy.deepcopy() cannot deep-copy but can shallow-copy the 2D set with a 1D tuple as shown below:

import copy

A = {(0, 1, 2)}
B = copy.deepcopy(A)

print(A) # {(0, 1, 2)}
print(B) # {(0, 1, 2)}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # (0, 1, 2)
print(B) # (0, 1, 2)

print(A is B)
# True
Enter fullscreen mode Exit fullscreen mode

Additionally, copy.deepcopy() cannot deep-copy but can shallow-copy the 3D set with a 2D tuple as shown below:

import copy 

A = {((0, 1, 2),)}
B = copy.deepcopy(A)

print(A) # {((0, 1, 2),)}
print(B) # {((0, 1, 2),)}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # ((0, 1, 2),)
print(B) # ((0, 1, 2),)

print(A[0]) # (0, 1, 2)
print(B[0]) # (0, 1, 2)

print(A is B, A[0] is B[0])
# True True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)