Would there be support for a more general cmp/__cmp__
Antoon Pardon
apardon at forel.vub.ac.be
Thu Oct 20 07:13:28 EDT 2005
Op 2005-10-20, Steve Holden schreef <steve at holdenweb.com>:
> 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.
I'm not suggesting that they 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)?
My appologies, I should have been more complete.
What I want is a way to say that all of the following are False:
a < b, a <= b, a == b, a > b, a >= b
and that only the following is True:
a != b
So if a coder writes one of the comparisons and as a result python
calls the __cmp__ method on one of the terms which would return
either None or raise an exceptions I would like it to reflect
the above behaviour.
--
Antoon Pardon
More information about the Python-list
mailing list