New submission from Géry <gery.o...@gmail.com>:

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.

----------
components: Interpreter Core
messages: 363426
nosy: maggyero
priority: normal
severity: normal
status: open
title: Why are the union relationships not implemented by default for ≤ and ≥?
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39862>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to