Python 3.0 - is this true?

Arnaud Delobelle arnodel at googlemail.com
Sun Nov 9 13:45:57 EST 2008


"Martin v. Löwis" <martin at v.loewis.de> writes:

>> def comp(x1, x2):
>>    try:
>>        if x1<x2:
>>            return -1
>>        else:
>>            return 1
>>    except TypeError:
>>        if str(x1)<str(x2):
>>            return -1
>>        else:
>>            return 1
>> 
>
> Please correct me if I'm wrong, but I think this is not transitive. If
> strings and ints are uncomparable, this will give you 20 < "5" and
> "5" < 8, but not 20 < 8. As a result, quicksort will do nonsense
> with that comparison function (i.e. it won't guarantee that things
> are sorted in increasing order)

Even in 2.x it doesn't work (I think I posted this earlier but I'm not
sure anymore) as this example shows:

    2 < 3j and 3j < True, but True < 2

-- 
Arnaud
 



More information about the Python-list mailing list