- 4.2. Naming and binding
- =======================
- 4.2.2. Resolution of names
- --------------------------
- A *scope* defines the visibility of a name within a block. If a local
- variable is defined in a block, its scope includes that block. If the
- definition occurs in a function block, the scope extends to any blocks
- contained within the defining one, unless a contained block introduces
- a different binding for the name.
- When a name is used in a code block, it is resolved using the nearest
- enclosing scope. The set of all such scopes visible to a code block
- is called the block's *environment*.
- When a name is not found at all, a "NameError" exception is raised. If
- the current scope is a function scope, and the name refers to a local
- variable that has not yet been bound to a value at the point where the
- name is used, an "UnboundLocalError" exception is raised.
- "UnboundLocalError" is a subclass of "NameError".
- If a name binding operation occurs anywhere within a code block, all
- uses of the name within the block are treated as references to the
- current block. This can lead to errors when a name is used within a
- block before it is bound. This rule is subtle. Python lacks
- declarations and allows name binding operations to occur anywhere
- within a code block. The local variables of a code block can be
- determined by scanning the entire text of the block for name binding
- operations.
- If the "global" statement occurs within a block, all uses of the names
- specified in the statement refer to the bindings of those names in the
- top-level namespace. Names are resolved in the top-level namespace by
- searching the global namespace, i.e. the namespace of the module
- containing the code block, and the builtins namespace, the namespace
- of the module "builtins". The global namespace is searched first. If
- the names are not found there, the builtins namespace is searched.
- The "global" statement must precede all uses of the listed names.
- The "global" statement has the same scope as a name binding operation
- in the same block. If the nearest enclosing scope for a free variable
- contains a global statement, the free variable is treated as a global.
- The "nonlocal" statement causes corresponding names to refer to
- previously bound variables in the nearest enclosing function scope.
- "SyntaxError" is raised at compile time if the given name does not
- exist in any enclosing function scope.
- The namespace for a module is automatically created the first time a
- module is imported. The main module for a script is always called
- "__main__".
- Class definition blocks and arguments to "exec()" and "eval()" are
- special in the context of name resolution. A class definition is an
- executable statement that may use and define names. These references
- follow the normal rules for name resolution with an exception that
- unbound local variables are looked up in the global namespace. The
- namespace of the class definition becomes the attribute dictionary of
- the class. The scope of names defined in a class block is limited to
- the class block; it does not extend to the code blocks of methods --
- this includes comprehensions and generator expressions since they are
- implemented using a function scope. This means that the following
- will fail:
- class A:
- a = 42
- b = list(a + i for i in range(10))
- Related video
- Python Course/Intermediate > Python Scope, Namespaces, Name Resolution and First-Class Objects
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license