[Python-Dev] Rich comparison confusion
Guido van Rossum
Wed, 17 Jan 2001 11:09:27 -0500
> I'm a bit confused about Guido's rich comparison stuff. In the description
> he states that __le__ and __ge__ are inverses as are __lt__ and __gt__.
Yes. By this I mean that A<B and B>A are interchangeable, ditto for
A<=B and B>=A. Also A==B interchanges for B==A, and A!=B for B!=A.
> From a boolean standpoint this just can't be so. Guido mentions partial
> orderings, but I'm still confused. Consider this example: Objects of type A
> implement rich comparisons. Objects of type B don't. If my code looks like
> a = A()
> b = B()
> if b < a:
> My interpretation of the rich comparison stuff is that either
> 1. Since b doesn't implement rich comparisons, the interpreter falls
> back to old fashioned comparisons which may or may not allow the
> comparison of B objects and A objects.
> 2. The sense of the inequality is switched (a > b) and the rich
> comparison code in A's implementation is called.
It's case 2.
> That's my reading of it. It has to be wrong. The inverse comparison should
> be a >= b, not a > b, but the described pairing of comparison functions
> would imply otherwise.
We're trying very hard *not* to make any connections between a<b and
a>=b. You've learned in grade school that these are each other's
Boolean inverse (a<b is true iff a>=b is false). However, for partial
orderings this may not be true: for unordered a and b, none of a<b,
a<=b, a>b, a>=b, a==b may be true.
On the other hand, even for partially ordered types, a<b and b>a
(note: swapped arguments *and* swapped sense of comparison) always
give the same outcome!
> I'm sure I'm missing something obvious or revealing some fundamental failure
> of my grade school education. Please explain...
I think what threw you off was the ambiguity of "inverse". This means
Boolean negation. I'm not relying on Boolean negation here -- I'm
relying on the more fundamental property that a<b and b>a have the
--Guido van Rossum (home page: http://www.python.org/~guido/)