Reputation: 3877
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())
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.
Upvotes: 15
Views: 13798
Reputation: 11567
In Python 2.x, views don't allow you to mutate your underlying object:
>>> a = { 'a' : 1 }
>>> a.keys()[0] = 'b'
>>> a
{'a': 1}
>>> a.values()[0] = 'b'
>>> a
{'a': 1}
In Python 3.x, mutating views gives a TypeError:
>>> a = { 'a':1}
>>> a.keys()[0] = 'b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object does not support item assignment
Upvotes: 6
Reputation: 6451
That's probably a bad idea, as it breaks the assumption that those methods return views in the first place, and replaces them with mutable objects that no longer update to match the underlying dict.
How are your views able to mutate your dictionary? Views don't support item assignment or deletion, so I don't believe that they can change the underlying dictionary.
Upvotes: 0