Python 3.0, rich comparisons and sorting order

Carlos Ribeiro carribeiro at gmail.com
Tue Sep 21 12:50:41 EDT 2004


Reading PEP-3000, one proposed change caught my attention. I've
checked the Python Wiki, and here it is:

-> Raise an exception when making comparisons between two 
   incongruent types (other than equality and inequality.)
-> Reason: such comparisons do not make sense and are especially 
   confusing to new users of Python.

While I agree that having comparison between values of different types
(classes?) may be confusing, it is useful to define the sorting order.
I thought that the idea (since 2.1, and even more with 2.4) is the
sort ordering would rely on the rich comparison methods.

For example: today's lists may contain objects of arbitrary types and
can be sorted; even if the actual ordering may seem arbitrary, it
works for most purposes. My question is: If Python 3.0 abolishes
comparison between arbitrary types, how will generic sorting be
handled?

My guess is that it will not be possible unless you provide a generic
__cmp__ comparison function (that was the pre-2.1 method, I think).
The underlying assumption is that heterogeneous lists are not supposed
to be sorted, unless the programmer really knows what he's doing.
(BTW, for all purposes the same can be said about the use of the new
key argument provided by 2.4).

p.s. The definitive answer to this question (if any) should be
included in PEP3000.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list