Question about sorted in Python 3.0rc1

Terry Reedy tjreedy at udel.edu
Mon Sep 22 19:55:50 CEST 2008


josh logan wrote:

Here is a minimal example showing the problematic behavior.

class Int():
     def __init__(self, i):
         self.i = i
     def __cmp__(self, other):
         return cmp(self.i, other.i)

Is = [Int(i) for i in range(8)]
Is.sort()  # throws TypeError: unorderable types Int() < Int()
sorted(Is) # ditto, if above not present

The 3.0b2 version of LibRef/ Built-in Types/ Comparisions says
"Instances of a class cannot be ordered with respect to other instances 
of the same class, or other types of object, unless the class defines 
enough of the methods __cmp__(), __lt__(), __le__(), __gt__(), and 
__ge__() (in general, either __cmp__() or both __lt__() and __eq__() are 
sufficient, if you want the conventional meanings of the comparison 
operators).

The notes for Mutable Sequence .sort() say nothing more.  So the 
exception appears to be a bug, perhaps left over from when there was a 
plan (since aborted) to delete cmp and __cmp__.  If the 3.0c1 version of 
the docs say the same, and no one says otherwise, I would file a report 
on the tracker at bugs.python.org, using the minimal example above.

Terry Jan Reedy




More information about the Python-list mailing list