[Python-Dev] decorate-sort-undecorate

Guido van Rossum guido at python.org
Tue Oct 14 15:19:22 EDT 2003

> I would rather wrap Tim's existing code than muck with assignment logic.
> Ideally, the performance of list.sort() should stay unchanged when the
> key function is not specified.

Impossible -- the aux objects tax the memory cache more.  Also the
characteristics of the data will be very different.

> Tim's original (key, index, value) idea seems to be simplest.  The only
> sticking point is the immortality of PyInts.  One easy, but not so
> elegant way around this is to use another mortal object for a tiebreaker
> (for example, "00000", "00001", ...).  Alternatively, is there a way of
> telling a PyInt to be mortal?

I still like custom objects better.

> Besides immortality and speed, another consideration is the interaction
> between the cmp function and the key function.  If both are specified,
> then the underlying decoration becomes visible to the user:
>     def viewcmp(a, b):
>         print a, b  # the decoration just became visible
>         return cmp(a,b)
>     mylist.sort(cmp=viewcmp, key=str.lower)
> Since the decoration can be visible, it should be as understandable as
> possible.  Viewed this way, PyInts are preferable to a custom object.

I think we should disallow specifying both cmp and key arguments.
Using both just doesn't make sense.

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

More information about the Python-Dev mailing list