非ローカルの使用中のpythonについて

# 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()

公開された12元の記事 ウォン称賛7 ビュー160

おすすめ

転載: blog.csdn.net/caiyongxin_001/article/details/105032398