1- \chapter {Execution model\label {execmodel } }
1+ \chapter {Execution model \label {execmodel } }
22\index {execution model}
33
4- \section {Code blocks, execution frames, and namespaces\label {execframes } }
4+ \section {Code blocks, execution frames, and namespaces \label {execframes } }
55\index {code block}
6- \indexii {execution}{frame}
76\index {namespace}
7+ \indexii {execution}{frame}
88
9- A \dfn {code block} is a piece of Python program text that can be
10- executed as a unit, such as a module, a class definition or a function
11- body. Some code blocks (like modules) are normally executed only once, others
12- (like function bodies) may be executed many times. Code blocks may
13- textually contain other code blocks. Code blocks may invoke other
14- code blocks (that may or may not be textually contained in them) as
15- part of their execution, e.g., by invoking (calling) a function.
16- \index {code block}
17- \indexii {code}{block}
9+ A \dfn {code block}\indexii {code}{block} is a piece
10+ of Python program text that can be executed as a unit, such as a
11+ module, a class definition or a function body. Some code blocks (like
12+ modules) are normally executed only once, others (like function
13+ bodies) may be executed many times. Code blocks may textually contain
14+ other code blocks. Code blocks may invoke other code blocks (that may
15+ or may not be textually contained in them) as part of their execution,
16+ e.g., by invoking (calling) a function.
1817
1918The following are code blocks: A module is a code block. A function
2019body is a code block. A class definition is a code block. Each
2120command typed interactively is a separate code block; a script file (a
2221file given as standard input to the interpreter or specified on the
2322interpreter command line the first argument) is a code block; a script
2423command (a command specified on the interpreter command line with the
25- `\code {-c}' option) is a code block. The file read by the built-in
24+ `\strong {-c}' option) is a code block. The file read by the built-in
2625function \function {execfile()} is a code block. The string argument
2726passed to the built-in function \function {eval()} and to the
2827\keyword {exec} statement is a code block. And finally, the expression
2928read and evaluated by the built-in function \function {input()} is a
3029code block.
3130
3231A code block is executed in an execution frame. An \dfn {execution
33- frame} contains some administrative information (used for debugging),
34- determines where and how execution continues after the code block's
35- execution has completed, and (perhaps most importantly) defines two
36- namespaces, the local and the global namespace, that affect
37- execution of the code block.
38- \indexii {execution}{frame}
39-
40- A \dfn {namespace} is a mapping from names (identifiers) to objects.
41- A particular namespace may be referenced by more than one execution
42- frame, and from other places as well. Adding a name to a namespace
43- is called \dfn {binding} a name (to an object); changing the mapping of
44- a name is called \dfn {rebinding}; removing a name is \dfn {unbinding}.
45- Namespaces are functionally equivalent to dictionaries (and often
46- implemented as dictionaries).
47- \index {namespace}
48- \indexii {binding}{name}
49- \indexii {rebinding}{name}
50- \indexii {unbinding}{name}
51-
52- The \dfn {local namespace} of an execution frame determines the default
53- place where names are defined and searched. The \dfn {global
54- namespace} determines the place where names listed in \keyword {global}
55- statements are defined and searched, and where names that are not
56- bound anywhere in the current code block are searched.
57- \indexii {local}{namespace}
58- \indexii {global}{namespace}
59- \stindex {global}
32+ frame}\indexii {execution}{frame} contains some administrative
33+ information (used for debugging), determines where and how execution
34+ continues after the code block's execution has completed, and (perhaps
35+ most importantly) defines two namespaces, the local and the global
36+ namespace, that affect execution of the code block.
37+
38+ A \dfn {namespace}\index {namespace} is a mapping from names
39+ (identifiers) to objects. A particular namespace may be referenced by
40+ more than one execution frame, and from other places as well. Adding
41+ a name to a namespace is called \dfn {binding}\indexii {binding}{name} a
42+ name (to an object); changing the mapping of a name is called
43+ \dfn {rebinding}\indexii {rebinding}{name}; removing a name is
44+ \dfn {unbinding}\indexii {unbinding}{name}. Namespaces are functionally
45+ equivalent to dictionaries (and often implemented as dictionaries).
46+
47+ The \dfn {local namespace}\indexii {local}{namespace} of an execution
48+ frame determines the default place where names are defined and
49+ searched. The
50+ \dfn {global namespace}\indexii {global}{namespace} determines the place
51+ where names listed in \keyword {global}\stindex {global} statements are
52+ defined and searched, and where names that are not bound anywhere in
53+ the current code block are searched.
6054
6155Whether a name is local or global in a code block is determined by
6256static inspection of the source text for the code block: in the
@@ -72,31 +66,32 @@ \section{Code blocks, execution frames, and namespaces\label{execframes}}
7266header. Local names are searched only on the local namespace; global
7367names are searched only in the global and built-in
7468namespace.\footnote {
75- If the code block contains \keyword {exec} statements or the construct
76- `` \samp {from \ldots import *}'' , the semantics of local names change:
77- local name lookup first searches the local namespace, then the global
78- namespace and the built-in namespace.}
69+ If the code block contains \keyword {exec} statements or the
70+ construct `` \samp {from \ldots import *}'' , the semantics of local
71+ names change: local name lookup first searches the local namespace,
72+ then the global namespace and the built-in namespace.}
7973
8074A target occurring in a \keyword {del} statement is also considered bound
8175for this purpose (though the actual semantics are to `` unbind'' the
8276name).
8377
8478When a global name is not found in the global namespace, it is
8579searched in the built-in namespace (which is actually the global
86- namespace of the module \module {__builtin__}). The built-in namespace
87- associated with the execution of a code block is actually found by
88- looking up the name \code {__builtins__} is its global namespace; this
89- should be a dictionary or a module (in the latter case its dictionary
90- is used). Normally, the \code {__builtins__} namespace is the
91- dictionary of the built-in module \module {__builtin__} (note: no `s');
92- if it isn't, restricted execution mode is in effect. When a name is
93- not found at all, a \exception {NameError} exception is raised.%
94- \refbimodindex {__builtin__}
80+ namespace of the module
81+ \module {__builtin__}\refbimodindex {__builtin__}). The built-in
82+ namespace associated with the execution of a code block is actually
83+ found by looking up the name \code {__builtins__} is its global
84+ namespace; this should be a dictionary or a module (in the latter case
85+ its dictionary is used). Normally, the \code {__builtins__} namespace
86+ is the dictionary of the built-in module \module {__builtin__} (note:
87+ no `s'); if it isn't, restricted
88+ execution\indexii {restricted}{execution} mode is in effect. When a
89+ name is not found at all, a
90+ \exception {NameError}\withsubitem {(built-in
91+ exception)}{\ttindex {NameError}} exception is raised.
9592\stindex {from}
9693\stindex {exec}
9794\stindex {global}
98- \indexii {restricted}{execution}
99- \withsubitem {(built-in exception)}{\ttindex {NameError}}
10095
10196The following table lists the meaning of the local and global
10297namespace for various types of code blocks. The namespace for a
@@ -111,10 +106,10 @@ \section{Code blocks, execution frames, and namespaces\label{execframes}}
111106 {n.s. for this module}
112107 {same as global}{}
113108 \lineiv {Script (file or command)}
114- {n.s. for \module {__main__}}
109+ {n.s. for \module {__main__}\refbimodindex {__main__} }
115110 {same as global}{(1)}
116111 \lineiv {Interactive command}
117- {n.s. for \module {__main__}}
112+ {n.s. for \module {__main__}\refbimodindex {__main__} }
118113 {same as global}{}
119114 \lineiv {Class definition}
120115 {global n.s. of containing block}
@@ -135,7 +130,6 @@ \section{Code blocks, execution frames, and namespaces\label{execframes}}
135130 {global n.s. of caller}
136131 {local n.s. of caller}{}
137132\end {tableiv }
138- \refbimodindex {__main__}
139133
140134Notes:
141135
@@ -160,22 +154,23 @@ \section{Code blocks, execution frames, and namespaces\label{execframes}}
160154dictionary representing the current global and local namespace,
161155respectively. The effect of modifications to this dictionary on the
162156namespace are undefined.\footnote {
163- The current implementations return the dictionary actually used to
164- implement the namespace, \emph {except } for functions, where the
165- optimizer may cause the local namespace to be implemented differently,
166- and \function {locals()} returns a read-only dictionary.}
157+ The current implementations return the dictionary actually used to
158+ implement the namespace, \emph {except } for functions, where the
159+ optimizer may cause the local namespace to be implemented
160+ differently, and \function {locals()} returns a read-only
161+ dictionary.}
167162
168- \section {Exceptions\label {exceptions } }
163+
164+ \section {Exceptions \label {exceptions } }
165+ \index {exception}
169166
170167Exceptions are a means of breaking out of the normal flow of control
171168of a code block in order to handle errors or other exceptional
172- conditions. An exception is \emph {raised } at the point where the error
173- is detected; it may be \emph {handled } by the surrounding code block or
174- by any code block that directly or indirectly invoked the code block
175- where the error occurred.
176- \index {exception}
177- \index {raise an exception}
178- \index {handle an exception}
169+ conditions. An exception is
170+ \emph {raised }\index {raise an exception} at the point where the error
171+ is detected; it may be \emph {handled }\index {handle an exception} by
172+ the surrounding code block or by any code block that directly or
173+ indirectly invoked the code block where the error occurred.
179174\index {exception handler}
180175\index {errors}
181176\index {error handling}
@@ -197,7 +192,8 @@ \section{Exceptions\label{exceptions}}
197192When an exception is not handled at all, the interpreter terminates
198193execution of the program, or returns to its interactive main loop. In
199194either case, it prints a stack backtrace, except when the exception is
200- \exception {SystemExit}.\ttindex {SystemExit}
195+ \exception {SystemExit}\withsubitem {(built-in
196+ exception)}{\ttindex {SystemExit}}.
201197
202198Exceptions are identified by string objects or class instances.
203199Selection of a matching except clause is based on object identity
@@ -215,4 +211,4 @@ \section{Exceptions\label{exceptions}}
215211being raised.
216212
217213See also the description of the \keyword {try} and \keyword {raise}
218- statements in chapter 7 .
214+ statements in chapter \ref { compound } .
0 commit comments