[Python-ideas] Subject: Re: `OrderedDict.sort`

Vernon D. Cole vernondcole at gmail.com
Wed Sep 25 16:11:41 CEST 2013


>
>
> > And yes, NaN is a problem, but it's exactly the same problem it is
> > everywhere else in Python.
>
> I was serious about wanting to know how dictionaries handle NaN as a
> key. Is it a special case? The obvious way of implementing it would
> conclude it is a hash collision but not a match. I notice that
> Decimal('NaN') and float nan don't match each other (as do any other
> float/Decimal with the same value) but they do both work as dictionary
> keys.
>

NaN is, by definition, never equal to another NaN, which is why the
following happens:

>>> nan = float('NAN')
>>> n2 = nan
>>> n2 == nan
False
>>> n2 is nan
True

It turns out that many other things which I never thought about before can
be dictionary keys...

>>> d = {'a':1, nan: 2}
>>> d[n2]
2
>>> d[NotImplemented] = 3
>>> d[...] = 4
>>> d[None] = 5
>>> d[True] = 6
>>> d[False] = 7
>>> d
{'a': 1, nan: 2, False: 7, True: 6, NotImplemented: 3, Ellipsis: 4, None: 5}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130925/88006fb6/attachment.html>


More information about the Python-ideas mailing list