Immutable dictionary in Python 3: how to make keys(), items(), and values() dictionary views immutable

无人久伴 提交于 2019-12-08 16:23:05

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/10111212/immutable-dictionary-in-python-3-how-to-make-keys-items-and-values-dict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!