集合操作:应该仅与集合一起使用,但可以与dict_keys一起使用?
请注意,Union(),bettrosection()的非操作版本, 差异(),symmetric_difference(),issubset()和issuperset() 方法将接受任何觉得作为参数。相反,他们 基于操作员的同行要求将其参数设置为设置。这 排除诸如
set('abc')& amp; amp; amp; amp; amp; amp;排除了。 'CBS'
支持 更可读的set('abc')。交叉点('cbs')
。
通过以下实验对此进行测试:
# Python 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0] on linux
>>> set('ab') & set('ac')
{'a'}
# works, as expected
>>> set('ab') & 'ac'
TypeError: unsupported operand type(s) for &: 'set' and 'str'
# doesn't work, as expected
>>> set('ab') & list('ac')
TypeError: unsupported operand type(s) for &: 'set' and 'list'
# doesn't work, as expected
>>> set('ab') & iter('ac')
TypeError: unsupported operand type(s) for &: 'set' and 'str_iterator'
# doesn't work, as expected
>>> set('ab') & dict(zip('ac', 'ac')).keys()
{'a'}
# works??
>>> type({}.keys())
<class 'dict_keys'>
>>> isinstance({}.keys(), (set, frozenset))
False
因此,这是悖论:
set
操作员&amp;
与dict_keys
对象一起工作;- 该文件说,它只能与集合一起使用;
dict_keys
未设置对象。
为什么设置运算符&amp;
与dict_keys对象一起工作?它还有其他类型吗?我如何找到这些类型的列表?
The documentation for set operations says:
Note, the non-operator versions of union(), intersection(),
difference(), symmetric_difference(), issubset(), and issuperset()
methods will accept any iterable as an argument. In contrast, their
operator based counterparts require their arguments to be sets. This
precludes error-prone constructions likeset('abc') & 'cbs'
in favor
of the more readableset('abc').intersection('cbs')
.
Testing this with the following experiment:
# Python 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0] on linux
>>> set('ab') & set('ac')
{'a'}
# works, as expected
>>> set('ab') & 'ac'
TypeError: unsupported operand type(s) for &: 'set' and 'str'
# doesn't work, as expected
>>> set('ab') & list('ac')
TypeError: unsupported operand type(s) for &: 'set' and 'list'
# doesn't work, as expected
>>> set('ab') & iter('ac')
TypeError: unsupported operand type(s) for &: 'set' and 'str_iterator'
# doesn't work, as expected
>>> set('ab') & dict(zip('ac', 'ac')).keys()
{'a'}
# works??
>>> type({}.keys())
<class 'dict_keys'>
>>> isinstance({}.keys(), (set, frozenset))
False
So, here is the paradox:
set
operator&
works withdict_keys
objects;- The documentation says it should only work with sets;
dict_keys
objects are not sets.
Why does set operator &
work with dict_keys objects? Are there other types that it works with? How can I find a list of these types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是一个完整的答案,但是
dict_keys
是collections.abc.set
:this is not a complete answer, but
dict_keys
are instances ofcollections.abc.Set
:文档的这一部分是指
set
/frozenset
类型支持。确实,set
不支持与dict_keys
实例的比较,并且它正确返回。它成功:
如果
类型(左).__和__
和type(右)。 ,然后您将看到
typeerror
异常。因此,没有悖论,只有数据模型中的一个微妙之处:
set
选择退出,dict_keys
类型有机会从右手处理二进制操作边。This section of the docs is referring to what the
set
/frozenset
types support. Indeed,set
does not support the comparison withdict_keys
instances, and it correctly returnsNotImplemented
to indicate this operation is unsupported:This return value indicates that Python should attempt the reflected operation with the other operand, to see if that is supported. And it succeeds:
If both
type(left).__and__
andtype(right).__rand__
are returningNotImplemented
, then you will see aTypeError
exception.So, there is no paradox, just a subtlety in the datamodel: after
set
opts out, thedict_keys
type is afforded the chance to handle the binary operation from the right-hand side.(没有足够的代表来发表评论)
设置运算符也可以在dict_keys和列表之间工作。
但是由于某种原因而不是在集合和列表之间似乎不一致
(don't have enough rep. to comment)
The set operators also work between dict_keys and lists.
But for some reason not between sets and lists which seems inconsistent