Would there be support for a more general cmp/__cmp__

Steve Holden steve at holdenweb.com
Thu Oct 20 12:53:52 CEST 2005

Antoon Pardon wrote:
> I was wondering how people would feel if the cmp function and
> the __cmp__ method would be a bit more generalised.
> The problem now is that the cmp protocol has no way to
> indicate two objects are incomparable, they are not
> equal but neither is one less or greater than the other.
> So I thought that either cmp could return None in this
> case or throw a specific exception. People writing a
> __cmp__ method could do the same.
The current behaviour is, of course, by design: """The operators <, >, 
==, >=, <=, and != compare the values of two objects. The objects need 
not have the same type. If both are numbers, they are converted to a 
common type. Otherwise, objects of different types always compare 
unequal, and are ordered consistently but arbitrarily."""

Personally I'm still not convinced that your requirement reflects a 
substantial use case (but then I'm getting used to that ;-). Just 
because an ordering is partial that doesn't mean that two instances of a 
class shouldn't be compared.

What would you have Python do when the programmer tries to perform an 
invalid comparison (i.e. what are the exact semantics imposed when 
__cmp__() returns None/raises an exception)?

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/

More information about the Python-list mailing list