python2 nonlocal

python2 没有nonlocal ,当需要从python3 转python2时遇到nonlocal时可以采用下文的形式解决

  https://technotroph.wordpress.com/2012/10/01/python-closures-and-the-python-2-7-nonlocal-solution/

def outer():
    d={"y":10}
    i=10
    def inner():
        nonlocal i
        d['y']+=1
        i+=1
        print(d['y'],i)

    inner()
    inner()

if '__main__'==__name__:
    outer()

d就可以实现python3的nonlocal 作用

posted on 2018-01-24 18:27  reedlau  阅读(741)  评论(0编辑  收藏  举报

导航