[Python-ideas] Automatic total ordering

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Oct 17 00:25:33 CEST 2008

It still bothers me that there is no longer a way to
provide a single method that performs a three-way
comparison. Not only because total ordering is the
most common case, but because it makes comparing
sequences for ordering very inefficient -- you end up
comparing everything twice, once for < and once for =.

So I'd like to see this addressed somehow in any
scheme to revamp the comparison system.

One way would be to add a slot such as __compare__
that works like the old __cmp__ except that it can
return four possible results -- less, equal, greater
or not-equal.

Comparison operations would first look for the
corresponding individual method, and then fall back
on calling __compare__.

There would be a function compare(x, y) that first
looks for a __compare__ method, then falls back on
trying to find individual methods with which to
work out the result.

If we were to adopt something like this, it would
obviate any need for direct translations between the
comparison operations, and in fact any such direct
translations might get in the way.

So the bottom line is that I think such features
should be kept in mixins or decorators for the time
being, until this can all be thought through properly.


More information about the Python-ideas mailing list