[Python-Dev] cmp(x,x)

Tim Peters tim.one at comcast.net
Tue May 18 22:44:05 EDT 2004


[Armin Rigo]
>>> A minor semantic change that creeped in some time ago was an implicit
>>> assumption that any object x should "reasonably" be expected to
>>> compare equal to itself.

[Michael Hudson]
>> I have to admit that I haven't done my research, but I think you have
>> your history backwards: the recent change AIUI is that it's possible
>> that 'x == x' might be something *other* than Py_True...

[Armin]
> Uh?  You're probably referring to a much older change.  It was possible
> from at least Python 2.1, and I don't know if it has ever been
> impossible.

So-called "rich comparisons" were new in 2.1.  Before then, all comparisons,
however spelled, ended up invoking a spelling of cmp(), and
PyObject_Compare() has had its current

	if (v == w)
		return 0;

special case ever since revison 1.1 of object.c (that was about 14(!) years
ago now).

So, yes, before Python 2.1, it wasn't possible for x == x (or x <= x or x >=
x) to return false, or for x < x, x > x, or x != x to return true, or for
cmp(x, x) to return anything other than 0.  Rich comparisons changed all
that excepting the last.  At first in 2.1, and through all released Pythons
since then, the "v==w implies equal" special case has remained in
PyObject_Compare, but was left out of implementations of the 6 "new",
true/false relational operators.

> I am speaking about these two changes in object.c:
> ----------------------------
> revision 2.215
> date: 2004/03/21 17:01:44;  author: rhettinger;  state: Exp;  lines: +11 -
> 1
> Add identity shortcut to PyObject_RichCompareBool.
> ----------------------------

Raymond added that after you, Guido and I agreed to it at the PyCon sprints
in March.  It was specifically to ease your troubles with comparing
recursive dictionaries, which started after the hideous cyclic-compare hacks
were removed.  As CVS records, removing them was also your fault <wink>:

> and:
> 
> ----------------------------
> revision 2.211
> date: 2003/10/28 12:05:47;  author: arigo;  state: Exp;  lines: +8 -180
> Deleting cyclic object comparison.
> SF patch 825639
> http://mail.python.org/pipermail/python-dev/2003-October/039445.html





More information about the Python-Dev mailing list