[Python-Dev] cmp(x,x)

Tim Peters tim.one at comcast.net
Tue May 25 12:58:07 EDT 2004


[Gustavo Niemeyer]
>> Looking again, this is true for any operation, not only
>> Py_EQ or Py_NE.

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

It could for lists specifically, but not for objects in general.  The rich
comparison PEP explicitly promised that, for example, x <= y is not
necessarily the same as "x < y or x == y":  from x == y, we're not allowed
to infer that x <= y, or x >= y, or not x < y, or not x > y.  For that
matter, the PEP

    http://www.python.org/peps/pep-0207.html

also promised

   3 The == and != operators are not assumed to be each other's
     complement

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

Any shortcuts based on object identity are optimizations, and dubious ones
because they violate promises made in that 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"
(PyObject_RichCompareBool gets invoked with Py_EQ and Py_NE all over the
place in the core; it's never invoked with Py_GT, Py_GE, or Py_LE; there are
a few invocations with Py_LT, mostly involved with sorting).




More information about the Python-Dev mailing list