@@ -26,25 +26,24 @@ Naming and binding
2626Each occurrence of a name in the program text refers to the :dfn: `binding ` of
2727that name established in the innermost function block containing the use.
2828
29- .. index :: single: block
29+ .. index :: block
3030
3131A :dfn: `block ` is a piece of Python program text that is executed as a unit.
3232The following are blocks: a module, a function body, and a class definition.
3333Each command typed interactively is a block. A script file (a file given as
3434standard input to the interpreter or specified on the interpreter command line
3535the first argument) is a code block. A script command (a command specified on
36- the interpreter command line with the '**-c **' option) is a code block. The string
37- argument passed to the built-in functions :func: `eval ` and :func: `exec ` is a
38- code block. The expression read and evaluated by the built-in function
39- :func: `input ` is a code block.
36+ the interpreter command line with the '**-c **' option) is a code block. The
37+ string argument passed to the built-in functions :func: `eval ` and :func: `exec `
38+ is a code block.
4039
4140.. index :: pair: execution; frame
4241
4342A code block is executed in an :dfn: `execution frame `. A frame contains some
4443administrative information (used for debugging) and determines where and how
4544execution continues after the code block's execution has completed.
4645
47- .. index :: single: scope
46+ .. index :: scope
4847
4948A :dfn: `scope ` defines the visibility of a name within a block. If a local
5049variable is defined in a block, its scope includes that block. If the
@@ -61,10 +60,11 @@ scope. The set of all such scopes visible to a code block is called the block's
6160
6261.. index :: pair: free; variable
6362
64- If a name is bound in a block, it is a local variable of that block. If a name
65- is bound at the module level, it is a global variable. (The variables of the
66- module code block are local and global.) If a variable is used in a code block
67- but not defined there, it is a :dfn: `free variable `.
63+ If a name is bound in a block, it is a local variable of that block, unless
64+ declared as :keyword: `nonlocal `. If a name is bound at the module level, it is
65+ a global variable. (The variables of the module code block are local and
66+ global.) If a variable is used in a code block but not defined there, it is a
67+ :dfn: `free variable `.
6868
6969.. index ::
7070 single: NameError (built-in exception)
@@ -96,18 +96,20 @@ function definition or at the module level (the top-level code block).
9696
9797If a name binding operation occurs anywhere within a code block, all uses of the
9898name within the block are treated as references to the current block. This can
99- lead to errors when a name is used within a block before it is bound. This rule
99+ lead to errors when a name is used within a block before it is bound. This rule
100100is subtle. Python lacks declarations and allows name binding operations to
101101occur anywhere within a code block. The local variables of a code block can be
102102determined by scanning the entire text of the block for name binding operations.
103103
104- If the global statement occurs within a block, all uses of the name specified in
105- the statement refer to the binding of that name in the top-level namespace.
106- Names are resolved in the top-level namespace by searching the global namespace,
107- i.e. the namespace of the module containing the code block, and the builtin
108- namespace, the namespace of the module :mod: `__builtin__ `. The global namespace
109- is searched first. If the name is not found there, the builtin namespace is
110- searched. The global statement must precede all uses of the name.
104+ If the :keyword: `global ` statement occurs within a block, all uses of the name
105+ specified in the statement refer to the binding of that name in the top-level
106+ namespace. Names are resolved in the top-level namespace by searching the
107+ global namespace, i.e. the namespace of the module containing the code block,
108+ and the builtin namespace, the namespace of the module :mod: `__builtin__ `. The
109+ global namespace is searched first. If the name is not found there, the builtin
110+ namespace is searched. The global statement must precede all uses of the name.
111+
112+ .. XXX document "nonlocal" semantics here
111113
112114 .. index :: pair: restricted; execution
113115
@@ -137,7 +139,7 @@ block. If the nearest enclosing scope for a free variable contains a global
137139statement, the free variable is treated as a global.
138140
139141A class definition is an executable statement that may use and define names.
140- These references follow the normal rules for name resolution. The namespace of
142+ These references follow the normal rules for name resolution. The namespace of
141143the class definition becomes the attribute dictionary of the class. Names
142144defined at the class scope are not visible in methods.
143145
@@ -157,13 +159,14 @@ If the wild card form of import --- ``import *`` --- is used in a function and
157159the function contains or is a nested block with free variables, the compiler
158160will raise a :exc: `SyntaxError `.
159161
160- The :func: `eval ` and :func: `exec ` functions do
161- not have access to the full environment for resolving names. Names may be
162- resolved in the local and global namespaces of the caller. Free variables are
163- not resolved in the nearest enclosing namespace, but in the global namespace.
164- [# ]_ The :func: `exec ` and :func: `eval ` functions have optional
165- arguments to override the global and local namespace. If only one namespace is
166- specified, it is used for both.
162+ .. XXX from * also invalid with relative imports (at least currently)
163+
164+ The :func: `eval ` and :func: `exec ` functions do not have access to the full
165+ environment for resolving names. Names may be resolved in the local and global
166+ namespaces of the caller. Free variables are not resolved in the nearest
167+ enclosing namespace, but in the global namespace. [# ]_ The :func: `exec ` and
168+ :func: `eval ` functions have optional arguments to override the global and local
169+ namespace. If only one namespace is specified, it is used for both.
167170
168171
169172.. _exceptions :
@@ -205,28 +208,24 @@ re-entering the offending piece of code from the top).
205208
206209When an exception is not handled at all, the interpreter terminates execution of
207210the program, or returns to its interactive main loop. In either case, it prints
208- a stack backtrace, except when the exception is :exc: `SystemExit `.
211+ a stack backtrace, except when the exception is :exc: `SystemExit `.
209212
210213Exceptions are identified by class instances. The :keyword: `except ` clause is
211214selected depending on the class of the instance: it must reference the class of
212215the instance or a base class thereof. The instance can be received by the
213216handler and can carry additional information about the exceptional condition.
214217
215- Exceptions can also be identified by strings, in which case the
216- :keyword: `except ` clause is selected by object identity. An arbitrary value can
217- be raised along with the identifying string which can be passed to the handler.
218-
219218.. warning ::
220219
221- Messages to exceptions are not part of the Python API. Their contents may
222- change from one version of Python to the next without warning and should not be
220+ Exception messages are not part of the Python API. Their contents may change
221+ from one version of Python to the next without warning and should not be
223222 relied on by code which will run under multiple versions of the interpreter.
224223
225224See also the description of the :keyword: `try ` statement in section :ref: `try `
226225and :keyword: `raise ` statement in section :ref: `raise `.
227226
228227.. rubric :: Footnotes
229228
230- .. [# ] This limitation occurs because the code that is executed by these operations is
231- not available at the time the module is compiled.
229+ .. [# ] This limitation occurs because the code that is executed by these operations
230+ is not available at the time the module is compiled.
232231
0 commit comments