Python中的不可变字典3:如何使key(),items()和values()字典视图不可变 [英] Immutable dictionary in Python 3: how to make keys(), items(), and values() dictionary views immutable

查看:202
本文介绍了Python中的不可变字典3:如何使key(),items()和values()字典视图不可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:覆盖 dict.keys()和朋友的最佳方法是让自己无意中修改我的(据称)在Python 3中的不可变字典3 ?



在最近的一个问题中,我询问了。从那时起,我已经建立了一个不可变的,可以哈希的字典,我很高兴。不过,我发现它有一个漏洞:字典视图 keys() items() values()仍然允许我不小心突变我的(据称)不可变字典。



Stack Overflow中唯一的问题我可以找到关于字典视图的是 Python创建自己的字典子集的dict视图,但是没有似乎与我的问题有很多关系,什么是冻结的dict ?似乎没有超越 keys()等。



会做某事这样就阻止我意外修改,例如我的不可变字典的键?

  class FrozenCounter(collections.Counter):
将可分散的多集作为不可变字典建模。
#...
def key(self):
return list(super()。keys())
def values(self):
return list super()。values())
def items(self):
return list(super()。items())




从答案中收集到的内容


$ b $我不能读,主要是。



dictviews不能修改dicts。在 Python 3文档中,我误读了他们提供字典条目的动态视图,这意味着当字典更改时,视图会在视图更改时反映这些更改说,字典反映了这些更改。显然,这不是文件说的。



对不起,浪费大家的时间有一个坏的问题!

解决方案

在Python 2.x中,视图不允许您突变基础对象:

 >>> a = {'a':1} 
>>>> a.keys()[0] ='b'
>>> a
{'a':1}
>>> a.values()[0] ='b'
>>> a
{'a':1}

在Python 3.x中, TypeError:

 >>> a = {'a':1} 
>>>> a.keys()[0] ='b'
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
TypeError:'dict_keys'对象不支持项目分配


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 be a frozen dict? 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.

Sorry for wasting everyone's time with a bad question!

解决方案

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

这篇关于Python中的不可变字典3:如何使key(),items()和values()字典视图不可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆