copysort patch, was RE: [Python-Dev] inline sort option
Mark Russell
marktrussell at btopenworld.com
Mon Oct 20 18:04:12 EDT 2003
On Mon, 2003-10-20 at 22:43, Raymond Hettinger wrote:
> Let's see what the use cases look like under the various proposals:
>
> [1] todo = [t for t in tasks.copysort() if due_today(t)]
> [2] todo = [t for t in list.sorted(tasks) if due_today(t)]
> [3] todo = [t for t in list(tasks, sorted=True) if due_today(t)]
Well, #3 is (I hope) a non-starter, given the need for the extra sort
keyword arguments. And the instance method is less capable - it can't
sort a non-list iterable (except via list(xxx).copysort()). So I would
definitely prefer #2, especially as I would tend to put:
sort = list.sorted
at the top of my modules where needed. Then I'd have:
todo = [t for t in sort(tasks) if due_today(t)]
genhistory(date, sort(events, key=incidenttime))
for f in sort(os.listdir()): . . .
which to me looks enough like pseudocode that I'm happy. This might
seem like an argument for having sort() as a builtin, but I think it's
still better as a list constructor. Adding "sort = list.sorted" to the
modules that need it is a small price to pay in boilerplate for the big
win of not cluttering the builtin namespace.
Mark Russell
More information about the Python-Dev
mailing list