In response to Jeffrey's answer, I would like to add that in Python 3 you can access the variable more widely from the nearest enclosing area:
def set_local_var():
var = None
def set_var(v):
nonlocal var
var = v
return (var, set_var)
(my_var, my_set) = set_local_var()
print my_var
my_set(3)
print my_var
(Caution: I have not tested this since I do not have Python 3.)
source
share