Can you assign to a variable defined in a parent function? [duplicate]

佐手、 提交于 2019-12-07 19:30:16

问题


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.



来源:https://stackoverflow.com/questions/8178633/can-you-assign-to-a-variable-defined-in-a-parent-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!