如何访问闭包内的外部函数变量(python 2.6)?

Thi*_*uda 4 python django python-2.6 python-3.x python-nonlocal

来自维基百科

我需要以与使用python 3.x中的'nonlocal'关键字类似的方式访问外部函数变量.有没有办法在python 2.6中做到这一点?(不一定使用nonlocal关键字)

And*_*Dog 5

在这种情况下我总是使用辅助对象:

def outerFunction():
    class Helper:
        val = None
    helper = Helper()

    def innerFunction():
        helper.val = "some value"
Run Code Online (Sandbox Code Playgroud)

当你启动一个应该将值写入外部函数作用域的新线程时,这也会派上用场.在这种情况下,helper将作为参数传递给innerFunction(线程的函数).