diff options
-rw-r--r-- | Doc/ref/ref4.tex | 140 |
1 files changed, 68 insertions, 72 deletions
diff --git a/Doc/ref/ref4.tex b/Doc/ref/ref4.tex index 8df8fc2..673fea3 100644 --- a/Doc/ref/ref4.tex +++ b/Doc/ref/ref4.tex @@ -1,20 +1,19 @@ -\chapter{Execution model\label{execmodel}} +\chapter{Execution model \label{execmodel}} \index{execution model} -\section{Code blocks, execution frames, and namespaces\label{execframes}} +\section{Code blocks, execution frames, and namespaces \label{execframes}} \index{code block} -\indexii{execution}{frame} \index{namespace} +\indexii{execution}{frame} -A \dfn{code block} is a piece of Python program text that can be -executed as a unit, such as a module, a class definition or a function -body. Some code blocks (like modules) are normally executed only once, others -(like function bodies) may be executed many times. Code blocks may -textually contain other code blocks. Code blocks may invoke other -code blocks (that may or may not be textually contained in them) as -part of their execution, e.g., by invoking (calling) a function. -\index{code block} -\indexii{code}{block} +A \dfn{code block}\indexii{code}{block} is a piece +of Python program text that can be executed as a unit, such as a +module, a class definition or a function body. Some code blocks (like +modules) are normally executed only once, others (like function +bodies) may be executed many times. Code blocks may textually contain +other code blocks. Code blocks may invoke other code blocks (that may +or may not be textually contained in them) as part of their execution, +e.g., by invoking (calling) a function. 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 @@ -22,7 +21,7 @@ command typed interactively is a separate code 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 -`\code{-c}' option) is a code block. The file read by the built-in +`\strong{-c}' option) is a code block. The file read by the built-in function \function{execfile()} is a code block. The string argument passed to the built-in function \function{eval()} and to the \keyword{exec} statement is a code block. And finally, the expression @@ -30,33 +29,28 @@ read and evaluated by the built-in function \function{input()} is a code block. A code block is executed in an execution frame. An \dfn{execution -frame} contains some administrative information (used for debugging), -determines where and how execution continues after the code block's -execution has completed, and (perhaps most importantly) defines two -namespaces, the local and the global namespace, that affect -execution of the code block. -\indexii{execution}{frame} - -A \dfn{namespace} is a mapping from names (identifiers) to objects. -A particular namespace may be referenced by more than one execution -frame, and from other places as well. Adding a name to a namespace -is called \dfn{binding} a name (to an object); changing the mapping of -a name is called \dfn{rebinding}; removing a name is \dfn{unbinding}. -Namespaces are functionally equivalent to dictionaries (and often -implemented as dictionaries). -\index{namespace} -\indexii{binding}{name} -\indexii{rebinding}{name} -\indexii{unbinding}{name} - -The \dfn{local namespace} of an execution frame determines the default -place where names are defined and searched. The \dfn{global -namespace} determines the place where names listed in \keyword{global} -statements are defined and searched, and where names that are not -bound anywhere in the current code block are searched. -\indexii{local}{namespace} -\indexii{global}{namespace} -\stindex{global} +frame}\indexii{execution}{frame} contains some administrative +information (used for debugging), determines where and how execution +continues after the code block's execution has completed, and (perhaps +most importantly) defines two namespaces, the local and the global +namespace, that affect execution of the code block. + +A \dfn{namespace}\index{namespace} is a mapping from names +(identifiers) to objects. A particular namespace may be referenced by +more than one execution frame, and from other places as well. Adding +a name to a namespace is called \dfn{binding}\indexii{binding}{name} a +name (to an object); changing the mapping of a name is called +\dfn{rebinding}\indexii{rebinding}{name}; removing a name is +\dfn{unbinding}\indexii{unbinding}{name}. Namespaces are functionally +equivalent to dictionaries (and often implemented as dictionaries). + +The \dfn{local namespace}\indexii{local}{namespace} of an execution +frame determines the default place where names are defined and +searched. The +\dfn{global namespace}\indexii{global}{namespace} determines the place +where names listed in \keyword{global}\stindex{global} statements are +defined and searched, and where names that are not bound anywhere 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 @@ -72,10 +66,10 @@ header, or in the second position of an \keyword{except} clause header. Local names are searched only on the local namespace; global names are searched only in the global and built-in namespace.\footnote{ -If the code block contains \keyword{exec} statements or the construct -``\samp{from \ldots import *}'', the semantics of local names change: -local name lookup first searches the local namespace, then the global -namespace and the built-in namespace.} + If the code block contains \keyword{exec} statements or the + construct ``\samp{from \ldots import *}'', the semantics of local + names change: local name lookup first searches the local namespace, + then the global namespace and the built-in namespace.} A target occurring in a \keyword{del} statement is also considered bound for this purpose (though the actual semantics are to ``unbind'' the @@ -83,20 +77,21 @@ name). When a global name is not found in the global namespace, it is searched in the built-in namespace (which is actually the global -namespace of the module \module{__builtin__}). The built-in namespace -associated with the execution of a code block is actually found by -looking up the name \code{__builtins__} is its global namespace; this -should be a dictionary or a module (in the latter case its dictionary -is used). Normally, the \code{__builtins__} namespace is the -dictionary of the built-in module \module{__builtin__} (note: no `s'); -if it isn't, restricted execution mode is in effect. When a name is -not found at all, a \exception{NameError} exception is raised.% -\refbimodindex{__builtin__} +namespace of the module +\module{__builtin__}\refbimodindex{__builtin__}). The built-in +namespace associated with the execution of a code block is actually +found by looking up the name \code{__builtins__} is its global +namespace; this should be a dictionary or a module (in the latter case +its dictionary is used). Normally, the \code{__builtins__} namespace +is the dictionary of the built-in module \module{__builtin__} (note: +no `s'); if it isn't, restricted +execution\indexii{restricted}{execution} mode is in effect. When a +name is not found at all, a +\exception{NameError}\withsubitem{(built-in +exception)}{\ttindex{NameError}} exception is raised. \stindex{from} \stindex{exec} \stindex{global} -\indexii{restricted}{execution} -\withsubitem{(built-in exception)}{\ttindex{NameError}} The following table lists the meaning of the local and global namespace for various types of code blocks. The namespace for a @@ -111,10 +106,10 @@ scopes in Python do not nest! {n.s. for this module} {same as global}{} \lineiv{Script (file or command)} - {n.s. for \module{__main__}} + {n.s. for \module{__main__}\refbimodindex{__main__}} {same as global}{(1)} \lineiv{Interactive command} - {n.s. for \module{__main__}} + {n.s. for \module{__main__}\refbimodindex{__main__}} {same as global}{} \lineiv{Class definition} {global n.s. of containing block} @@ -135,7 +130,6 @@ scopes in Python do not nest! {global n.s. of caller} {local n.s. of caller}{} \end{tableiv} -\refbimodindex{__main__} Notes: @@ -160,22 +154,23 @@ The built-in functions \function{globals()} and \function{locals()} returns a dictionary representing the current global and local namespace, respectively. The effect of modifications to this dictionary on the namespace are undefined.\footnote{ -The current implementations return the dictionary actually used to -implement the namespace, \emph{except} for functions, where the -optimizer may cause the local namespace to be implemented differently, -and \function{locals()} returns a read-only dictionary.} + The current implementations return the dictionary actually used to + implement the namespace, \emph{except} for functions, where the + optimizer may cause the local namespace to be implemented + differently, and \function{locals()} returns a read-only + dictionary.} -\section{Exceptions\label{exceptions}} + +\section{Exceptions \label{exceptions}} +\index{exception} Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional -conditions. An exception is \emph{raised} at the point where the error -is detected; it may be \emph{handled} by the surrounding code block or -by any code block that directly or indirectly invoked the code block -where the error occurred. -\index{exception} -\index{raise an exception} -\index{handle an exception} +conditions. An exception is +\emph{raised}\index{raise an exception} at the point where the error +is detected; it may be \emph{handled}\index{handle an exception} by +the surrounding code block or by any code block that directly or +indirectly invoked the code block where the error occurred. \index{exception handler} \index{errors} \index{error handling} @@ -197,7 +192,8 @@ 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 -\exception{SystemExit}.\ttindex{SystemExit} +\exception{SystemExit}\withsubitem{(built-in +exception)}{\ttindex{SystemExit}}. Exceptions are identified by string objects or class instances. Selection of a matching except clause is based on object identity @@ -215,4 +211,4 @@ exceptions, this object must be an instance of the exception class being raised. See also the description of the \keyword{try} and \keyword{raise} -statements in chapter 7. +statements in chapter \ref{compound}. |