[issue26614] False/0 and True/1 collision when used as dict keys?

Mark Dickinson report at bugs.python.org
Thu Mar 24 08:34:38 EDT 2016


Mark Dickinson added the comment:

"Apparently True and 1 hash to the same item and False and 0 hash to the same item"

Just to clear up a possible misconception here, the key point here is not that they hash to the same integer, but that they're *equal*:

>>> True == 1
True
>>> False == 0
True

In contrast, here are two elements whose hash is equal but which serve as distinct keys:

>>> hash(-1) == hash(-2)
True
>>> len({-1: -1, -2: -2})
2

IOW, dictionary semantics are defined in terms of equality, not hashing. The hashing part should really be thought of as just a (somewhat exposed) implementation detail.

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

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26614>
_______________________________________


More information about the Python-bugs-list mailing list