Can you assign to a variable defined in a parent f

2019-05-20 22:07发布

问题:

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)

回答1:

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:

def a():
    def b():
        print d["x"]
        d["x"]=2
    d = dict(x=1)
    b()
    print d["x"]

If you absolutely must emulate nonlocal for CPython 2, you can hack it with the Python C API this way:

import ctypes
import inspect

locals_to_fast = ctypes.pythonapi.PyFrame_LocalsToFast
locals_to_fast.restype = None
locals_to_fast.argtypes = [ctypes.py_object, ctypes.c_int]

def set_in_frame(frame, name, value):
    frame.f_locals[name] = value
    locals_to_fast(frame, 1)

def a():
    def b(frame=inspect.currentframe()):
        print x
        set_in_frame(frame, "x", 2)
    x = 1
    b()
    print x

You could also set the frame local, and instead of calling PyFrame_LocalsToFast(), you could manipulate the bytecode of a so that it uses LOAD_NAME instead of LOAD_FAST. Please don't do either of these things. There is surely a better solution for your use case.