I know I am late, but I found a messy workaround which gets the job done (tested in Python 3.7)
If you use a double lambda (like I said, very messy) you can preserve the value, like so:
Step 1: Create the nested lambda statement:
send_param = lambda val: lambda: print(val)
Step 2: Use the lambda statement:
send_param(i)
The send_param
method returns the inner most lambda (lambda: print(val)
) without executing the statement, until you call the result of send_param
which takes no arguments, for example:
a = send_param(i)
a()
Only the second line will execute the print
statement.