blob: 429302a9070e2ad8ac11e018e048f56cfbecd837 [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 Rossumaaec4031995-03-21 14:41:57 +000080\bimodindex{__builtin__}
81\stindex{from}
82\stindex{exec}
83\stindex{global}
84\ttindex{NameError}
Guido van Rossum46f3e001992-08-14 09:11:01 +000085
86The following table lists the meaning of the local and global name
87space for various types of code blocks. The name space for a
88particular module is automatically created when the module is first
Guido van Rossum6938f061994-08-01 12:22:53 +000089referenced. Note that in almost all cases, the global name space is
Guido van Rossum86751151995-02-28 17:14:32 +000090the name space of the containing module --- scopes in Python do not
Guido van Rossum6938f061994-08-01 12:22:53 +000091nest!
Guido van Rossum46f3e001992-08-14 09:11:01 +000092
93\begin{center}
94\begin{tabular}{|l|l|l|l|}
95\hline
96Code block type & Global name space & Local name space & Notes \\
97\hline
98Module & n.s. for this module & same as global & \\
Guido van Rossum6938f061994-08-01 12:22:53 +000099Script & n.s. for \verb@__main__@ & same as global & \\
100Interactive command & n.s. for \verb@__main__@ & same as global & \\
Guido van Rossum46f3e001992-08-14 09:11:01 +0000101Class definition & global n.s. of containing block & new n.s. & \\
102Function body & global n.s. of containing block & new n.s. & \\
Guido van Rossum6938f061994-08-01 12:22:53 +0000103String passed to \verb@exec@ statement
104 & global n.s. of cobtaining block
105 & local n.s. of containing block & (1) \\
106String passed to \verb@eval()@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000107 & global n.s. of caller & local n.s. of caller & (1) \\
Guido van Rossum6938f061994-08-01 12:22:53 +0000108File read by \verb@execfile()@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000109 & global n.s. of caller & local n.s. of caller & (1) \\
Guido van Rossum6938f061994-08-01 12:22:53 +0000110Expression read by \verb@input@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000111 & global n.s. of caller & local n.s. of caller & \\
112\hline
113\end{tabular}
114\end{center}
Guido van Rossumaaec4031995-03-21 14:41:57 +0000115\bimodindex{__main__}
Guido van Rossum46f3e001992-08-14 09:11:01 +0000116
117Notes:
118
119\begin{description}
120
121\item[n.s.] means {\em name space}
122
Guido van Rossum6938f061994-08-01 12:22:53 +0000123\item[(1)] The global and local name space for these can be
Guido van Rossum46f3e001992-08-14 09:11:01 +0000124overridden with optional extra arguments.
125
126\end{description}
127
Guido van Rossum611be701995-07-07 23:06:33 +0000128The built-in functions \verb@globals()@ and \verb@locals()@ returns a
129dictionary representing the current global and local name space,
130respectively. The effect of modifications to this dictionary on the
131name space are undefined.%
132\footnote{The current implementations return the dictionary actually
Guido van Rossum46f21571995-03-07 10:09:34 +0000133used to implement the name space, {\em except} for functions, where
134the optimizer may cause the local name space to be implemented
Guido van Rossum611be701995-07-07 23:06:33 +0000135differently, and \verb@locals()@ returns a read-only dictionary.}
Guido van Rossum46f21571995-03-07 10:09:34 +0000136
Guido van Rossum46f3e001992-08-14 09:11:01 +0000137\section{Exceptions}
138
139Exceptions are a means of breaking out of the normal flow of control
140of a code block in order to handle errors or other exceptional
141conditions. An exception is {\em raised} at the point where the error
142is detected; it may be {\em handled} by the surrounding code block or
143by any code block that directly or indirectly invoked the code block
144where the error occurred.
145\index{exception}
146\index{raise an exception}
147\index{handle an exception}
148\index{exception handler}
149\index{errors}
150\index{error handling}
151
152The Python interpreter raises an exception when it detects an run-time
153error (such as division by zero). A Python program can also
Guido van Rossum6938f061994-08-01 12:22:53 +0000154explicitly raise an exception with the \verb@raise@ statement.
155Exception handlers are specified with the \verb@try...except@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000156statement.
157
158Python uses the ``termination'' model of error handling: an exception
159handler can find out what happened and continue execution at an outer
160level, but it cannot repair the cause of the error and retry the
161failing operation (except by re-entering the the offending piece of
162code from the top).
163
164When an exception is not handled at all, the interpreter terminates
165execution of the program, or returns to its interactive main loop.
166
167Exceptions are identified by string objects. Two different string
168objects with the same value identify different exceptions.
169
Guido van Rossum6938f061994-08-01 12:22:53 +0000170When an exception is raised, an object (maybe \verb@None@) is passed
Guido van Rossum46f3e001992-08-14 09:11:01 +0000171as the exception's ``parameter''; this object does not affect the
172selection of an exception handler, but is passed to the selected
173exception handler as additional information.
174
Guido van Rossum6938f061994-08-01 12:22:53 +0000175See also the description of the \verb@try@ and \verb@raise@
Guido van Rossum46f3e001992-08-14 09:11:01 +0000176statements.