[issue4296] Python assumes identity implies equivalence; contradicts NaN

Mark Dickinson report at bugs.python.org
Tue Nov 11 11:52:51 CET 2008


Mark Dickinson <dickinsm at gmail.com> added the comment:

This is indeed interesting.  For what it's worth, I think
the Python 3.0 behaviour is the right one, here.  Perhaps
it's worth adding a test to Python 3.0 to make sure that
this behaviour doesn't accidentally disappear, or at least
to make sure that someone thinks about it before making
the behaviour disappear.

But in general I don't think the fact that NaNs are weird
should get in the way of optimizations.  In other words,
I'm not sure that Python should be asked to guarantee
anything more than "b == b" returning False when b is
a NaN.  It wouldn't seem unreasonable to consider
behaviour of nans in containers (sets, lists, dicts)
as undefined when it comes to equality and identity
checks.

There are other questionable things going on when NaNs
are put into containers.  For example:

>>> a = float('nan')
>>> b = [a]
>>> a in b
True

A strict reading of the definition of 'in' would say
that "a in b" should return False here, since a is not
equal to any element of b.  But I presume that the 'in'
operator does identity checks under the hood before
testing for equality.  'Fixing' this so that nans did
the right thing might slow down a lot of other code
just to handle one peculiar special case correctly.

Michael, is this adversely affecting real-world code?
If not, I'd be inclined to say that it's not worth
changing Python's behaviour here.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4296>
_______________________________________


More information about the Python-bugs-list mailing list