I have a problem with lambdas in a loop. It is hard to explain the problem so I will show it in a short example:
class TestObj():
def __init__(self, value):
self.value = value
lambdas = []
values_list = [10, 1]
for ele in values_list:
foo = TestObj(ele)
lambdas.append(lambda: foo.value > 5)
print([single_lambda() for single_lambda in lambdas])
as a result of print I get:
[False, False]
Despite I would expect [True, False]. Could you tell me why it does not work? and how Could I get the result that I expected? I only add that lambdas in a loop are required in my code to define formulas(those formulas are a lot and can be much more complex). Those formulas are defined in one place in the application and executed after many lines below the definition. I would be grateful for your help.