Python lambda's binding to local values

后端 未结 2 467
你的背包
你的背包 2020-11-22 16:48

The following code spits out 1 twice, but I expect to see 0 and then 1.

def pv(v) :
  print v

x = []
for v in range(2)         


        
2条回答
  •  抹茶落季
    2020-11-22 17:03

    The lambda's closure holds a reference to the variable being used, not its value, so if the value of the variable later changes, the value in the closure also changes. That is, the closure variable's value is resolved when the function is called, not when it is created. (Python's behavior here is not unusual in the functional programming world, for what it's worth.)

    There are two solutions:

    1. Use a default argument, binding the current value of the variable to a local name at the time of definition. lambda v=v: pv(v)

    2. Use a double-lambda and immediately call the first one. (lambda v: lambda: pv(v))(v)

提交回复
热议问题