Function composition

Cees de Groot cg at gaia.cdg.acriter.nl
Thu Aug 3 02:29:55 EDT 2000


Erik Max Francis  <max at alcyone.com> said:
>It's not that it's impossible to come up with some general form of
>function composition, it's just that there are different ways to do it,
>and without any further clues one can't guess which one would be better.
>
Thanks for the frefresher. I was talking about the "inner" function
being able to have multiple arguments (f(g()), f(g(x)), f(g(x, y)), ..)
The other mails (thanks to y'all for the fast responses!) have probably
put me on the right track.

Haven't seen his mail here (yet), but Paul Foley corrected me:

> No it's not.  That doesn't work at all in Python!  Python doesn't have
> lexical closures, so f and g are not defined in the returned lambda
> function.
> 
> You have to write
> 
>   def compose(f, g): return lambda x, f=f, g=g: f(g(x))

He is right, of course. 
-- 
Cees de Groot               http://www.cdegroot.com     <cg at cdegroot.com>
GnuPG 1024D/E0989E8B 0016 F679 F38D 5946 4ECD  1986 F303 937F E098 9E8B
Forge your CipherSaber and list it: http://www.xs4all.nl/~cg/ciphersaber/



More information about the Python-list mailing list