Sorting Lists

Alex Martelli aleaxit at yahoo.com
Thu Aug 2 05:13:29 EDT 2001


"Tom Bryan" <tbryan at python.net> wrote in message
news:9jt3l9$bo2$1 at slb6.atl.mindspring.net...
    ...
> Of course, I never claimed that it was faster.  One simply avoids
> copying the list and sorts in place rather than returning a sorted
> copy of the list. :-)

If you don't like returning a sorted copy, just do:

def modify_in_place(alist, func_returning_a_copy, *whatever_else,
**kwds_too):
    alist[:] = func_returning_a_copy(alist, *whatever_else, **kwds_too)

oh, and, for completeness, the adapter in the other direction:

def return_a_copy(alist, func_modifying_in_place, *whatever_else,
**kwds_too):
    thecopy = alist[:]
    func_modifying_in_place(thecopy, *whatever_else, **kwds_too)
    return thecopy

It's cooler to do it in a functional programming style, of course -- get
a callable with the semantics you dislike (inplace/returning-copy) and
return a callable with the other semantics (returning-copy/inplace).
But that would get us into often-debated issues (such currying & wrapping
is much easier when taking advantage of nested scopes, etc, etc).


Alex






More information about the Python-list mailing list