集合操作:只适用于集合,但适用于 dict_keys?
Set operations: should only work with sets, but works with dict_keys?
documentation for set operations 说:
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 like set('abc') & 'cbs'
in favor
of the more readable set('abc').intersection('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'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'set' and 'str'
# doesn't work, as expected
>>> set('ab') & list('ac')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'set' and 'list'
# doesn't work, as expected
>>> set('ab') & iter('ac')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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
运算符 &
适用于 dict_keys
个对象;
- 文档说它应该只适用于集合;
dict_keys
对象不是集合。
为什么集合运算符 &
与 dict_keys 对象一起使用?它还有其他类型吗?我怎样才能找到这些类型的列表?
这不是一个完整的答案,但 dict_keys
是 collections.abc.Set
的实例:
from collections.abc import Set
k = dict(zip('ac', 'ac')).keys()
print(isinstance(k, Set)) # -> True
documentation for set operations 说:
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 like
set('abc') & 'cbs'
in favor of the more readableset('abc').intersection('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'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'set' and 'str'
# doesn't work, as expected
>>> set('ab') & list('ac')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'set' and 'list'
# doesn't work, as expected
>>> set('ab') & iter('ac')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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
运算符&
适用于dict_keys
个对象;- 文档说它应该只适用于集合;
dict_keys
对象不是集合。
为什么集合运算符 &
与 dict_keys 对象一起使用?它还有其他类型吗?我怎样才能找到这些类型的列表?
这不是一个完整的答案,但 dict_keys
是 collections.abc.Set
的实例:
from collections.abc import Set
k = dict(zip('ac', 'ac')).keys()
print(isinstance(k, Set)) # -> True