copysort patch, was RE: [Python-Dev] inline sort option

Nick Coghlan ncoghlan at iinet.net.au
Mon Oct 20 08:58:35 EDT 2003


Raymond Hettinger strung bits together to say:

> Remember, list.copysort() isn't about chaining or even "saving a line or
> two".  It is about using an expression instead of a series of
> statements.
> That makes it possible to use it wherever expressions are allowed, 
> including function call arguments and list comprehensions.
> 
> Here are some examples taken from the patch comments:
> 
>   genhistory(date, events.copysort(key=incidenttime))
> 
>   todo = [t for t in tasks.copysort() if due_today(t)]

'chain' may be a bad name then, since all that function really does is take an 
arbitrary bound method, execute it and then return the object that the method 
was bound to. If we used a name like 'method_as_expr' (instead of 'chain'), then 
the above examples would be:

   genhistory(date, method_as_expr(list(events).sort, key=incidenttime))

   todo = [t for t in method_as_expr(list(tasks).sort) if due_today(t)]

Granted, it's not quite as clear (some might say it's positively arcane!), but 
it also isn't using anything that's not already in the language/standard library.

> The forces working against introducing an in-line sort are:
> * the time to copy the list (which Alex later showed to be irrelevant),
> * having two list methods with a similar purpose, and 
> * the proposed method names are less than sublime
> 
> If someone could come-up with a name more elegant than "copysort", I
> the idea would be much more appetizing.

Would something like 'sortedcopy' be an improvement?

Although Alex's suggestion of a class method like dict.fromkeys() also sounded 
good - naming it is still an issue, though.

I'm not entirely opposed to the idea (the 'method_as_expr' approach feels like 
something of a hack, even to me) - but the object method just doesn't seem to 
fit cleanly into the design of the basic types.

Cheers,
Nick.

-- 
Nick Coghlan           |              Brisbane, Australia
ICQ#: 68854767         |               ncoghlan at email.com
Mobile: 0409 573 268   |   http://www.talkinboutstuff.net
"Let go your prejudices,
               lest they limit your thoughts and actions."




More information about the Python-Dev mailing list