[Tutor] Thoughts on little lambda
Jeff Shannon
jeff@ccvcorp.com
Mon, 11 Mar 2002 10:35:32 -0800
Recently, Kirby Urner described a bit about the differences between Python's lambda and Lisp-ish
lambdas. To create something that works a bit more like the Lisp-ish lambda, Kirby proposed using a
function factory like this:
> def funcfact(r,j):
> def main(x):
> if not x%2:
> return pow(x,j)+4
> else:
> return pow(x,r)+3
> return lambda x: main(x)
Now, I might be missing something (wouldn't be the first time ;) ) but it seems to me that the return
statement doesn't need to create a lambda; it should be equivalent to change it to simply
return main
shouldn't it? ISTM that the lambda here creates a function object that calls a function, using
exactly the arguments passed to it; I see no benefit to encapsulating that second function object
(the main in the above example) inside of another function object (and some cost in terms of
overhead).
Jeff Shannon
Technician/Programmer
Credit International