Why doesn't Python's nonlocal keyword like the global scope?

前端 未结 4 1303
一生所求
一生所求 2021-02-01 21:08

In Python 3.3.1, this works:

i = 76

def A():
    global i
    i += 10

print(i) # 76
A()
print(i) # 86

This also works:

def en         


        
4条回答
  •  不知归路
    2021-02-01 21:31

    The answer is that the global scope does not enclose anything - it is global to everything. Use the global keyword in such a case.

提交回复
热议问题