*Memo:
- My post explains a frozenset (1).
- My post explains a frozenset (2).
- My post explains a frozenset (3).
A frozenset cannot be read by indexing and slicing and changed by indexing, slicing and a del statement as shown below:
*Memo:
- A
del
statement cannot remove zero or more elements from a frozenset by indexing and slicing but can remove one or more variables themselves.
A = frozenset([10, 20, 30, 40, 50, 60])
print(A[1], A[3:5])
print(A[-5], A[-3:-1])
# TypeError: 'frozenset' object is not subscriptable
A = frozenset([10, 20, 30, 40, 50, 60])
A[1] = 'X'
A[-5] = 'X'
A[3:5] = ['Y', 'Z']
A[-3:-1] = ['Y', 'Z']
A[1], A[3:5] = 'X', ['Y', 'Z']
A[-5], A[-3:-1] = 'X', ['Y', 'Z']
# TypeError: 'frozenset' object does not support item assignment
A = frozenset([10, 20, 30, 40, 50, 60])
del A[1], A[3:5]
del A[-5], A[-2:5]
# TypeError: 'frozenset' object does not support item deletion
A = frozenset([10, 20, 30, 40, 50, 60])
del A
print(A)
# NameError: name 'A' is not defined
If you really want to read a frozenset by indexing and slicing or change a frozenset by indexing, slicing and a del
statement, use list() and frozenset() as shown below:
A = frozenset([10, 20, 30, 40, 50, 60])
A = list(A)
print(A[1], A[3:5])
print(A[-5], A[-3:-1])
# 10 [20, 60]
A[1] = 'X'
A[-5] = 'X'
A[3:5] = ['Y', 'Z']
A[-3:-1] = ['Y', 'Z']
A[1], A[3:5] = 'X', ['Y', 'Z']
A[-5], A[-3:-1] = 'X', ['Y', 'Z']
A = frozenset(A)
print(A)
# frozenset({'Z', 40, 50, 'Y', 30, 'X'})
A = frozenset([10, 20, 30, 40, 50, 60])
A = list(A)
del A[1], A[3:5]
# del A[-5], A[-2:5]
A = frozenset(A)
print(A)
# frozenset({40, 50, 20})
A frozenset can be continuously used through multiple variables as shown below:
A = B = C = frozenset([10, 20, 30]) # Equivalent
# A = frozenset([10, 20, 30])
print(A) # frozenset({10, 20, 30}) # B = A
print(B) # frozenset({10, 20, 30}) # C = B
print(C) # frozenset({10, 20, 30})
A frozenset cannot be shallow-copied but can be deep-copied as shown below:
<Shallow copy>:
*Memo:
-
A
andB
refer to the same outer and inner frozenset. -
is
keyword can check ifA
andB
refer to the same outer and inner frozenset. -
frozenset.copy(), copy.copy() and
frozenset()
cannot shallow-copy a frozenset:-
frozenset.copy()
has no arguments.
-
import copy
A = frozenset([frozenset([0, 1, 2])])
B = A.copy()
B = copy.copy(A)
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
<Deep copy>:
*Memo:
-
A
andB
refer to different outer and inner frozensets. - copy.deepcopy() deep-copies a frozenset.
-
copy.deepcopy()
should be used because it's safe, deeply copying a frozenset whilefrozenset.copy()
,copy.copy()
andfrozenset()
aren't safe, shallowly copying a 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
Top comments (0)