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

Andrew Barnert abarnert at yahoo.com
Wed Sep 25 18:02:21 CEST 2013


On Sep 25, 2013, at 7:11, "Vernon D. Cole" <vernondcole at gmail.com> wrote:

>> 
>> > 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}

While some of these are odd things to use as keys, they don't have any odd behavior with equality except nan. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130925/69b6e0f5/attachment.html>


More information about the Python-ideas mailing list