May 11, 2021
Uh-uh, sets are mutable:
a = set()
assert 1 not in aa.add(1)
assert 1 in a
This is why sets can’t be used as dictionary keys, and you need to use frozenset
:
b = {}
b[1] = 1
b['hello'] = 2
b[False] = 3
b[(1, 2, 3)] = 4
b[frozenset((1, 2, 3))] = 5# This wouldn't work:
# b[{1, 2, 3}] = 5# More details!
assert b[False] == b[0] == 3
assert b[True] == b[1] == 1
assert frozenset((1, 2, 3)) == {1, 2, 3}