NaN comparisons - Call For Anecdotes

Chris Angelico rosuav at gmail.com
Tue Jul 8 17:31:41 CEST 2014


On Wed, Jul 9, 2014 at 1:24 AM, Skip Montanaro <skip at pobox.com> wrote:
> On Tue, Jul 8, 2014 at 10:19 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> For hash keys, float object identity will successfully look them up:
>>>>> d={}
>>>>> d[float("nan")]=1
>>>>> d[float("nan")]=2
>>>>> x=float("nan")
>>>>> d[x]=3
>>>>> d[x]
>> 3
>>>>> d
>> {nan: 1, nan: 2, nan: 3}
>
> Neat!

Yeah. It's one of those arguable points; is it a mere optimization
that dict lookup does an identity check before an equality check, or
is it crucial to other invariants (like the iteration one - if you
iterate over items(), it should give exactly the same results as
iterating over keys() and then looking things up)? Obviously it's
better for the dict to use equality checks rather than identity checks
(otherwise, at the very least, you'd have to explicitly intern all
strings used as dict keys - that'd just be ridiculous), but with
objects that don't compare equal to themselves, what should be done?

I think Python's picked a quite reasonable approach.

ChrisA



More information about the Python-list mailing list