*Memo:
- My post explains the shallow and deep copy of a list.
- My post explains the shallow and deep copy of a tuple.
- My post explains the shallow and deep copy of the set with a frozenset.
- My post explains the shallow and deep copy of the set with a tuple.
- My post explains the shallow and deep copy of the set with an iterator.
- My post explains the shallow and deep copy of a dictionary.
- My post explains the shallow and deep copy of an iterator.
- My post explains a frozenset (1).
Different frozensets are referred to only if deep-copied.
A 2D frozenset is experimented, doing assignment and shallow and deep copy as shown below:
*Memo:
- For a 2D frozenset, only deep copy is possible.
- frozenset.copy() doesn't do shallow copy 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
anditerator
but cannot have the unhashable types of elements likebytearray
,list
,set
anddict
.
<Assignment>:
*Memo:
-
A
andB
refer to the same shallow and deep frozenset. -
is
keyword can check ifA
andB
refer to the same frozenset.
##### Shallow frozenset #####
# ↓↓↓↓↓↓↓↓↓↓↓ ↓↓
A = frozenset([frozenset(['a'])])
# ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ Deep frozenset
B = A
print(A) # frozenset({frozenset({'a'})})
print(B) # frozenset({frozenset({'a'})})
print(A is B)
# True
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# True
<Shallow copy>:
frozenset.copy() doesn't shallow-copy the 2D frozenset as shown below:
*Memo:
-
A
andB
refer to the same shallow and deep frozenset.
A = frozenset([frozenset(['a'])])
B = A.copy()
print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}
print(A is B)
# True
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# True
copy.copy() doesn't shallow-copy a 2D frozenset as shown below:
import copy
A = frozenset([frozenset(['a'])])
B = copy.copy(A)
print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}
print(A is B)
# True
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# True
frozenset() doesn't shallow-copy a 2D frozenset as shown below:
A = frozenset([frozenset(['a'])])
B = frozenset(A)
print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}
print(A is B)
# True
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# True
<Deep copy>:
copy.deepcopy() deep-copies a 2D frozenset as shown below:
*Memo:
-
A
andB
refer to different shallow and deep frozensets. -
copy.deepcopy()
should be used because it's safe, copying deeply whilefrozenset.copy()
,copy.copy()
andfrozenset()
aren't safe, copying shallowly.
import copy
A = frozenset([frozenset(['a'])])
B = copy.deepcopy(A)
print(A) # {frozenset({'a'})}
print(B) # {frozenset({'a'})}
print(A is B)
# False
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# False
Additionally, copy.deepcopy() deep-copies a 3D frozenset as shown below:
import copy
A = frozenset([frozenset([frozenset(['a'])])])
B = copy.deepcopy(A)
print(A) # frozenset({frozenset({frozenset({'a'})})})
print(B) # frozenset({frozenset({frozenset({'a'})})})
print(A is B)
# False
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({frozenset({'a'})})
print(B) # frozenset({frozenset({'a'})})
print(A is B)
# False
A = set(A).pop()
B = set(B).pop()
print(A) # frozenset({'a'})
print(B) # frozenset({'a'})
print(A is B)
# False
Top comments (0)