Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are (lt, gt) and (le, ge) reflections instead of (lt, ge) and (le, gt)?

Tags:

python

In Basic customisation, the python docs state about comparison methods:

[no swapped-argument versions of these methods]; rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection.

I'd be less surprised by __lt__() and __ge__() being each other’s reflection(, as well as __le__() and __gt__()).

While the docs also state:

… no other implied relationships among the comparison operators, for example, the truth of (x<y or x==y) does not imply x<=y,

what, if anything, is the or would be a rationale for the reflection relations chosen?

like image 803
greybeard Avatar asked Nov 24 '16 16:11

greybeard


2 Answers

Because a < b and b > a are equivalent, as are a <= b and b >= a.

like image 150
RemcoGerlich Avatar answered Nov 14 '22 21:11

RemcoGerlich


Reflection means swapping the operands, not applying "not" to the operator.

__lt__(a,b)
    # if we don't know what to do, call
    return __gt__(b,a)

You were thinking the following

__lt__(a,b)
    # if we don't know what to do 
    return not __ge__(a,b)

But that's not what reflection means.

like image 34
Sanjay Manohar Avatar answered Nov 14 '22 20:11

Sanjay Manohar