Fast attribute/list item extraction

Bengt Richter bokr at oz.net
Mon Dec 1 11:29:08 EST 2003


On Mon, 01 Dec 2003 10:11:16 GMT, "Raymond Hettinger" <vze4rx4y at verizon.net> wrote:

>[Peter Otten]
>> > extract[1]
>> > extract["key"]
>
>[Robert Brewer]
>> I'm having a hard time seeing the use cases, given that I find
>> most of them more readable if done with list comprehensions
>> or good ol' for loops.
>
>Peter's post focused on implementation instead of the context.
>
>For Py2.4, the list.sort() will have an optional key argument that encapsulates
>the decorate/sort/undercorate pattern.  For example, here is the new fastest way
>to have a case insensitive sort leaving the original case intact:
>
>>>> words = 'The quick BROWN fox JumPed OVER the LAzy dog'.split()
>>>> words.sort(key=str.lower)
Please can we have a name that better expresses the functionality?

E.g., 'keyfunc', or 'xform' or something that says the method is expecting
a function that will be called with the sorting argument to produce the value
used in sorting? "key=" doesn't do it IMO.

In fact, 'key' could well be used to *select* a key from indexable objectss being sorted.
E.g., key=1 could provide an efficient way to do the typical value-based sort on
dictionary 2-tuple items.

Regards,
Bengt Richter




More information about the Python-list mailing list