Would there be support for a more general cmp/__cmp__

Antoon Pardon apardon at forel.vub.ac.be
Tue Oct 25 16:18:37 CEST 2005


Op 2005-10-25, Christopher Subich schreef <csubich.spam.block at spam.subich.block.com>:
> Antoon Pardon wrote:
>> It *is* a definition of an ordering.
>> 
>> For something to be an ordering it has to be anti symmetric and transitive.
>> 
>> The subset relationships on sets conform to these conditions so it is a (partial)
>> ordering. Check your mathematic books, Why you would think this is abuse is beyond me
>
> Which is exactly why a < b on sets returns True xor False, but cmp(a,b) 
> throws an exception.

I don't see the conection.

The documentation states that cmp(a,b) will return a negative value if
a < b. So why should it throw an exception?

> a <COMPARE> b is a local comparison, asking only for the relationship 
> between two elements.  In some bases, like the complex numbers, some 
> comparisons are ill-defined.; in others, like sets, they're well-defined 
>   but don't give a total ordering.
>
> cmp(a,b) asks for their relative rankings in some total ordering.

The documentation doesn't state that. I also didn't find anything in
the documentation on how the programmer should code in order to
enforce this.

So someone who just use the rich comparisons to implement his
partial order will screw up this total order that cmp is somehow
providing.

And cmp doesn't provide a total ordering anyway. Sets are clearly
uncomparable using cmp, so cmp doesn't provide a total order.

Maybe the original idea was that cmp provided some total ordering,
maybe the general idea is that cmp should provide a total ordering,
but that is not documented, nor is there any documentation in
helping the programmer in this. 

And even if we leave sets aside it still isn't true.

>>> from decimal import Decimal
>>> Zero = Decimal(0)
>>> cmp( ( ) , Zero)
-1
>>> cmp(Zero, 1)
-1
>>> cmp(1, ( ) )
-1


> For a 
> space that does not have a total ordering, cmp(a,b) is meaningless at 
> best and dangerous at worst.

The current specs and implemantation are.

I see nothing wrong with a function that would provide four kinds of
results when given two elements. The same results as cmp gives now
when it applies and None or other appropiate value or a raised
exception when not.

Given how little this functionality differs from the current cmp,
I don't see why it couldn't replace the current cmp.

> It /should/ throw an exception when the 
> results of cmp aren't well-defined, consistent, antisymmetric, and 
> transitive.

That an order is not total in no way implies any of the above.
The order on sets is well-defined, consistent, antisymmetric
and transitive.

Python may not be able to handle it in a well-defined, consistent
manner, but that is IMO a problem of python not of partial orders.

-- 
Antoon Pardon



More information about the Python-list mailing list