No subject
Nick Eubank
nickeubank at gmail.com
Tue Mar 22 23:17:07 EDT 2016
Hello All,
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'
I understand that True and False are sub-types of ints, but it's not clear
to me why (i.e. certainly didn't feel intuitive) that they would be treated
the same as keys.
Relatedly, if this is a desired behavior, any advice one how best to work
with dictionaries when one wants "True" and 1 to be different? I'm working
on a function that accepts arguments that may be "True" or 1 (meaning very
different things) and am seeking a pythonic solution...
More information about the Python-list
mailing list