nonlocal解释
nonlocal global
对nonlocal 关键字感到有些困惑,上手实践一下
Definition:
The nonlocal
keyword is used to work with variables inside nested functions, where the variable should not belong to the inner function.
Use the keyword nonlocal
to declare that the variable is not local.
1 | count = "Global" |
1 | Enclosing |
nonlocal 可以让a()中count变量变为非局部,可以让b()使用
global作为对比
1 | if __name__ == '__main__': |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Way2BeBetter!
评论