What is "lambda x=x : ... " ?
Scott David Daniels
Scott.Daniels at Acm.Org
Thu Jan 10 20:33:33 EST 2008
zslevi at gmail.com wrote:
> I'm reading this page: http://www.ps.uni-sb.de/~duchier/python/continuations.html
> and I've found a strange usage of lambda:
>
> ####################
> Now, CPS would transform the baz function above into:
>
> def baz(x,y,c):
> mul(2,x,lambda v,y=y,c=c: add(v,y,c))
>
> ###################
>
> What does "y=y" and "c=c" mean in the lambda function?
> I thought it bounds the outer variables, so I experimented a little
> bit:
>
> #################
> x = 3
> y = lambda x=x : x+10
>
> print y(2)
> ##################
>
> It prints 12, so it doesn't bind the variable in the outer scope.
Primary use:
funcs = [lambda x=x: x+2 for x in range(10)]
print funcs[3]()
_or_
funcs = []
for x in range(10):
funcs.append(lambda x=x: x+2)
print funcs[3]()
Try these w/o the default binding.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list