1- \chapter {Execution model }
1+ \chapter {Execution model\label { execmodel } }
22\index {execution model}
33
44\section {Code blocks, execution frames, and namespaces } \label {execframes }
55\index {code block}
66\indexii {execution}{frame}
77\index {namespace}
88
9- A { \em code block} is a piece of Python program text that can be
9+ A \dfn { code block} is a piece of Python program text that can be
1010executed as a unit, such as a module, a class definition or a function
1111body. Some code blocks (like modules) are normally executed only once, others
1212(like function bodies) may be executed many times. Code blocks may
@@ -29,28 +29,28 @@ \section{Code blocks, execution frames, and namespaces} \label{execframes}
2929read and evaluated by the built-in function \function {input()} is a
3030code block.
3131
32- A code block is executed in an execution frame. An { \em execution
32+ A code block is executed in an execution frame. An \dfn { execution
3333frame} contains some administrative information (used for debugging),
3434determines where and how execution continues after the code block's
3535execution has completed, and (perhaps most importantly) defines two
3636namespaces, the local and the global namespace, that affect
3737execution of the code block.
3838\indexii {execution}{frame}
3939
40- A { \em namespace} is a mapping from names (identifiers) to objects.
40+ A \dfn { namespace} is a mapping from names (identifiers) to objects.
4141A particular namespace may be referenced by more than one execution
4242frame, and from other places as well. Adding a name to a namespace
43- is called { \em binding} a name (to an object); changing the mapping of
44- a name is called { \em rebinding}; removing a name is { \em unbinding}.
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}.
4545Namespaces are functionally equivalent to dictionaries (and often
4646implemented as dictionaries).
4747\index {namespace}
4848\indexii {binding}{name}
4949\indexii {rebinding}{name}
5050\indexii {unbinding}{name}
5151
52- The { \em local namespace} of an execution frame determines the default
53- place where names are defined and searched. The { \em global
52+ The \dfn { local namespace} of an execution frame determines the default
53+ place where names are defined and searched. The \dfn { global
5454namespace} determines the place where names listed in \keyword {global}
5555statements are defined and searched, and where names that are not
5656bound anywhere in the current code block are searched.
@@ -105,36 +105,43 @@ \section{Code blocks, execution frames, and namespaces} \label{execframes}
105105the global namespace is the namespace of the containing module ---
106106scopes in Python do not nest!
107107
108- \begin {center }
109- \begin {tabular }{|l|l|l|l|}
110- \hline
111- Code block type & Global namespace & Local namespace & Notes \\
112- \hline
113- Module & n.s. for this module & same as global & \\
114- Script (file or command) & n.s. for \module {__main__} & same as global
115- & (1) \\
116- Interactive command & n.s. for \module {__main__} & same as global & \\
117- Class definition & global n.s. of containing block & new n.s. & \\
118- Function body & global n.s. of containing block & new n.s. & (2) \\
119- String passed to \keyword {exec} statement
120- & global n.s. of containing block
121- & local n.s. of containing block & (2), (3) \\
122- String passed to \function {eval()}
123- & global n.s. of caller & local n.s. of caller & (2), (3) \\
124- File read by \function {execfile()}
125- & global n.s. of caller & local n.s. of caller & (2), (3) \\
126- Expression read by \function {input()}
127- & global n.s. of caller & local n.s. of caller & \\
128- \hline
129- \end {tabular }
130- \end {center }
108+ \begin {tableiv }{l|l|l|l}{textrm}%
109+ {Code block type}{Global namespace}{Local namespace}{Notes}
110+ \lineiv {Module}%
111+ {n.s. for this module}%
112+ {same as global}{}
113+ \lineiv {Script (file or command)}%
114+ {n.s. for \module {__main__}}%
115+ {same as global}{(1)}
116+ \lineiv {Interactive command}%
117+ {n.s. for \module {__main__}}%
118+ {same as global}{}
119+ \lineiv {Class definition}%
120+ {global n.s. of containing block}%
121+ {new n.s.}{}
122+ \lineiv {Function body}%
123+ {global n.s. of containing block}%
124+ {new n.s.}{(2)}
125+ \lineiv {String passed to \keyword {exec} statement}%
126+ {global n.s. of containing block}%
127+ {local n.s. of containing block}{(2), (3)}
128+ \lineiv {String passed to \function {eval()}}%
129+ {global n.s. of caller}%
130+ {local n.s. of caller}{(2), (3)}
131+ \lineiv {File read by \function {execfile()}}%
132+ {global n.s. of caller}%
133+ {local n.s. of caller}{(2), (3)}
134+ \lineiv {Expression read by \function {input()}}%
135+ {global n.s. of caller}%
136+ {local n.s. of caller}{}
137+ \end {tableiv }
131138\refbimodindex {__main__}
132139
133140Notes:
134141
135142\begin {description }
136143
137- \item [n.s.] means { \em namespace}
144+ \item [n.s.] means \emph { namespace }
138145
139146\item [(1)] The main module for a script is always called
140147\module {__main__}; `` the filename don't enter into it.''
@@ -154,16 +161,16 @@ \section{Code blocks, execution frames, and namespaces} \label{execframes}
154161respectively. The effect of modifications to this dictionary on the
155162namespace are undefined.%
156163\footnote {The current implementations return the dictionary actually
157- used to implement the namespace, { \em except} for functions, where
164+ used to implement the namespace, \emph { except } for functions, where
158165the optimizer may cause the local namespace to be implemented
159166differently, and \function {locals()} returns a read-only dictionary.}
160167
161168\section {Exceptions }
162169
163170Exceptions are a means of breaking out of the normal flow of control
164171of a code block in order to handle errors or other exceptional
165- conditions. An exception is { \em raised} at the point where the error
166- is detected; it may be { \em handled} by the surrounding code block or
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
167174by any code block that directly or indirectly invoked the code block
168175where the error occurred.
169176\index {exception}
0 commit comments