def funct():
x = 4
action = (lambda n: x ** n)
return action
x = funct()
print(x(2)) # prints 16
... I don't quite understand why 2 is assigned to n automatically?
def funct():
x = 4
action = (lambda n: x ** n)
return action
x = funct()
print(x(2)) # prints 16
... I don't quite understand why 2 is assigned to n automatically?
n
is the argument of the anonymous function returned by funct
. An exactly equivalent defintion of funct
is
def funct():
x = 4
def action(n):
return x ** n
return action
Does this form make any more sense?
It's not assigned "automatically": it's assigned very explicitly and non-automatically by your passing it as the actual argument corresponding to the n
parameter. That complicated way to set x
is almost identical (net of x.__name__
and other minor introspective details) to def x(n): return 4**n
.