Within a loop, I'm building a collection of anonymous functions. The problem is that I want to define each function in terms of the current value of the variable I'm looping over, but when this variable changes, the function retrospectively changes because values are passed by reference.
Here's a simplified example:
c = 8
f = lambda x: x+c
f(1) # returns 9
c = 100
f(1) # returns 101
What's an idiomatic way to define a function in an immutable way so that further changes to other variables don't generally redefine the function's behavior? (I expect this question has come up before, but I wasn't able to locate a duplicate question on Stack Overflow.)