Message363426
Mathematically, the [binary relation](https://en.wikipedia.org/wiki/Binary_relation) ≤ is the [union](https://en.wikipedia.org/wiki/Binary_relation#Union) of the binary relations < and =, while the binary relation ≥ is the union of the binary relations > and =. So is there a reason why Python does not implement `__le__` in terms of `__lt__` and `__eq__` by default, and `__ge__` in terms of `__gt__` and `__eq__` by default?
The default implementation would be like this (but probably in C for performance, like `__ne__`):
```python
def __le__(self, other):
result_1 = self.__lt__(other)
result_2 = self.__eq__(other)
if result_1 is not NotImplemented and result_2 is not NotImplemented:
return result_1 or result_2
return NotImplemented
def __ge__(self, other):
result_1 = self.__gt__(other)
result_2 = self.__eq__(other)
if result_1 is not NotImplemented and result_2 is not NotImplemented:
return result_1 or result_2
return NotImplemented
```
This would save users from implementing these two methods all the time.
Here is the relevant paragraph in the [Python documentation](https://docs.python.org/3/reference/datamodel.html#object.__lt__) (emphasis mine):
> By default, `__ne__()` delegates to `__eq__()` and inverts the result
> unless it is `NotImplemented`. There are no other implied
> relationships among the comparison operators, **for example, the truth
> of `(x<y or x==y)` does not imply `x<=y`.**
*Note.* — These union relationships are always valid, contrary to the following relationships which are only valid for [total orders](https://en.wikipedia.org/wiki/Binary_relation#Properties) (also called connex orders) and therefore not implemented by default: < is the [complement](https://en.wikipedia.org/wiki/Binary_relation#Complement) of ≥, and > is the complement of ≤. These complementary relationships can be easily implemented by users when they are valid with the [`functools.total_ordering`](https://docs.python.org/3/library/functools.html#functools.total_ordering) class decorator provided by the Python standard library. |
|
Date |
User |
Action |
Args |
2020-03-05 10:29:41 | maggyero | set | recipients:
+ maggyero |
2020-03-05 10:29:41 | maggyero | set | messageid: <1583404181.09.0.848601767593.issue39862@roundup.psfhosted.org> |
2020-03-05 10:29:41 | maggyero | link | issue39862 messages |
2020-03-05 10:29:40 | maggyero | create | |
|