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

nickeubank report at bugs.python.org
Tue Mar 22 23:04:00 EDT 2016


New submission from nickeubank:

Found an odd behavior I'd never known about today, not sure if it's a bug or known. Python 3.4.4 (anaconda)

True, False, 0, 1 can all be used as dictionary keys.

But

Apparently True and 1 hash to the same item and False and 0 hash to the same item, so they can easily overwrite (which I spent a while banging my head over today). In other words:

In[1]:
    d = {True: 'a', False: 'b'}
    d[0] = 'z'
    d[False]

Out[1]:
    'z'

----------
messages: 262232
nosy: nickeubank
priority: normal
severity: normal
status: open
title: False/0 and True/1 collision when used as dict keys?
versions: Python 3.4

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


More information about the Python-bugs-list mailing list