Are there any list comparison optimizations in Python?

Peter Hansen peter at engcorp.com
Thu Nov 15 21:18:10 EST 2001


Huaiyu Zhu wrote:
> 
> 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.  

Well, you just pointed out one case, which is probably enough reason
not to add a special case to the language itself.

Other practical uses I can think of start with using it during
testing, to verify that a particular code path will actually
work if it is taken, or for troubleshooting a particularly
unclear problem which involves comparisons.

Not that it should necessarily matter.  If == is defined
as using __cmp__, then for consistency (principle of 
least surprise) one should be able to rely on that definition.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list