list.sort(cmpfunc) question

Harri Pasanen harri at trema.com
Fri Mar 16 09:15:06 EST 2001


The list.sort(cmp) docs for Python 2.0 state:

   The sort() method takes an optional argument specifying a
   comparison function of two arguments (list items) which should
   return -1, 0 or 1 depending on whether the first argument is
   considered smaller than, equal to, or larger than the second
   argument.


The build in cmp() function docs state:


cmp (x, y)

   Compare the two objects x and y and return an integer according to
   the outcome. The return value is negative if x < y, zero if x == y
   and strictly positive if x > y.


So interpreted strictly the built in cmp() could not be used as
list.sort() argument, as it can apparently (and does) occasionally
return for instance the value 2.

In practice list.sort() seems to be happy with having a cmp function
that behaves as built in cmp(), but can this be relied upon?  And if
yes, maybe the doc should be changed?

Regards,

Harri






More information about the Python-list mailing list