[Python-ideas] Automatic comparisons by default

Facundo Batista facundobatista at gmail.com
Tue Mar 15 20:27:23 CET 2011

On Tue, Mar 15, 2011 at 3:18 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:

> It's a little more complicated than "if Python doesn't find ...".
> In Python 3, object() already has __le,__, __gt__, __ge__, and __gt__,
> so those methods always get found.

I'm seeing that it also defines __eq__ and __ne__, but in this case we
could make __ne__ to return NotImplemented, and automatically call
"not __eq__()" (that for object() is the opposite).

> You can use an identity check to see if those methods have been
> overridden, but I think the only truly correct way to tell if one of the
> rich comparison methods is defined is to call it and see whether
> it returns NotImplemented.

Not following... object.__lt__ raises TypeError, does not return NotImplemented.

We could make object() to return NotImplemented for __le__ and __ge__,
and in such case it would call "__eq__() or __lt__()" (returning True
if the object is the same, or raising TypeError to prevent ordering
the unorderable object)

.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

More information about the Python-ideas mailing list