I know that an empty list as a default parameter value of a function does not behave as expected. I was wondering if it were safe to capture a list in a lambda expression:
my_lists = [[1,2], [3, 4], [5, 6]]
def foo():
my_lambdas = []
for i in range(3):
my_lambdas.append(lambda my_list=my_lists[i]: my_list)
return my_lambdas
all_lambdas = foo()
This code seems to work, each lambda in all_lambdas
returning its own list.
My question is: Does capturing lists in lambdas raise similar issues as when we use lists as default parameter values? Are there any issues I should be aware of in the code above (even though it seems to work)? If there are any issues, how would I solve them?