Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Scope (computer science)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Python === For variables, Python has function scope, module scope, and global scope. Names enter context at the start of a scope (function, module, or global scope), and exit context when a non-nested function is called or the scope ends. If a name is used prior to variable initialization, this raises a runtime exception. If a variable is simply accessed (not assigned to), name resolution follows the LEGB (Local, Enclosing, Global, Built-in) rule which resolves names to the narrowest relevant context. However, if a variable is assigned to, it defaults to declaring a variable whose scope starts at the start of the level (function, module, or global), not at the assignment. Both these rules can be overridden with a <code>global</code> or <code>nonlocal</code> (in Python 3) declaration prior to use, which allows accessing global variables even if there is a masking nonlocal variable, and assigning to global or nonlocal variables. As a simple example, a function resolves a variable to the global scope: <syntaxhighlight lang="pycon"> >>> def f(): ... print(x) ... >>> x = "global" >>> f() global </syntaxhighlight> Note that <code>x</code> is defined before <code>f</code> is called, so no error is raised, even though it is defined after its reference in the definition of <code>f</code>. Lexically this is a [[forward reference]], which is allowed in Python. Here assignment creates a new local variable, which does not change the value of the global variable: <syntaxhighlight lang="pycon"> >>> def f(): ... x = "f" ... print(x) ... >>> x = "global" >>> print(x) global >>> f() f >>> print(x) global </syntaxhighlight> Assignment to a variable within a function causes it to be declared local to the function, hence its scope is the entire function, and thus using it prior to this assignment raises an error. This differs from C, where the scope of the local variable start at its declaration. This code raises an error: <syntaxhighlight lang="pycon"> >>> def f(): ... print(x) ... x = "f" ... >>> x = "global" >>> f() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f UnboundLocalError: local variable 'x' referenced before assignment </syntaxhighlight> The default name resolution rules can be overridden with the <code>global</code> or <code>nonlocal</code> (in Python 3) keywords. In the below code, the <code>global x</code> declaration in <code>g</code> means that <code>x</code> resolves to the global variable. It thus can be accessed (as it has already been defined), and assignment assigns to the global variable, rather than declaring a new local variable. Note that no <code>global</code> declaration is needed in <code>f</code>βsince it does not assign to the variable, it defaults to resolving to the global variable. <syntaxhighlight lang="pycon"> >>> def f(): ... print(x) ... >>> def g(): ... global x ... print(x) ... x = "g" ... >>> x = "global" >>> f() global >>> g() global >>> f() g </syntaxhighlight> <code>global</code> can also be used for nested functions. In addition to allowing assignment to a global variable, as in an unnested function, this can also be used to access the global variable in the presence of a nonlocal variable: <syntaxhighlight lang="pycon"> >>> def f(): ... def g(): ... global x ... print(x) ... x = "f" ... g() ... >>> x = "global" >>> f() global </syntaxhighlight> For nested functions, there is also the <code>nonlocal</code> declaration, for assigning to a nonlocal variable, similar to using <code>global</code> in an unnested function: <syntaxhighlight lang="pycon"> >>> def f(): ... def g(): ... nonlocal x # Python 3 only ... x = "g" ... x = "f" ... g() ... print(x) ... >>> x = "global" >>> f() g >>> print(x) global </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Scope (computer science)
(section)
Add topic