Python inner functions

后端 未结 3 2015
忘了有多久
忘了有多久 2020-12-17 19:51

In python, I can write :

def func():
    x = 1
    print x
    x+=1

    def _func():
        print x
    return _func

test = func()
test()
3条回答
  •  天涯浪人
    2020-12-17 20:27

    Check this answer : https://stackoverflow.com/a/293097/1741450

    Variables in scopes other than the local function's variables can be accessed, but can't be rebound to new parameters without further syntax. Instead, assignment will create a new local variable instead of affecting the variable in the parent scope. For example:

提交回复
热议问题