Why lambda function to get the list of values i = 4 .During the call lambda, enclosing scope does not exist. The function f has finished work and returned control (the variable i does not exist).
def f():
L = []
for i in range(5):
L.append(lambda x: i ** x)
return L
L = f()
L[0]
def f1(N):
def f2(X):
return X**N
return f2
f=f1(2)
f (3)
9
g = f1(3)
g(3)
27
f(3)
9
Python uses closures to capture references to the original variable. The
lambda
objects retain a reference to thei
name, through which the value can be accessed. This means that thei
variable continues to live on afterf
completes.You can introspect this closure in the
.__closure__
tuple on the lambda objects; functions have the same attribute:This is also why all lambdas in your list
L
refer to the value4
, and not to the numbers 0 through to 4. They all refer to the same closure:The closure refers to the variable, not to the value of that variable at the time the closure was defined. At the end of the loop
i
was last set to4
, so when looking upi
in the lambda closure4
will be found, for all lambdas in your list.If you want your lambdas to refer to the value of
i
during the loop, capture it in a keyword argument:Now
i
is a local variable to the lambda, not a closure.Alternatively, create an entirely new scope from which to draw the closure:
Now
create_lambda()
is a new scope with it's own locali
for the lambda closure to refer to. The lambdas then each have their own closures:Closures refer to a variable in a specific namespace; each time you call a function a new local namespace is created, so each closure refers to
i
increate_lambda
in a separate namespace from other calls tocreate_lambda
.