Python nonlocal statement

前端 未结 9 1064
我在风中等你
我在风中等你 2020-11-22 03:52

What does the Python nonlocal statement do (in Python 3.0 and later)?

There\'s no documentation on the official Python website and help(\"nonloca

9条回答
  •  悲&欢浪女
    2020-11-22 04:23

    with 'nonlocal' inner functions(ie;nested inner functions) can get read & 'write' permission for that specific variable of the outer parent function. And nonlocal can be used only inside inner functions eg:

    a = 10
    def Outer(msg):
        a = 20
        b = 30
        def Inner():
            c = 50
            d = 60
            print("MU LCL =",locals())
            nonlocal a
            a = 100
            ans = a+c
            print("Hello from Inner",ans)       
            print("value of a Inner : ",a)
        Inner()
        print("value of a Outer : ",a)
    
    res = Outer("Hello World")
    print(res)
    print("value of a Global : ",a)
    

提交回复
热议问题