In Python 3.3.1, this works:
i = 76
def A():
global i
i += 10
print(i) # 76
A()
print(i) # 86
This also works:
def enclosing_function():
i = 76
def A():
nonlocal i
i += 10
print(i) # 76
A()
print(i) # 86
enclosing_function()
But this doesn't work:
i = 76
def A():
nonlocal i # "SyntaxError: no binding for nonlocal 'i' found"
i += 10
print(i)
A()
print(i)
The documentation for the nonlocal
keyword states (emphasis added):
The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope.
In the third example, the "nearest enclosing scope" just happens to be the global scope. So why doesn't it work?
I do notice that the documentation goes on to state (emphasis added):
The [
nonlocal
] statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.
but, strictly speaking, this doesn't mean that what I'm doing in the third example shouldn't work.
An important difference between nonlocal and global is that the a nonlocal variable must have been already bound in the enclosing namespace (otherwise an syntaxError will be raised) while a global declaration in a local scope does not require the variable is pre-bound (it will create a new binding in the global ...
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.
In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.
In python, nonlocal variables refer to all those variables that are declared within nested functions. The local scope of a nonlocal variable is not defined. This essentially means that the variable exists neither in the local scope nor in the global scope.
In this tutorial we will learn about global and nonlocal keyword in Python . Before going through this tutorial we need to know about namespace and scope in Python . Global keyword in Python helps us to change value of a global variable inside a function .
Python nonlocal keyword is used to reference a variable in the nearest scope. In this example, we demonstrate the working of the nonlocal keyword. The nonlocal keyword won’t work on local or global variables and therefore must be used to reference variables in another scope except the global and local one.
The nonlocal keyword won’t work on local or global variables and therefore must be used to reference variables in another scope except the global and local one. The nonlocal keyword is used in nested functions to reference a variable in the parent function. It helps in accessing the variable in the upper scope.
Global keyword in Python helps us to change value of a global variable inside a function . First we will write a program to change the value of a global variable inside a function without global keyword and check whether the program actually change the value of the global variable or not .
The search order for names is LEGB, i.e Local, Enclosing, Global, Builtin. So the global scope is not an enclosing scope.
EDIT
From the docs:
The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.
why is a module's scope considered global and not an enclosing one? It's still not global to other modules (well, unless you do
from module import *
), is it?
If you put some name into module
's namespace; it is visible in any module that uses module
i.e., it is global for the whole Python process.
In general, your application should use as few mutable globals as possible. See Why globals are bad?:
Therefore It would be bad if nonlocal
allowed to create globals by accident. If you want to modify a global variable; you could use global
keyword directly.
global
is the most destructive: may affect all uses of the module anywhere in the programnonlocal
is less destructive: limited by the outer() function scope (the binding is checked at compile time)You can read about history and motivation behind nonlocal
in PEP: 3104
Access to Names in Outer Scopes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With