Short version: What's the best way to override dict.keys()
and friends to keep myself from accidentally modifying my (supposedly) immutable dictionary in Python 3?
In a recent question I asked about Hashing an immutable dictionary in Python. Since then I have built an immutable, hashable dictionary I'm happy with. However, I realized it has a hole: the dictionary views returned by keys()
, items()
, and values()
still allow myself accidentally to mutate my (supposedly) immutable dictionary.
The only question on Stack Overflow I could find about dictionary views was Python create own dict view of subset of dictionary, but that didn't seem to have much to do with my problem, and the answers to What would a "frozen dict" be? didn't seem to get into overriding keys()
, etc.
Would doing something like this prevent me from accidentally modifying, for example, the keys of my immutable dictionary?
class FrozenCounter(collections.Counter):
"Model an hashable multiset as an immutable dictionary."
# ...
def keys(self):
return list(super().keys())
def values(self):
return list(super().values())
def items(self):
return list(super().items())
What I've gathered from the answers
I can't read, mainly.
dictviews cannot modify dicts. In the Python 3 documentation, I misread, "They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes" as saying "when the view changes, the dictionary reflects these changes." Obviously that is not what the documentation said.