Nested Areas and Lambdas

def funct(): x = 4 action = (lambda n: x ** n) return action x = funct() print(x(2)) # prints 16 

... I do not quite understand why 2 is automatically assigned n?

+3
python lambda nested
Jan 05 '10 at 5:48
source share
2 answers

n is an argument to the anonymous function returned by funct . The exactly equivalent permutation funct equals

 def funct(): x = 4 def action(n): return x ** n return action 

Does this form make sense?

+5
Jan 05 '10 at 5:55
source share

It is not assigned "automatically": it is assigned very explicitly and non- automatically, passing it as the actual argument corresponding to parameter n . This complicated way to set x almost identical (net x.__name__ and other minor introspective data) before def x(n): return 4**n .

+3
Jan 05 '10 at 5:58
source share



All Articles