I'd like to construct filters depending on certain parameters, by chaining together python lambdas like this:
filter_even = True
my_filter = lambda x: x # Base filter
if filter_even:
my_filter = lambda x: my_filter(x) and x % 2 == 0
list(filter(my_filter, [1,2,3,4]))
But my_filter
is not bound in the lambda, but a reference to the new lambda, so I get
RecursionError: maximum recursion depth exceeded
What is the best way to bind the old lambda inside the new one? I thought lambdas behave like variables, but in this case it seems to reference the old lambda by name instead of the content of the variable (which is replaced with the new lambda afterward)