DEV Community

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

Posted on • Edited on

Frozenset shallow & deep copy in Python

Buy Me a Coffee

*Memo for shallow and deep copy:

*Memo for others:

  • My post explains a frozenset (1).

Different frozensets are referred to, only deep-copied.


A 2D frozenset is experimented, doing an assignment and shallow and deep copy as shown below:

*Memo:

  • A 2D frozenset cannot be shallow-copied but can be deep-copied.
  • frozenset.copy() doesn't shallow-copy the frozenset so I asked to remove it in the issue.
  • A frozenset 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.
  • 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 and inner frozenset.
  • is keyword can check if A and B refer to the same outer and inner frozenset.

A 2D frozenset is assigned to a variable without copied as shown below:

    ######## Outer frozenset ########
#   ↓↓↓↓↓↓↓↓↓↓↓                    ↓↓ 
A = frozenset([frozenset([0, 1, 2])])
             # ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ Inner frozenset
B = A

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

print(A is B)
# True

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

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

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

<Shallow copy>:

*Memo:

  • A and B refer to the same outer and inner frozenset.

frozenset.copy() cannot shallow-copy the 2D frozenset as shown below:

A = frozenset([frozenset([0, 1, 2])])
B = A.copy()

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

print(A is B)
# True

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

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

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

copy.copy() cannot shallow-copy a 2D frozenset as shown below:

import copy

A = frozenset([frozenset([0, 1, 2])])
B = copy.copy(A)

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

print(A is B)
# True

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

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

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

frozenset() cannot shallow-copy a 2D frozenset as shown below:

A = frozenset([frozenset([0, 1, 2])])
B = frozenset(A)

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

print(A is B)
# True

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

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

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

<Deep copy>:

*Memo:

  • A and B refer to different outer and inner frozensets.

copy.deepcopy() can deep-copy a 2D frozenset as shown below:

*Memo:

  • copy.deepcopy() should be used because it's safe, deeply copying a 2D frozenset while frozenset.copy(), copy.copy() and frozenset() aren't safe, shallowly copying a 2D frozenset.
import copy

A = frozenset([frozenset([0, 1, 2])])
B = copy.deepcopy(A)

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

print(A is B)
# False

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

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

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

Additionally, copy.deepcopy() can deep-copy a 3D frozenset as shown below:

import copy

A = frozenset([frozenset([frozenset([0, 1, 2])])])
B = copy.deepcopy(A)

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

print(A is B)
# False

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

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

print(A is B)
# False

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

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

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

Top comments (0)