summaryrefslogtreecommitdiffstats
path: root/Doc/reference/executionmodel.rst
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-09-07 14:15:41 (GMT)
committerGeorg Brandl <georg@python.org>2007-09-07 14:15:41 (GMT)
commit96593ed348078403eb30fd5d2767fc96c17e913a (patch)
treef1221b6a26fb84fc1159ead1f4064fb7dc59a354 /Doc/reference/executionmodel.rst
parent44fa8f650f3a69318afe37874e82c6688f88e311 (diff)
downloadcpython-96593ed348078403eb30fd5d2767fc96c17e913a.zip
cpython-96593ed348078403eb30fd5d2767fc96c17e913a.tar.gz
cpython-96593ed348078403eb30fd5d2767fc96c17e913a.tar.bz2
Continue going through the language reference, bringing it up-to-date.
In particular, document the new comprehensions and remove mentions of long integers. Fix a bunch of related things in the lib ref.
Diffstat (limited to 'Doc/reference/executionmodel.rst')
-rw-r--r--Doc/reference/executionmodel.rst69
1 files changed, 34 insertions, 35 deletions
diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst
index 27802c8..1478cc7 100644
--- a/Doc/reference/executionmodel.rst
+++ b/Doc/reference/executionmodel.rst
@@ -26,17 +26,16 @@ Naming and binding
Each occurrence of a name in the program text refers to the :dfn:`binding` of
that name established in the innermost function block containing the use.
-.. index:: single: block
+.. index:: block
A :dfn:`block` is a piece of Python program text that is executed as a unit.
The following are blocks: a module, a function body, and a class definition.
Each command typed interactively is a block. A script file (a file given as
standard input to the interpreter or specified on the interpreter command line
the first argument) is a code block. A script command (a command specified on
-the interpreter command line with the '**-c**' option) is a code block. The string
-argument passed to the built-in functions :func:`eval` and :func:`exec` is a
-code block. The expression read and evaluated by the built-in function
-:func:`input` is a code block.
+the interpreter command line with the '**-c**' option) is a code block. The
+string argument passed to the built-in functions :func:`eval` and :func:`exec`
+is a code block.
.. index:: pair: execution; frame
@@ -44,7 +43,7 @@ A code block is executed in an :dfn:`execution frame`. A frame contains some
administrative information (used for debugging) and determines where and how
execution continues after the code block's execution has completed.
-.. index:: single: scope
+.. index:: scope
A :dfn:`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
@@ -61,10 +60,11 @@ scope. The set of all such scopes visible to a code block is called the block's
.. index:: pair: free; variable
-If a name is bound in a block, it is a local variable of that block. If a name
-is bound at the module level, it is a global variable. (The variables of the
-module code block are local and global.) If a variable is used in a code block
-but not defined there, it is a :dfn:`free variable`.
+If a name is bound in a block, it is a local variable of that block, unless
+declared as :keyword:`nonlocal`. If a name is bound at the module level, it is
+a global variable. (The variables of the module code block are local and
+global.) If a variable is used in a code block but not defined there, it is a
+:dfn:`free variable`.
.. index::
single: NameError (built-in exception)
@@ -96,18 +96,20 @@ function definition or at the module level (the top-level code block).
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
+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 name specified in
-the statement refer to the binding of that name 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 builtin
-namespace, the namespace of the module :mod:`__builtin__`. The global namespace
-is searched first. If the name is not found there, the builtin namespace is
-searched. The global statement must precede all uses of the name.
+If the :keyword:`global` statement occurs within a block, all uses of the name
+specified in the statement refer to the binding of that name 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 builtin namespace, the namespace of the module :mod:`__builtin__`. The
+global namespace is searched first. If the name is not found there, the builtin
+namespace is searched. The global statement must precede all uses of the name.
+
+.. XXX document "nonlocal" semantics here
.. index:: pair: restricted; execution
@@ -137,7 +139,7 @@ block. If the nearest enclosing scope for a free variable contains a global
statement, the free variable is treated as a global.
A class definition is an executable statement that may use and define names.
-These references follow the normal rules for name resolution. The namespace of
+These references follow the normal rules for name resolution. The namespace of
the class definition becomes the attribute dictionary of the class. Names
defined at the class scope are not visible in methods.
@@ -157,13 +159,14 @@ If the wild card form of import --- ``import *`` --- is used in a function and
the function contains or is a nested block with free variables, the compiler
will raise a :exc:`SyntaxError`.
-The :func:`eval` and :func:`exec` functions do
-not have access to the full environment for resolving names. Names may be
-resolved in the local and global namespaces of the caller. Free variables are
-not resolved in the nearest enclosing namespace, but in the global namespace.
-[#]_ The :func:`exec` and :func:`eval` functions have optional
-arguments to override the global and local namespace. If only one namespace is
-specified, it is used for both.
+.. XXX from * also invalid with relative imports (at least currently)
+
+The :func:`eval` and :func:`exec` functions do not have access to the full
+environment for resolving names. Names may be resolved in the local and global
+namespaces of the caller. Free variables are not resolved in the nearest
+enclosing namespace, but in the global namespace. [#]_ The :func:`exec` and
+:func:`eval` functions have optional arguments to override the global and local
+namespace. If only one namespace is specified, it is used for both.
.. _exceptions:
@@ -205,21 +208,17 @@ re-entering the offending piece of code from the top).
When an exception is not handled at all, the interpreter terminates execution of
the program, or returns to its interactive main loop. In either case, it prints
-a stack backtrace, except when the exception is :exc:`SystemExit`.
+a stack backtrace, except when the exception is :exc:`SystemExit`.
Exceptions are identified by class instances. The :keyword:`except` clause is
selected depending on the class of the instance: it must reference the class of
the instance or a base class thereof. The instance can be received by the
handler and can carry additional information about the exceptional condition.
-Exceptions can also be identified by strings, in which case the
-:keyword:`except` clause is selected by object identity. An arbitrary value can
-be raised along with the identifying string which can be passed to the handler.
-
.. warning::
- Messages to exceptions are not part of the Python API. Their contents may
- change from one version of Python to the next without warning and should not be
+ Exception messages are not part of the Python API. Their contents may change
+ from one version of Python to the next without warning and should not be
relied on by code which will run under multiple versions of the interpreter.
See also the description of the :keyword:`try` statement in section :ref:`try`
@@ -227,6 +226,6 @@ and :keyword:`raise` statement in section :ref:`raise`.
.. rubric:: Footnotes
-.. [#] This limitation occurs because the code that is executed by these operations is
- not available at the time the module is compiled.
+.. [#] This limitation occurs because the code that is executed by these operations
+ is not available at the time the module is compiled.