classification
Title: Dict collision on boolean and integer values
Type: behavior Stage:
Components: Build Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, sbermeister
Priority: normal Keywords:

Created on 2012-05-24 10:38 by sbermeister, last changed 2012-05-24 10:48 by sbermeister. This issue is now closed.

Messages (3)
msg161497 - (view) Author: Sasha B (sbermeister) Date: 2012-05-24 10:38
Not sure if this is predicted behaviour, but if I make a dict like:

>>> x = {0: 'bar', True: 'foo'}

and modify True with 1, or 0 with False:
>>> x[False] = 'boo'
>>> x[1] = 'far'

the modifications happen:
>>> x
{0: 'boo', True: 'far'}

Is this expected behaviour? It seems that the hashes for 'False' and 0 are confused, as are the hashes for 'True' and 1.
msg161498 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-05-24 10:41
Thanks for the report.  Yes, this is expected.  Dictionary membership is based on equality of keys.  Since True and 1 are equal, only one of them can be present in a dictionary at a time (and a key lookup works with either).

>>> x = {0: 'bar'}
>>> x[0]
'bar'
>>> x[False]
'bar'
>>> x[0.0]
'bar'
>>> 0 == False
True
msg161499 - (view) Author: Sasha B (sbermeister) Date: 2012-05-24 10:48
Ahh, I see. You are correct. Thanks.
History
Date User Action Args
2012-05-24 10:48:47sbermeistersetmessages: + msg161499
2012-05-24 10:41:02mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg161498

resolution: not a bug
2012-05-24 10:39:05sbermeistersettype: behavior
components: + Build
versions: + Python 2.7
2012-05-24 10:38:30sbermeistercreate