Suppose someone wants to make a lambda function, and they want everything in the lambda function, except for the input arguments, to be treated like a function's local variables. How would they accomplish that? By saying that the variables are "local," I mean that there is no access to those variables from outside of the lambda function.
Code
right = 5
demo_lamb = lambda left : print("INSIDE LAMB left == ", left, ",", "right == ", right)
for lt in range(0, 5):
demo_lamb(lt)
right = 100*lt + 99
print("OUTSIDE LAMB right == ", right)
Printed Output
INSIDE LAMB left == 0 , right == 5
OUTSIDE LAMB right == 99
INSIDE LAMB left == 1 , right == 99
OUTSIDE LAMB right == 199
INSIDE LAMB left == 2 , right == 199
OUTSIDE LAMB right == 299
INSIDE LAMB left == 3 , right == 299
OUTSIDE LAMB right == 399
INSIDE LAMB left == 4 , right == 399
OUTSIDE LAMB right == 499
Desired Output
INSIDE LAMB left == 0 , right == 5
OUTSIDE LAMB right == 99
INSIDE LAMB left == 1 , right == 5
OUTSIDE LAMB right == 199
INSIDE LAMB left == 2 , right == 5
OUTSIDE LAMB right == 299
INSIDE LAMB left == 3 , right == 5
OUTSIDE LAMB right == 399
INSIDE LAMB left == 4 , right == 5
OUTSIDE LAMB right == 499
How do we "freeze" the value of all of the variables inside of a lambda function (except the input arguments) to be what they were when the lambda function was created?
One Failed Solution
Importing copy
and replacing right
inside the lambda function with copy.deepcopy(right)
makes no difference to the output whatsoever.