float("nan") in set or as key

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun May 29 04:51:33 EDT 2011


On Sun, 29 May 2011 10:32:43 +1000, Chris Angelico wrote:

> On Sun, May 29, 2011 at 10:28 AM, Albert Hopkins
> <marduk at letterboxes.org> wrote:
>> This is the same nan, so it is equal to itself.
>>
>>
> Actually, they're not. But it's possible the dictionary uses an 'is'
> check to save computation, and if one thing 'is' another, it is assumed
> to equal it. That's true of most well-behaved objects, but nan is not
> well-behaved :)

*Exactly* correct.

NAN != NAN even if they are the same NAN, by design. This makes NANs ill-
behaved, but usefully so. Most (all?) Python built-ins assume that any 
object X is equal to itself, so they behave strangely with NANs.


-- 
Steven



More information about the Python-list mailing list