3 ms·
> did I miss something? In your examples you're binding or rebinding to vars in the current scope. The issue is rebinding vars outside of the current scope. P
by cturner 16y ago
> did I miss something?
In your examples you're binding or rebinding to vars in the current scope. The issue is rebinding vars outside of the current scope.
Perhaps this code demonstates what I'm getting at.
def my_function():
a = 1
def modify_a():
a = 2
modify_a()
print a # prints 1
#
b = [1]
def modify_b():
b = 2
modify_b()
print b[0] # prints 1
#
c = [1]
def modify_c():
c[0] = 2
modify_c()
print c[0] # prints 2
my_function()
- masklinn 16y agoThis is clearer, but it has absolutely nothing to do with `for` loops (which is why your statement utterly confused me) and everything to do with Python's broken scoping in general: Python 2 gives no writing access to lexical scopes between global and local (Python 3 added a new keyword, `nonlocal`, which is used in the same manner as `global` to set a binding as writable in a parent scope).
- deleted 16y ago[deleted]