[Python-Dev] PyObject_RichCompareBool identity shortcut
Glenn Linderman
v+python at g.nevcal.com
Thu Apr 28 02:05:35 CEST 2011
On 4/27/2011 2:04 PM, Mark Dickinson wrote:
> On Wed, Apr 27, 2011 at 10:37 AM, Hrvoje Niksic<hrvoje.niksic at avl.com> wrote:
>> The other day I was surprised to learn this:
>>
>>>>> nan = float('nan')
>>>>> nan == nan
>> False
>>>>> [nan] == [nan]
>> True # also True in tuples, dicts, etc.
> That one surprises me a bit too: I knew we were using
> identity-then-equality checks for containment (nan in [nan]), but I
> hadn't realised identity-then-equality was also used for the
> item-by-item comparisons when comparing two lists. It's defensible,
> though: [nan] == [nan] should presumably produce the same result as
> {nan} == {nan}, and the latter is a test that's arguably based on
> containment (for sets s and t, s == t if each element of s is in t,
> and vice versa).
>
> I don't think any of this should change. It seems to me that we've
> currently got something approaching the best approximation to
> consistency and sanity achievable, given the fundamental
> incompatibility of (1) nan breaking reflexivity of equality and (2)
> containment being based on equality. That incompatibility is bound to
> create inconsistencies somewhere along the line.
>
> Declaring that 'nan == nan' should be True seems attractive in theory,
> but I agree that it doesn't really seem like a realistic option in
> terms of backwards compatibility and compatibility with other
> mainstream languages.
I think it should change. Inserting a NaN, even the same instance of
NaN into a list shouldn't suddenly make it compare equal to itself,
especially since the docs (section 5.9. Comparisons) say:
*
Tuples and lists are compared lexicographically using comparison
of corresponding elements. This means that to compare equal, each
element must compare equal and the two sequences must be of the
same type and have the same length.
If not equal, the sequences are ordered the same as their first
differing elements. For example, [1,2,x] <= [1,2,y] has the same
value as x <= y. If the corresponding element does not exist, the
shorter sequence is ordered first (for example, [1,2] < [1,2,3]).
The principle of least surprise, says that if two unequal items are
inserted into otherwise equal lists, the lists should be unequal. NaN
is unequal to itself.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20110427/c2f16595/attachment.html>
More information about the Python-Dev
mailing list