How/why does set() in {frozenset()} work?({frozenset()} 中的 set() 如何/为什么起作用?)
问题描述
即使集合是不可散列的,其他集合中的成员资格检查也有效:
Even though sets are unhashable, membership check in other set works:
>>> set() in {frozenset()}
True
我预计TypeError: unhashable type: 'set',与Python中的其他行为一致:
I expected TypeError: unhashable type: 'set', consistent with other behaviours in Python:
>>> set() in {} # doesn't work when checking in dict
TypeError: unhashable type: 'set'
>>> {} in {frozenset()} # looking up some other unhashable type doesn't work
TypeError: unhashable type: 'dict'
那么,其他集合中的集合成员是如何实现的?
So, how is set membership in other set implemented?
推荐答案
最后一行sets 的文档讨论了这个:
The last line of the documentation for sets discusses this:
注意, 的 elem 参数__contains__(), remove() 和 丢弃()方法可以是一个set.支持搜索等价物frozenset,从 elem 创建一个临时的.
Note, the
elemargument to the__contains__(),remove(), anddiscard()methods may be aset. To support searching for an equivalentfrozenset, a temporary one is created fromelem.
这篇关于{frozenset()} 中的 set() 如何/为什么起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:{frozenset()} 中的 set() 如何/为什么起作用?
基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
