[Python-Dev] decorate-sort-undecorate

Phillip J. Eby pje at telecommunity.com
Mon Oct 13 21:53:33 EDT 2003


At 07:45 PM 10/13/03 -0400, Tim Peters wrote:
>[Phillip J. Eby]
> > Why not just have the decoration be (key,index,value) then?  Why does
> > the key function need the index?
>
>It doesn't if indices are synthesized by magic under the covers.

It's not magic if that's the defined behavior, e.g.:

"""Specifying a 'key' callable causes items' sort order to be determined by 
comparing 'key(item)' in place of the item being compared.  In the event 
that 'key()' returns an equal value for two different items, the items' 
order in the original list is preserved.

The 'key' callable is called only once for each item in the list, so in 
general sorting with 'key' is faster than sorting with 'cmpfunc'.  It 
requires more memory, however, because it creates a temporary list of 
'(key(item),original_item_position,item)' tuples in order to perform the 
sort."""


>If you want to pay that expense now, you do so
>explicitly, and nothing about it is hidden.

What expense?  The extra memory overhead for the index?  I suppose so.  But 
if you *don't* want that behavior, you can still DSU manually, no?




More information about the Python-Dev mailing list