These special methods allow your class to specify what
happens when comparison operators such as “<=” are used, and the left-hand
operator is an instance of your class. (In most cases
the right-hand operator is also an instance of the same
class, but this is not actually required.)
In each case, the calling sequence for the method must look like this:
def __method__(self, other):
...
The self argument is the left-hand
operand and the other argument is the
operand on the right hand of the operator.
Each method must return a numeric value:
A negative number indicates that self precedes other.
Zero indicates that self and other are considered equal.
A positive number indicates that other precedes self.
If the method does not implement the operation, it
may return the special value NotImplemented.
| Operator | Method name |
|---|---|
== | __eq__ |
>= | __ge__ |
> | __gt__ |
<= | __le__ |
< | __lt__ |
!= | __ne__ |