[Python-ideas] Automatic comparisons by default

Masklinn masklinn at masklinn.net
Tue Mar 15 20:17:05 CET 2011

On 2011-03-15, at 19:47 , Guido van Rossum wrote:
>> 2. On "<=", if Python doesn't find __le__, use "__eq__() or __lt__()".
>> The same for ">=", of course.
> Big -1 for this. Inequalities (orderings) are much more subtle than
> equalities. See e.g. sets.
Still, there should only be need for two operators to provide total ordering, and having to reimplement every single operator correctly is probably trickier than implementing just two of them.

Haskel's ``Ord``, for instance, only requires (minimal complete definition) that either ``<=`` or ``compare`` (__le__ and __cmp__, respectively) be implemented, and Ord derives from Eq so Eq also needs to be defined, requiring either ``==`` (__eq__) or ``/=`` (__ne__) (that you have to define either ``<=`` or ``compare`` at least and not, say, ``>`` comes from the way the ``Ord`` typeclass is defined: all operators call ``compare`` and ``compare`` calls ``==`` and ``<=``, so the circularity is broken either by redefining ``compare`` or by redefining the ``<=`` operator it uses).

Technically, through Python's dynamism, it should be possible to ask for any of the 4 ordering operator and any of the two (in)equality ones and then do the right thing in all situations.

More information about the Python-ideas mailing list