Author mikecurtis
Recipients mikecurtis
Date 2008-11-11.01:44:40
SpamBayes Score 7.58846e-11
Marked as misclassified No
Message-id <>
Found in Python 2.4; not sure what other versions may be affected.

I noticed a contradiction with regards to equivalence when experimenting
with NaN, which has the special property that it "is" itself, but it
doesn't "==" itself:

>>> a = float('nan')
>>> a is a
>>> a == a
>>> b = [float('nan')]
>>> b is b
>>> b == b

I am not at all familiar with Python internals, but the issue appears to
be in PyObject_RichCompareBool of python/trunk/Objects/object.c

This method "Guarantees that identity implies equality".  However, this
doesn't "Gaurantee" this fact, but instead "Assumes" it, because it is
not something that is always True.  NaN is identical to itself, but not
equivalent to itself.

At a minimum, the contradiction introduced by this assumption should be
documented.  However, it may be possible to do better, by fixing it. 
The assumption appears to be made that identity should imply
equivalence, for the common case.  Would it therefore be possible to,
instead of having objects such as lists perform this optimization and
make this assumption, instead have the base object types implement this
assumption.  That is, for regular objects, when we evaluate equivalence,
we return True if the objects are identical.  Then, the optimization can
be removed from objects such as list, so that when they check the
equivalence of each object, the optimization is performed there.  NaN
can then override the default behavior, so that it always returns False
in equivalence comparisons.
Date User Action Args
2008-11-11 01:44:44mikecurtissetrecipients: + mikecurtis
2008-11-11 01:44:42mikecurtissetmessageid: <>
2008-11-11 01:44:41mikecurtislinkissue4296 messages
2008-11-11 01:44:40mikecurtiscreate