Python 中 global 和 nonlocal 关键字有什么区别?
global
nonlocal
示例:
# global x = 10 def func(): global x x = 20 func() print(x) # 输出 20 # nonlocal def outer(): y = 30 def inner(): nonlocal y y = 40 inner() print(y) # 输出 40 outer()