Possible Duplicate:
Python nested functions variable scoping
After much trial and error I have eventually discovered that this doesn't work:
def a():
def b():
print x
x=2
x = 1
b()
print x
You get an exception (x not defined before being referenced). So it looks like b can read from x, but if it tries to assign to it, Python changes its interpretation of 'x' to be a local variable, which is now not defined.
Question for my own sick curiosity: is there any way of achieving this? Is there a way of explicitly accessing the scope of the parent function? (x is not global)
The
nonlocal
statement in Python 3 will do this.Edit: In Python 2, there's not a simple way to do it. I suggest you use some mutable container object if you need this capability. For example:
If you absolutely must emulate
nonlocal
for CPython 2, you can hack it with the Python C API this way:You could also set the frame local, and instead of calling
PyFrame_LocalsToFast()
, you could manipulate the bytecode ofa
so that it usesLOAD_NAME
instead ofLOAD_FAST
. Please don't do either of these things. There is surely a better solution for your use case.