Python - Function Lambda scope


A lambda expression introduces a new local scope for the function it creates.

Lambdas can see all the variables that live in the functions where they are coded.


def func(): 
    x = 4 # ww  w . ja v  a2 s .co m
    action = (lambda n: x ** n)          # x remembered from enclosing def 
    return action 

x = func() 
print(x(2))                              # Prints 16, 4 ** 2


Related Topic