blob: 151b0ae2f9a8d705f6613988949e1fdb85d981f5 [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
Guido van Rossum16d6e711994-08-08 12:30:22 +000012(like function bodies) may be executed many times. Code blocks may
Guido van Rossum46f3e001992-08-14 09:11:01 +000013textually 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
Guido van Rossum4bd023f1993-10-27 13:49:20 +000022a code block. The string argument passed to the built-in function
Guido van Rossum6938f061994-08-01 12:22:53 +000023\verb@eval@ and to the \verb@exec@ statement are code blocks.
Guido van Rossum4bd023f1993-10-27 13:49:20 +000024And finally, the
Guido van Rossum6938f061994-08-01 12:22:53 +000025expression read and evaluated by the built-in function \verb@input@ is
Guido van Rossum46f3e001992-08-14 09:11:01 +000026a code block.
27
28A code block is executed in an execution frame. An {\em execution
29frame} contains some administrative information (used for debugging),
30determines where and how execution continues after the code block's
31execution has completed, and (perhaps most importantly) defines two
32name spaces, the local and the global name space, that affect
33execution of the code block.
34\indexii{execution}{frame}
35
36A {\em name space} is a mapping from names (identifiers) to objects.
37A particular name space may be referenced by more than one execution
38frame, and from other places as well. Adding a name to a name space
39is called {\em binding} a name (to an object); changing the mapping of
40a name is called {\em rebinding}; removing a name is {\em unbinding}.
41Name spaces are functionally equivalent to dictionaries.
42\index{name space}
43\indexii{binding}{name}
44\indexii{rebinding}{name}
45\indexii{unbinding}{name}
46
47The {\em local name space} of an execution frame determines the default
48place where names are defined and searched. The {\em global name
Guido van Rossum6938f061994-08-01 12:22:53 +000049space} determines the place where names listed in \verb@global@
Guido van Rossum46f3e001992-08-14 09:11:01 +000050statements are defined and searched, and where names that are not
51explicitly bound in the current code block are searched.
52\indexii{local}{name space}
53\indexii{global}{name space}
54\stindex{global}
55
56Whether a name is local or global in a code block is determined by
57static inspection of the source text for the code block: in the
Guido van Rossum6938f061994-08-01 12:22:53 +000058absence of \verb@global@ statements, a name that is bound anywhere in
Guido van Rossum46f3e001992-08-14 09:11:01 +000059the code block is local in the entire code block; all other names are
Guido van Rossum6938f061994-08-01 12:22:53 +000060considered global. The \verb@global@ statement forces global
Guido van Rossum46f3e001992-08-14 09:11:01 +000061interpretation of selected names throughout the code block. The
Guido van Rossum6938f061994-08-01 12:22:53 +000062following constructs bind names: formal parameters, \verb@import@
Guido van Rossum46f3e001992-08-14 09:11:01 +000063statements, class and function definitions (these bind the class or
64function name), and targets that are identifiers if occurring in an
Guido van Rossum6938f061994-08-01 12:22:53 +000065assignment, \verb@for@ loop header, or \verb@except@ clause header.
66
67A target occurring in a \verb@del@ statement is also considered bound
68for this purpose (though the actual semantics are to ``unbind'' the
69name).
Guido van Rossum46f3e001992-08-14 09:11:01 +000070
71When a global name is not found in the global name space, it is
72searched in the list of ``built-in'' names (which is actually the
Guido van Rossum6938f061994-08-01 12:22:53 +000073global name space of the module \verb@__builtin__@). When a name is not
74found at all, the \verb@NameError@ exception is raised.%
Guido van Rossum31cce971995-01-04 19:17:34 +000075\footnote{If the code block contains {\tt exec} statements or the
76construct {\tt from \ldots import *}, the semantics of names not
77explicitly mentioned in a {\tt global} statement change subtly: name
Guido van Rossum6938f061994-08-01 12:22:53 +000078lookup first searches the local name space, then the global one, then
79the built-in one.}
Guido van Rossum46f3e001992-08-14 09:11:01 +000080
81The following table lists the meaning of the local and global name
82space for various types of code blocks. The name space for a
83particular module is automatically created when the module is first
Guido van Rossum6938f061994-08-01 12:22:53 +000084referenced. Note that in almost all cases, the global name space is
Guido van Rossum86751151995-02-28 17:14:32 +000085the name space of the containing module --- scopes in Python do not
Guido van Rossum6938f061994-08-01 12:22:53 +000086nest!
Guido van Rossum46f3e001992-08-14 09:11:01 +000087
88\begin{center}
89\begin{tabular}{|l|l|l|l|}
90\hline
91Code block type & Global name space & Local name space & Notes \\
92\hline
93Module & n.s. for this module & same as global & \\
Guido van Rossum6938f061994-08-01 12:22:53 +000094Script & n.s. for \verb@__main__@ & same as global & \\
95Interactive command & n.s. for \verb@__main__@ & same as global & \\
Guido van Rossum46f3e001992-08-14 09:11:01 +000096Class definition & global n.s. of containing block & new n.s. & \\
97Function body & global n.s. of containing block & new n.s. & \\
Guido van Rossum6938f061994-08-01 12:22:53 +000098String passed to \verb@exec@ statement
99 & global n.s. of cobtaining block
100 & local n.s. of containing block & (1) \\
101String passed to \verb@eval()@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000102 & global n.s. of caller & local n.s. of caller & (1) \\
Guido van Rossum6938f061994-08-01 12:22:53 +0000103File read by \verb@execfile()@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000104 & global n.s. of caller & local n.s. of caller & (1) \\
Guido van Rossum6938f061994-08-01 12:22:53 +0000105Expression read by \verb@input@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000106 & global n.s. of caller & local n.s. of caller & \\
107\hline
108\end{tabular}
109\end{center}
110
111Notes:
112
113\begin{description}
114
115\item[n.s.] means {\em name space}
116
Guido van Rossum6938f061994-08-01 12:22:53 +0000117\item[(1)] The global and local name space for these can be
Guido van Rossum46f3e001992-08-14 09:11:01 +0000118overridden with optional extra arguments.
119
120\end{description}
121
Guido van Rossum46f21571995-03-07 10:09:34 +0000122The built-in function \verb@vars()@ returns a dictionary representing
123the current local name space. The effect of modifications to this
124dictionary on the name space are undefined.%
125\footnote{The current implementation returns the dictionary actually
126used to implement the name space, {\em except} for functions, where
127the optimizer may cause the local name space to be implemented
128differently.}
129
Guido van Rossum46f3e001992-08-14 09:11:01 +0000130\section{Exceptions}
131
132Exceptions are a means of breaking out of the normal flow of control
133of a code block in order to handle errors or other exceptional
134conditions. An exception is {\em raised} at the point where the error
135is detected; it may be {\em handled} by the surrounding code block or
136by any code block that directly or indirectly invoked the code block
137where the error occurred.
138\index{exception}
139\index{raise an exception}
140\index{handle an exception}
141\index{exception handler}
142\index{errors}
143\index{error handling}
144
145The Python interpreter raises an exception when it detects an run-time
146error (such as division by zero). A Python program can also
Guido van Rossum6938f061994-08-01 12:22:53 +0000147explicitly raise an exception with the \verb@raise@ statement.
148Exception handlers are specified with the \verb@try...except@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000149statement.
150
151Python uses the ``termination'' model of error handling: an exception
152handler can find out what happened and continue execution at an outer
153level, but it cannot repair the cause of the error and retry the
154failing operation (except by re-entering the the offending piece of
155code from the top).
156
157When an exception is not handled at all, the interpreter terminates
158execution of the program, or returns to its interactive main loop.
159
160Exceptions are identified by string objects. Two different string
161objects with the same value identify different exceptions.
162
Guido van Rossum6938f061994-08-01 12:22:53 +0000163When an exception is raised, an object (maybe \verb@None@) is passed
Guido van Rossum46f3e001992-08-14 09:11:01 +0000164as the exception's ``parameter''; this object does not affect the
165selection of an exception handler, but is passed to the selected
166exception handler as additional information.
167
Guido van Rossum6938f061994-08-01 12:22:53 +0000168See also the description of the \verb@try@ and \verb@raise@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000169statements.