[issue33572] False/True as dictionary keys treated as integers

Mark Dickinson report at bugs.python.org
Fri May 18 11:26:49 EDT 2018


Mark Dickinson <dickinsm at gmail.com> added the comment:

It's documented here: https://docs.python.org/3/library/stdtypes.html#mapping-types-dict

> Numeric types used for keys obey the normal rules for numeric
> comparison: if two numbers compare equal (such as 1 and 1.0) then 
> they can be used interchangeably to index the same dictionary entry.

Since False == 0, False and 0 are interchangeable as dictionary keys. Similarly for True and 1. Note that the dictionary you created only actually has two entries:

>>> dta = {False: 'false', True: 'true', 0: 'zero', 1: 'one'}
>>> dta
{False: 'zero', True: 'one'}

Though calling out numeric types in particular in the docs does feel a little odd to me: the rule is that *any* two hashable objects that compare equal should be interchangeable for the purposes of dictionary lookup (or set membership, come to that). There's nothing particularly special about numbers in this context.

----------
nosy: +mark.dickinson

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


More information about the Python-bugs-list mailing list