Python 2.x breaks cmp() (was Re: A suspected bug)

Tim Peters tim.one at home.com
Sun Feb 25 00:15:58 EST 2001


[Scottie]
> ...
> I am trying to put together a nice priority queue package.
> With the new ops, I feel I should standardize my code to use a single
> test, rather than willy-nilly use < and <= in various places.
> What opinions do others have on whether I should use "<" or "<="?
> I realize exceptions may get thrown or strange orders result from
> using non-total-ordering relations (I view as a user problem).
>
> So here's my question:
> I'm building these priority queues on either < (>) or <= (>=).  If you
> were to build only one of these two on a type which would it be?
> Which of the two would you expect to be used in such a package?
> How should I make this choice?

Python's list.sort() originally used a full-blown cmp(), but in
*anticipation* of rich comparisons I rewrote it some years ago to only test
whether cmp did or did not return a value less than 0.  In other words,
rewrote it to *conceptually* use only <.

Rich comparisons finally are (will be) in 2.1, and Guido changed the
list.sort() comparison routine again to finish that job:  if Py_LT alone
settles the issue, that's all we ever call now.  So I expect that will
become a de facto standard.  Note that the standard module bisect.py also
only uses < (and, btw, note that bisect.insort() (also bisect.insort_right()
in 2.1) is very handy for building priority queues today (but "small"
ones -- it works with a straight Python list, so inserts "near the front"
can get unboundedly expensive as the list grows)).

> and-what-is-the-meaning-of-life-ly yours,

asking-the-question-prevents-the-answer-from-being-heard-ly y'rs  - tim





More information about the Python-list mailing list