[Python-Dev] cmp(x,x)

Jewett, Jim J jim.jewett at eds.com
Tue May 25 13:58:52 EDT 2004


[Jim J Jewett]
>> CVS has previously restricted the shortcut to those two,
>> but I agree it should be all or nothing.

[Tim Peters]
> The rich comparison PEP explicitly promised that, for example, 
> x <= y is not necessarily the same as "x < y or x == y":  

> ...it's also not kosher to infer x != y is False from 
> that x == y is True.

With consistent orderings, "x <= y" is strictly weaker than 
"x < y or x == y"; they differ only when a pair of objects 
don't know *which* of "<" and "==" is true.

If we do not assume consistency, then "is" does not even
imply "==", let alone "not !="

> Any shortcuts based on object identity are optimizations, 
> and dubious ones

Agreed. 

> because they violate promises made in that PEP. 

PEP 207 http://www.python.org/peps/pep-0207.html said that 
you can't (generally) reason from one rich compare to 
another.  It said nothing about reasoning from "is" to any 
of the comparisons directly.  So this optimization may 
violate the spirit of __eq__, but it doesn't violate the
letter of the PEP.

> There's a strong pragmatic ("go fast") case for starting to
> infer "x == y" and "not x != y" from "x is y", but that
> doesn't extend to inferring others from "x is y"

For normal objects, consistency holds.

When consistency does not hold, even Py_EQ and Py_NE are wrong.  
Doing (only) the other operations correctly won't fix the whole 
program; it will just make the bugs less obvious (and therefore 
harder to catch).

-jJ



More information about the Python-Dev mailing list