[issue45780] dict. keys view behavious diverges from set()

Gregory P. Smith report at bugs.python.org
Wed Nov 10 16:16:36 EST 2021


New submission from Gregory P. Smith <greg at krypto.org>:

Python 3.9.7 (default, Sep 24 2021, 09:43:00) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> dict(a=3,b=5).keys()
dict_keys(['a', 'b'])
>>> dict(a=3,b=5).keys() - 'a'
{'b'}
>>> dict(a=3,b=5).keys() - 'ab'
set()
>>> set(('a', 'b'))
{'b', 'a'}
>>> set(('a', 'b')) - 'ab'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'set' and 'str'
>>> set(('a', 'b')).difference('ab')
set()

basically it looks like the keys view treats - as .difference() whereas set() avoids bugs in its operators by requiring both to be sets.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45780>
_______________________________________


More information about the Python-bugs-list mailing list