[Python-Dev] Re: Re: lists v. tuples

Aahz aahz@pythoncraft.com
Sun, 16 Mar 2003 10:37:14 -0500

On Sun, Mar 16, 2003, Guido van Rossum wrote:
> I realized the first sentence wasn't very clear.  I meant that
> implementing cmp() is inefficient without __cmp__ for some types
> (especially container types).  Example:
>  cmp(range(1000)+[1], range(1000)+[0])
> If the list type implements __cmp__, each of the pairs of items is
> compared once.  OTOH, if the list type only implemented __lt__, __eq__
> and __gt__, cmp() presumably would have to try one of those first, and
> then another one.  If it picked __lt__ followed by __eq__, it would
> get two False results in a row, meaning it could return 1 (cmp()
> doesn't really expect incomparable results :-), but at the cost of
> comparing each pair of items twice.  If cmp() picked another set of
> two operators to try, I'd simply adjust the example.

That's something I've been thinking about.  I use cmp() for that purpose
in the BCD module, because I do need the 3-way result (and it appears
that Eric kept that).  OTOH, it's certainly easy enough to define a
cmp() function, and not having the builtin wouldn't kill performance.
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

Register for PyCon now!  http://www.python.org/pycon/reg.html