Are there any list comparison optimizations in Python?

Huaiyu Zhu huaiyu at
Thu Nov 15 19:59:59 CET 2001

On Wed, 14 Nov 2001 12:33:32 -0500, Steve Holden <sholden at> wrote:
>Once you create a class where instances are not equal to themselves then
>common sense goes out of the window, and readability is no longer your major
>problem. If two lists contain items that compare unequal, then surely by
>definition the lists are unequal, and so *not* short-circuiting the
>comparison gives correct behavior.

It is true that once objects can be unequal to themselves a list of such
objects can be uneqal to itself too.

So my question really is this: Is there any major practical use for an
object to be unequal to itself?  Would it be better to just define (a==b) to
take a shortcut of (a is b), always?

Yes, I know NaN is a special case.  But I do not really see any practical
use of (NaN==NaN)==0, because it violates

	(a!=b) == not (a==b)

anyway.  A case for 

	(NaN==NaN) == (NaN!=NaN) == NaN

might be more reasonable.  In any case this should not be a major concern,
as currently the treatment of NaN is severely crippled anyway.  To remove a
fundamental semantic constraint (a is b implies a==b) due to such an ad hoc
convention has quite dubious merit, to say the least.

I'm also troubled by the loss of well-orderedness of the new rich comparison
scheme.  I think cmp should be applicable to any objects, and sort should be
applicable to any list.  This should not be affected by the fact that there
is no "natural" well-order on some sets, such as complex numbers.  Python
can do cmp(1,"1").  Why can't it do cmp(1, 1j)?

Overall I think most of these problems stem from the misguided belief that
there should be either one "natural" order on a set, or there is not order.
This belief orginates from observation on the set of real numbers but it is
not valid on much of anything else.


More information about the Python-list mailing list