The only guaranteed way to ensure that a free variable in python function scope (it's not a closure) does not change is to pass it explicitly as a default value.
a = 1
def function(x, a=a):
return x + a
That will work in loops. Otherwise what you are really writing is a = 1
def function(x):
return x + the_last_value_a_obtains_in_this_scope
a = 2
assert function(1) == 3