[Python-ideas] Automatic comparisons by default

Guido van Rossum guido at python.org
Tue Mar 15 19:47:01 CET 2011

On Tue, Mar 15, 2011 at 11:03 AM, Facundo Batista
<facundobatista at gmail.com> wrote:
> Two very related proposals:
> 1. On "!=", if Python doesn't find __ne__, use "not __eq__()".

+1 on this one. I cannot count how often I have written a base class
for this sole purpose. And I cannot think of any cases where it would
be the wrong thing, *except* those damn IEEE NaNs (which we can

> 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.

I'd be okay with offering a standard base class to supply this (#2)
behavior though.

> Some considerations:
> - You can give decorators that fill the spaces, but I think that would
> be less error prone, more clean and less surprising with those rules
> (specially the first one).
> - If somebody wants for != not to be the opposite of ==, both methods
> can be defined. The same for __le__ and __ge__.
> - All that would be needed for comparisons (for those normal objects
> where those rules apply) would be __le__ and __eq__, which is what you
> need to replace __cmp__.
> - *I* don't know if this behaviour will break something (couldn't find a case).
> What do you think?

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list