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

Guido van Rossum guido@python.org
Sun, 16 Mar 2003 07:32:04 -0500


> Guido> Yes.  And I'm still hoping to remove __cmp__; there should be
> Guido> only one way to overload comparisons.
> 
> Moreover, for some data structures, the __cmp__ approach can be
> expensive.  For example, if you're comparing sequences of any kind,
> and you know that the comparison is for == or !=, you have your answer
> immediately if the sequences differ in length.  If you don't know
> what's being tested, as you wouldn't inside __cmp__, you may spend a
> lot more time to obtain a result that will be thrown away.

Yes.  OTOH, as long as cmp() is in the language, these same situations
are more efficiently done by a __cmp__ implementation than by calling
__lt__ and then __eq__ or similar (it's hard to decide which order is
best).  So cmp() should be removed at the same time as __cmp__.

And then we should also change list.sort(), as Tim points out.  Maybe
we can start introducing this earlier by using keyword arguments:

  list.sort(lt=function)     sorts using a < implementation
  list.sort(cmp=function)    sorts using a __cmp__ implementation

--Guido van Rossum (home page: http://www.python.org/~guido/)