I have a dictionary of properties and their limiting values. For properties containing 'max', the limiting values is the maximum allowed. Similarly for minima.
I'm trying to create a dictionary of functions. Each function will check an input value against the limiting value for a property of the list.
limits = {'variable1 max': 100,
'variable1 min': 10}
check_acceptable = {key: (lambda x: x < lim) if 'max' in key else
(lambda x: x > lim)
for key, lim in limits.items()}
# Now, I'd expect this to be True, since 90 < 100:
check_acceptable['variable1 max'](90)
Out: False
What happended here is that the lambda function got assigned the variable lim
, and in the for loop, lim
last value was 10, then my first lambda function is wrong.
How do I fix that?
--- edit - adding a more generic example of the same behaviour:
funcs = []
for i in range(5):
funcs.append(lambda _: i)
for j in range(5):
print(funcs[j](j), end='')
# Expected: 01234
Out: 44444