In python I can use the nonlocal
keyword to make sure that I use the variable from the outer scope, but how do I copy the variable from the outer scope into the closure?
I have code like this:
my_functions_with_parameters = []
for fun_name in my_function_names:
fun = my_functions[fun_name]
def fun_with_parameters():
return fun(1,2,3)
my_functions_with_parameters.append(fun_with_parameter)
My problem is, that each instance of fun_with_parameters
calls the last function, i.e., my_functions[my_function_names[-1]]
, because the variable fun is a reference.
I tried to use fun = copy.deepcopy(my_functions[fun_name])
but it did not change anything and is probably not the best or fastest way.