开发者

Creating lambda inside a loop [duplicate]

This question already has answers here: 开发者_StackOverflow中文版 What do lambda function closures capture? (7 answers) Creating functions (or lambdas) in a loop (or comprehension) (6 answers) Closed 8 months ago.

I am trying to create lambdas inside a loop that iterates over a list of objects:

lambdas_list = []
for obj in obj_list:
   lambdas_list.append(lambda : obj.some_var)

Now, if I iterate over the lambdas list and call them like this:

for f in lambdas_list:
    print(f())

I get the same value. That is the value of the last obj in obj_list since that was the last variable in the block of the list iterator. Any ideas for a good (pythonic) rewrite of the code to make it work?


Use this line instead:

lambdas_list.append(lambda obj=obj: obj.some_var)


You either have to capture the variable using default assignments

lambdas_list = [ lambda i=o: i.some_var for o in obj_list ]

or, use closures to capture the variable

lambdas_list = [ (lambda a: lambda: a.some_var)(o) for o in obj_list ]

In both cases the key is to make sure each value in the obj_list list is assigned to a unique scope.

Your solution didn't work because the lexical variable obj is referenced from the parent scope (the for).

The default assignment worked because we reference i from the lambda scope. We use the default assignment to make "passing" the variable implicit by making it part of the lambda definition and therefore its scope.

The closure solution works because the variable "passing" is explicit, into the outer lambda which is immediately executed, thus creating the binding during the list comprehension and and returning the inner lambda which references that binding. So when the inner lambda actually executes it will be referencing the binding created in the outer lambda scope.


You can do something like that:

def build_lambda(obj):
  return lambda : obj.some_var

lambdas_list = []
for obj in obj_list:
   lambdas_list.append(build_lambda(obj))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜