# nonlocal
# 1.不能操作全局变量。(以下是错误用法)
count = 1
def func():
nonlocal count
count += 1
func()
# 2.局部作用域:内层函数对外层函数的局部变量进行修改
def wrapper():
count = 1
def inner():
nonlocal count
count += 1
print(count) # --> 1
inner()
print(count) # --> 2
wrapper()
关于python中的nonlocal用法
最新推荐文章于 2024-03-10 07:00:00 发布