[Python-ideas] list.sort with a int or str key

Masklinn masklinn at masklinn.net
Fri Sep 17 06:49:21 CEST 2010


On 2010-09-17, at 08:41 , Terry Reedy wrote:
> On 9/16/2010 2:28 PM, Raymond Hettinger wrote:
>> The key= parameter is a protocol that is used across multiple tools min(). max(), groupby(), nmallest(), nlargest(), etc.  All of those would need to change to stay in-sync.
> ...
> 
>> ISTM, the performance would be about the same as you already get from attrgetter(), itemgetter(), and methodcaller().  Also, those three tools are already more flexible than the proposal, for example:
>> 
>> attrgetter('lastname', 'firstname')  # key = lambda r: (r.lastname, r.firstname)
>> itemgetter(0, 7)                     # key = lambda r: (r[0], r[7])
>> methodcaller('get_stats', 'size')    # key = lambda r: r.get_stats('size')
> 
> It is easy to not know about these. I think the doc set could usefully use an expanded entry on *key functions* (that would be a cross-reference link) that includes examples like the above.

+1, in my experience, the operator module in general is fairly unknown and the attrgetter/itemgetter/methodcaller family criminally so.

It doesn't help that they're kind-of lost in a big bunch of text at the very bottom of the module.


More information about the Python-ideas mailing list