blob: f8677b5bc4b4cd67a9d9f8fb644340fdbd06e70a [file] [log] [blame]
Guido van Rossum46f3e001992-08-14 09:11:01 +00001\chapter{Execution model}
2\index{execution model}
3
4\section{Code blocks, execution frames, and name spaces} \label{execframes}
5\index{code block}
6\indexii{execution}{frame}
7\index{name space}
8
9A {\em code block} is a piece of Python program text that can be
10executed as a unit, such as a module, a class definition or a function
11body. Some code blocks (like modules) are executed only once, others
12(like function bodies) may be executed many times. Code block may
13textually contain other code blocks. Code blocks may invoke other
14code blocks (that may or may not be textually contained in them) as
15part of their execution, e.g. by invoking (calling) a function.
16\index{code block}
17\indexii{code}{block}
18
19The following are code blocks: A module is a code block. A function
20body is a code block. A class definition is a code block. Each
21command typed interactively is a separate code block; a script file is
22a code block. The string argument passed to the built-in functions
23\verb\eval\ and \verb\exec\ are code blocks. And finally, the
24expression read and evaluated by the built-in function \verb\input\ is
25a code block.
26
27A code block is executed in an execution frame. An {\em execution
28frame} contains some administrative information (used for debugging),
29determines where and how execution continues after the code block's
30execution has completed, and (perhaps most importantly) defines two
31name spaces, the local and the global name space, that affect
32execution of the code block.
33\indexii{execution}{frame}
34
35A {\em name space} is a mapping from names (identifiers) to objects.
36A particular name space may be referenced by more than one execution
37frame, and from other places as well. Adding a name to a name space
38is called {\em binding} a name (to an object); changing the mapping of
39a name is called {\em rebinding}; removing a name is {\em unbinding}.
40Name spaces are functionally equivalent to dictionaries.
41\index{name space}
42\indexii{binding}{name}
43\indexii{rebinding}{name}
44\indexii{unbinding}{name}
45
46The {\em local name space} of an execution frame determines the default
47place where names are defined and searched. The {\em global name
48space} determines the place where names listed in \verb\global\
49statements are defined and searched, and where names that are not
50explicitly bound in the current code block are searched.
51\indexii{local}{name space}
52\indexii{global}{name space}
53\stindex{global}
54
55Whether a name is local or global in a code block is determined by
56static inspection of the source text for the code block: in the
57absence of \verb\global\ statements, a name that is bound anywhere in
58the code block is local in the entire code block; all other names are
59considered global. The \verb\global\ statement forces global
60interpretation of selected names throughout the code block. The
61following constructs bind names: formal parameters, \verb\import\
62statements, class and function definitions (these bind the class or
63function name), and targets that are identifiers if occurring in an
64assignment, \verb\for\ loop header, or \verb\except\ clause header.
65(A target occurring in a \verb\del\ statement does not bind a name.)
66
67When a global name is not found in the global name space, it is
68searched in the list of ``built-in'' names (which is actually the
69global name space of the module \verb\builtin\). When a name is not
70found at all, the \verb\NameError\ exception is raised.
71
72The following table lists the meaning of the local and global name
73space for various types of code blocks. The name space for a
74particular module is automatically created when the module is first
75referenced.
76
77\begin{center}
78\begin{tabular}{|l|l|l|l|}
79\hline
80Code block type & Global name space & Local name space & Notes \\
81\hline
82Module & n.s. for this module & same as global & \\
83Script & n.s. for \verb\__main__\ & same as global & \\
84Interactive command & n.s. for \verb\__main__\ & same as global & \\
85Class definition & global n.s. of containing block & new n.s. & \\
86Function body & global n.s. of containing block & new n.s. & \\
87String passed to \verb\exec\ or \verb\eval\
88 & global n.s. of caller & local n.s. of caller & (1) \\
89File read by \verb\execfile\
90 & global n.s. of caller & local n.s. of caller & (1) \\
91Expression read by \verb\input\
92 & global n.s. of caller & local n.s. of caller & \\
93\hline
94\end{tabular}
95\end{center}
96
97Notes:
98
99\begin{description}
100
101\item[n.s.] means {\em name space}
102
103\item[(1)] The global and local name space for these functions can be
104overridden with optional extra arguments.
105
106\end{description}
107
108\section{Exceptions}
109
110Exceptions are a means of breaking out of the normal flow of control
111of a code block in order to handle errors or other exceptional
112conditions. An exception is {\em raised} at the point where the error
113is detected; it may be {\em handled} by the surrounding code block or
114by any code block that directly or indirectly invoked the code block
115where the error occurred.
116\index{exception}
117\index{raise an exception}
118\index{handle an exception}
119\index{exception handler}
120\index{errors}
121\index{error handling}
122
123The Python interpreter raises an exception when it detects an run-time
124error (such as division by zero). A Python program can also
125explicitly raise an exception with the \verb\raise\ statement.
126Exception handlers are specified with the \verb\try...except\
127statement.
128
129Python uses the ``termination'' model of error handling: an exception
130handler can find out what happened and continue execution at an outer
131level, but it cannot repair the cause of the error and retry the
132failing operation (except by re-entering the the offending piece of
133code from the top).
134
135When an exception is not handled at all, the interpreter terminates
136execution of the program, or returns to its interactive main loop.
137
138Exceptions are identified by string objects. Two different string
139objects with the same value identify different exceptions.
140
141When an exception is raised, an object (maybe \verb\None\) is passed
142as the exception's ``parameter''; this object does not affect the
143selection of an exception handler, but is passed to the selected
144exception handler as additional information.
145
146See also the description of the \verb\try\ and \verb\raise\
147statements.