Are there any list comparison optimizations in Python?

Tim Peters tim at
Tue Nov 13 17:54:47 CET 2001

[Michael Hudson]
> I'm pretty sure that in Python <2.1 this "optimization" was enabled
> for all comparisons (i.e. if "x is y", then "x == y" returns 1 without
> calling any comparison methods).


> It disappeared when rich comparisons turned up; for one thing NaNs
> sohuld not compare equal to themselves according to ieee754 (tho' they
> still seem to in 2.2).

This is a platform-dependent crapshoot:  it depends on what the platform C
does.  For paradoxical(*) reasons, it gives the illusion of working on
Windows (under MSVC) today:

>>> x = 1e300
>>> x *= x     # build an infinity
>>> x
>>> n = x - x  # build a NaN
>>> n
>>> n == n

OTOH, 754 doesn't specify language bindings, and Python doesn't say anything
about 754, so the notion that Python "==" maps to 754's equality operator is
presumption on both ends <wink>.

(*) It's "paradoxical" because MSVC doesn't generate correct fp comparison
from 754's viewpoint.  But toss in a few other accidents, and by the time
Python reports a result, an even number of mistakes cancel out.

More information about the Python-list mailing list