summaryrefslogtreecommitdiffstats
path: root/Doc/ref4.tex
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/ref4.tex')
-rw-r--r--Doc/ref4.tex55
1 files changed, 34 insertions, 21 deletions
diff --git a/Doc/ref4.tex b/Doc/ref4.tex
index 62db120..c14fada 100644
--- a/Doc/ref4.tex
+++ b/Doc/ref4.tex
@@ -20,9 +20,9 @@ The following are code blocks: A module is a code block. A function
body is a code block. A class definition is a code block. Each
command typed interactively is a separate code block; a script file is
a code block. The string argument passed to the built-in function
-\verb\eval\ and to the \verb\exec\ statement are code blocks.
+\verb@eval@ and to the \verb@exec@ statement are code blocks.
And finally, the
-expression read and evaluated by the built-in function \verb\input\ is
+expression read and evaluated by the built-in function \verb@input@ is
a code block.
A code block is executed in an execution frame. An {\em execution
@@ -46,7 +46,7 @@ Name spaces are functionally equivalent to dictionaries.
The {\em local name space} of an execution frame determines the default
place where names are defined and searched. The {\em global name
-space} determines the place where names listed in \verb\global\
+space} determines the place where names listed in \verb@global@
statements are defined and searched, and where names that are not
explicitly bound in the current code block are searched.
\indexii{local}{name space}
@@ -55,25 +55,35 @@ explicitly bound in the current code block are searched.
Whether a name is local or global in a code block is determined by
static inspection of the source text for the code block: in the
-absence of \verb\global\ statements, a name that is bound anywhere in
+absence of \verb@global@ statements, a name that is bound anywhere in
the code block is local in the entire code block; all other names are
-considered global. The \verb\global\ statement forces global
+considered global. The \verb@global@ statement forces global
interpretation of selected names throughout the code block. The
-following constructs bind names: formal parameters, \verb\import\
+following constructs bind names: formal parameters, \verb@import@
statements, class and function definitions (these bind the class or
function name), and targets that are identifiers if occurring in an
-assignment, \verb\for\ loop header, or \verb\except\ clause header.
-(A target occurring in a \verb\del\ statement does not bind a name.)
+assignment, \verb@for@ loop header, or \verb@except@ clause header.
+
+A target occurring in a \verb@del@ statement is also considered bound
+for this purpose (though the actual semantics are to ``unbind'' the
+name).
When a global name is not found in the global name space, it is
searched in the list of ``built-in'' names (which is actually the
-global name space of the module \verb\__builtin__\). When a name is not
-found at all, the \verb\NameError\ exception is raised.
+global name space of the module \verb@__builtin__@). When a name is not
+found at all, the \verb@NameError@ exception is raised.%
+\footnote{If the code block contains \verb@exec@ statement or the
+construct \verb@from ... import *@, the semantics of names not
+explicitly mentioned in a \verb@global@ statement change subtly: name
+lookup first searches the local name space, then the global one, then
+the built-in one.}
The following table lists the meaning of the local and global name
space for various types of code blocks. The name space for a
particular module is automatically created when the module is first
-referenced.
+referenced. Note that in almost all cases, the global name space is
+the name space of the containing module -- scopes in Python do not
+nest!
\begin{center}
\begin{tabular}{|l|l|l|l|}
@@ -81,15 +91,18 @@ referenced.
Code block type & Global name space & Local name space & Notes \\
\hline
Module & n.s. for this module & same as global & \\
-Script & n.s. for \verb\__main__\ & same as global & \\
-Interactive command & n.s. for \verb\__main__\ & same as global & \\
+Script & n.s. for \verb@__main__@ & same as global & \\
+Interactive command & n.s. for \verb@__main__@ & same as global & \\
Class definition & global n.s. of containing block & new n.s. & \\
Function body & global n.s. of containing block & new n.s. & \\
-String passed to \verb\exec\ or \verb\eval\
+String passed to \verb@exec@ statement
+ & global n.s. of cobtaining block
+ & local n.s. of containing block & (1) \\
+String passed to \verb@eval()@
& global n.s. of caller & local n.s. of caller & (1) \\
-File read by \verb\execfile\
+File read by \verb@execfile()@
& global n.s. of caller & local n.s. of caller & (1) \\
-Expression read by \verb\input\
+Expression read by \verb@input@
& global n.s. of caller & local n.s. of caller & \\
\hline
\end{tabular}
@@ -101,7 +114,7 @@ Notes:
\item[n.s.] means {\em name space}
-\item[(1)] The global and local name space for these functions can be
+\item[(1)] The global and local name space for these can be
overridden with optional extra arguments.
\end{description}
@@ -123,8 +136,8 @@ where the error occurred.
The Python interpreter raises an exception when it detects an run-time
error (such as division by zero). A Python program can also
-explicitly raise an exception with the \verb\raise\ statement.
-Exception handlers are specified with the \verb\try...except\
+explicitly raise an exception with the \verb@raise@ statement.
+Exception handlers are specified with the \verb@try...except@
statement.
Python uses the ``termination'' model of error handling: an exception
@@ -139,10 +152,10 @@ execution of the program, or returns to its interactive main loop.
Exceptions are identified by string objects. Two different string
objects with the same value identify different exceptions.
-When an exception is raised, an object (maybe \verb\None\) is passed
+When an exception is raised, an object (maybe \verb@None@) is passed
as the exception's ``parameter''; this object does not affect the
selection of an exception handler, but is passed to the selected
exception handler as additional information.
-See also the description of the \verb\try\ and \verb\raise\
+See also the description of the \verb@try@ and \verb@raise@
statements.