if no global statement is in effect – assignments to names always go into the innermost scope.
同时:
The global statement can be used to indicate that particular variables live in the global scope and should be rebound there; the nonlocalstatement indicates that particular variables live in an enclosing scope and should be rebound there.
见9.2. Python Scopes and Namespaces:
同时:
您有许多解决方案:
i
作为参数✓传递(我将使用这个参数)nonlocal
关键字请注意,在Python2.x中,您可以访问非局部变量,但您不能更改它们。在
您可以使用
i
作为如下参数:使用
nonlocal
语句有关python3.x中添加的这个新语句的更多信息,请转到https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement
相关问题 更多 >
编程相关推荐