[Python-Dev] functools.compose to chain functions together

Antoine Pitrou solipsis at pitrou.net
Mon Aug 17 12:59:40 CEST 2009


Le lundi 17 août 2009 à 09:07 +0200, "Martin v. Löwis" a écrit :
> Ok, that's also what the patch has proposed. I was puzzled when I read
> 
>    l.sort(key=compose(itemgetter(1), itemgetter(0))))
> 
> because I expected it to mean
> 
>    l.sort(key=lambda x:x[1][0])

But that's itemgetter's fault, not compose's. Because itemgetter's
obvious equivalent (the [] operator) uses postfix notation, combining
several itemgetters reverses the lexical order of appearance.

Besides, the argument order is similar to the one in the function
composition notation in mathematics (which isn't really advanced stuff
and should have been taught to every former scientific/technical student
out there).

Regards

Antoine.




More information about the Python-Dev mailing list