blob: 8df8fc2d4d47593d6b2e703671e4abe8ef7a9e94 [file] [log] [blame]
Fred Drakea1cce711998-07-24 22:12:32 +00001\chapter{Execution model\label{execmodel}}
Fred Drakef6669171998-05-06 19:52:49 +00002\index{execution model}
3
Fred Drake61c77281998-07-28 19:34:22 +00004\section{Code blocks, execution frames, and namespaces\label{execframes}}
Fred Drakef6669171998-05-06 19:52:49 +00005\index{code block}
6\indexii{execution}{frame}
Guido van Rossumb18a93b1998-07-23 19:36:00 +00007\index{namespace}
Fred Drakef6669171998-05-06 19:52:49 +00008
Fred Drakea1cce711998-07-24 22:12:32 +00009A \dfn{code block} is a piece of Python program text that can be
Fred Drakef6669171998-05-06 19:52:49 +000010executed as a unit, such as a module, a class definition or a function
Guido van Rossumb18a93b1998-07-23 19:36:00 +000011body. Some code blocks (like modules) are normally executed only once, others
Fred Drakef6669171998-05-06 19:52:49 +000012(like function bodies) may be executed many times. Code blocks may
13textually contain other code blocks. Code blocks may invoke other
14code blocks (that may or may not be textually contained in them) as
Guido van Rossum7c0240f1998-07-24 15:36:43 +000015part of their execution, e.g., by invoking (calling) a function.
Fred Drakef6669171998-05-06 19:52:49 +000016\index{code block}
17\indexii{code}{block}
18
Guido van Rossumb18a93b1998-07-23 19:36:00 +000019The following are code blocks: A module is a code block. A function
Fred Drakef6669171998-05-06 19:52:49 +000020body is a code block. A class definition is a code block. Each
Guido van Rossumb18a93b1998-07-23 19:36:00 +000021command typed interactively is a separate code block; a script file (a
22file given as standard input to the interpreter or specified on the
23interpreter command line the first argument) is a code block; a script
24command (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
26function \function{execfile()} is a code block. The string argument
27passed to the built-in function \function{eval()} and to the
28\keyword{exec} statement is a code block. And finally, the expression
29read and evaluated by the built-in function \function{input()} is a
30code block.
Fred Drakef6669171998-05-06 19:52:49 +000031
Fred Drakea1cce711998-07-24 22:12:32 +000032A code block is executed in an execution frame. An \dfn{execution
Fred Drakef6669171998-05-06 19:52:49 +000033frame} contains some administrative information (used for debugging),
34determines where and how execution continues after the code block's
35execution has completed, and (perhaps most importantly) defines two
Guido van Rossumb18a93b1998-07-23 19:36:00 +000036namespaces, the local and the global namespace, that affect
Fred Drakef6669171998-05-06 19:52:49 +000037execution of the code block.
38\indexii{execution}{frame}
39
Fred Drakea1cce711998-07-24 22:12:32 +000040A \dfn{namespace} is a mapping from names (identifiers) to objects.
Guido van Rossumb18a93b1998-07-23 19:36:00 +000041A particular namespace may be referenced by more than one execution
42frame, and from other places as well. Adding a name to a namespace
Fred Drakea1cce711998-07-24 22:12:32 +000043is called \dfn{binding} a name (to an object); changing the mapping of
44a name is called \dfn{rebinding}; removing a name is \dfn{unbinding}.
Guido van Rossumb18a93b1998-07-23 19:36:00 +000045Namespaces are functionally equivalent to dictionaries (and often
46implemented as dictionaries).
47\index{namespace}
Fred Drakef6669171998-05-06 19:52:49 +000048\indexii{binding}{name}
49\indexii{rebinding}{name}
50\indexii{unbinding}{name}
51
Fred Drakea1cce711998-07-24 22:12:32 +000052The \dfn{local namespace} of an execution frame determines the default
53place where names are defined and searched. The \dfn{global
Guido van Rossumb18a93b1998-07-23 19:36:00 +000054namespace} determines the place where names listed in \keyword{global}
Fred Drakef6669171998-05-06 19:52:49 +000055statements are defined and searched, and where names that are not
Guido van Rossumb18a93b1998-07-23 19:36:00 +000056bound anywhere in the current code block are searched.
57\indexii{local}{namespace}
58\indexii{global}{namespace}
Fred Drakef6669171998-05-06 19:52:49 +000059\stindex{global}
60
61Whether a name is local or global in a code block is determined by
62static inspection of the source text for the code block: in the
Guido van Rossumb18a93b1998-07-23 19:36:00 +000063absence of \keyword{global} statements, a name that is bound anywhere
64in the code block is local in the entire code block; all other names
65are considered global. The \keyword{global} statement forces global
Fred Drakef6669171998-05-06 19:52:49 +000066interpretation of selected names throughout the code block. The
Guido van Rossumb18a93b1998-07-23 19:36:00 +000067following constructs bind names: formal parameters to functions,
68\keyword{import} statements, class and function definitions (these
69bind the class or function name in the defining block), and targets
70that are identifiers if occurring in an assignment, \keyword{for} loop
71header, or in the second position of an \keyword{except} clause
72header. Local names are searched only on the local namespace; global
Fred Drakeb55ce1e1999-04-05 21:32:52 +000073names are searched only in the global and built-in
74namespace.\footnote{
75If the code block contains \keyword{exec} statements or the construct
76``\samp{from \ldots import *}'', the semantics of local names change:
77local name lookup first searches the local namespace, then the global
78namespace and the built-in namespace.}
Fred Drakef6669171998-05-06 19:52:49 +000079
80A target occurring in a \keyword{del} statement is also considered bound
81for this purpose (though the actual semantics are to ``unbind'' the
82name).
83
Guido van Rossumb18a93b1998-07-23 19:36:00 +000084When a global name is not found in the global namespace, it is
85searched in the built-in namespace (which is actually the global
86namespace of the module \module{__builtin__}). The built-in namespace
87associated with the execution of a code block is actually found by
88looking up the name \code{__builtins__} is its global namespace; this
89should be a dictionary or a module (in the latter case its dictionary
90is used). Normally, the \code{__builtins__} namespace is the
91dictionary of the built-in module \module{__builtin__} (note: no `s');
92if it isn't, restricted execution mode is in effect. When a name is
93not found at all, a \exception{NameError} exception is raised.%
Fred Drakef6669171998-05-06 19:52:49 +000094\refbimodindex{__builtin__}
95\stindex{from}
96\stindex{exec}
97\stindex{global}
Guido van Rossumb18a93b1998-07-23 19:36:00 +000098\indexii{restricted}{execution}
Fred Drakef6669171998-05-06 19:52:49 +000099\withsubitem{(built-in exception)}{\ttindex{NameError}}
100
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000101The following table lists the meaning of the local and global
102namespace for various types of code blocks. The namespace for a
Fred Drakef6669171998-05-06 19:52:49 +0000103particular module is automatically created when the module is first
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000104imported (i.e., when it is loaded). Note that in almost all cases,
105the global namespace is the namespace of the containing module ---
106scopes in Python do not nest!
Fred Drakef6669171998-05-06 19:52:49 +0000107
Fred Drakedad11321998-10-21 00:26:56 +0000108\begin{tableiv}{l|l|l|l}{textrm}
Fred Drakea1cce711998-07-24 22:12:32 +0000109 {Code block type}{Global namespace}{Local namespace}{Notes}
Fred Drakedad11321998-10-21 00:26:56 +0000110 \lineiv{Module}
111 {n.s. for this module}
Fred Drakea1cce711998-07-24 22:12:32 +0000112 {same as global}{}
Fred Drakedad11321998-10-21 00:26:56 +0000113 \lineiv{Script (file or command)}
114 {n.s. for \module{__main__}}
Fred Drakea1cce711998-07-24 22:12:32 +0000115 {same as global}{(1)}
Fred Drakedad11321998-10-21 00:26:56 +0000116 \lineiv{Interactive command}
117 {n.s. for \module{__main__}}
Fred Drakea1cce711998-07-24 22:12:32 +0000118 {same as global}{}
Fred Drakedad11321998-10-21 00:26:56 +0000119 \lineiv{Class definition}
120 {global n.s. of containing block}
Fred Drakea1cce711998-07-24 22:12:32 +0000121 {new n.s.}{}
Fred Drakedad11321998-10-21 00:26:56 +0000122 \lineiv{Function body}
123 {global n.s. of containing block}
Fred Drakea1cce711998-07-24 22:12:32 +0000124 {new n.s.}{(2)}
Fred Drakedad11321998-10-21 00:26:56 +0000125 \lineiv{String passed to \keyword{exec} statement}
126 {global n.s. of containing block}
Fred Drakea1cce711998-07-24 22:12:32 +0000127 {local n.s. of containing block}{(2), (3)}
Fred Drakedad11321998-10-21 00:26:56 +0000128 \lineiv{String passed to \function{eval()}}
129 {global n.s. of caller}
Fred Drakea1cce711998-07-24 22:12:32 +0000130 {local n.s. of caller}{(2), (3)}
Fred Drakedad11321998-10-21 00:26:56 +0000131 \lineiv{File read by \function{execfile()}}
132 {global n.s. of caller}
Fred Drakea1cce711998-07-24 22:12:32 +0000133 {local n.s. of caller}{(2), (3)}
Fred Drakedad11321998-10-21 00:26:56 +0000134 \lineiv{Expression read by \function{input()}}
135 {global n.s. of caller}
Fred Drakea1cce711998-07-24 22:12:32 +0000136 {local n.s. of caller}{}
137\end{tableiv}
Fred Drakef6669171998-05-06 19:52:49 +0000138\refbimodindex{__main__}
139
140Notes:
141
142\begin{description}
143
Fred Drakea1cce711998-07-24 22:12:32 +0000144\item[n.s.] means \emph{namespace}
Fred Drakef6669171998-05-06 19:52:49 +0000145
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000146\item[(1)] The main module for a script is always called
147\module{__main__}; ``the filename don't enter into it.''
148
149\item[(2)] The global and local namespace for these can be
Fred Drakef6669171998-05-06 19:52:49 +0000150overridden with optional extra arguments.
151
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000152\item[(3)] The \keyword{exec} statement and the \function{eval()} and
153\function{execfile()} functions have optional arguments to override
154the global and local namespace. If only one namespace is specified,
155it is used for both.
Fred Drakef6669171998-05-06 19:52:49 +0000156
157\end{description}
158
159The built-in functions \function{globals()} and \function{locals()} returns a
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000160dictionary representing the current global and local namespace,
Fred Drakef6669171998-05-06 19:52:49 +0000161respectively. The effect of modifications to this dictionary on the
Fred Drakeb55ce1e1999-04-05 21:32:52 +0000162namespace are undefined.\footnote{
163The current implementations return the dictionary actually used to
164implement the namespace, \emph{except} for functions, where the
165optimizer may cause the local namespace to be implemented differently,
166and \function{locals()} returns a read-only dictionary.}
Fred Drakef6669171998-05-06 19:52:49 +0000167
Fred Drake61c77281998-07-28 19:34:22 +0000168\section{Exceptions\label{exceptions}}
Fred Drakef6669171998-05-06 19:52:49 +0000169
170Exceptions are a means of breaking out of the normal flow of control
171of a code block in order to handle errors or other exceptional
Fred Drakea1cce711998-07-24 22:12:32 +0000172conditions. An exception is \emph{raised} at the point where the error
173is detected; it may be \emph{handled} by the surrounding code block or
Fred Drakef6669171998-05-06 19:52:49 +0000174by any code block that directly or indirectly invoked the code block
175where the error occurred.
176\index{exception}
177\index{raise an exception}
178\index{handle an exception}
179\index{exception handler}
180\index{errors}
181\index{error handling}
182
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000183The Python interpreter raises an exception when it detects a run-time
Fred Drakef6669171998-05-06 19:52:49 +0000184error (such as division by zero). A Python program can also
185explicitly raise an exception with the \keyword{raise} statement.
186Exception handlers are specified with the \keyword{try} ... \keyword{except}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000187statement. The \keyword{try} ... \keyword{finally} statement
188specifies cleanup code which does not handle the exception, but is
189executed whether an exception occurred or not in the preceding code.
Fred Drakef6669171998-05-06 19:52:49 +0000190
191Python uses the ``termination'' model of error handling: an exception
192handler can find out what happened and continue execution at an outer
193level, but it cannot repair the cause of the error and retry the
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000194failing operation (except by re-entering the offending piece of
Fred Drakef6669171998-05-06 19:52:49 +0000195code from the top).
196
197When an exception is not handled at all, the interpreter terminates
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000198execution of the program, or returns to its interactive main loop. In
199either case, it prints a stack backtrace, except when the exception is
200\exception{SystemExit}.\ttindex{SystemExit}
Fred Drakef6669171998-05-06 19:52:49 +0000201
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000202Exceptions are identified by string objects or class instances.
203Selection of a matching except clause is based on object identity
204(i.e., two different string objects with the same value represent
205different exceptions!) For string exceptions, the \keyword{except}
206clause must reference the same string object. For class exceptions,
207the \keyword{except} clause must reference the same class or a base
208class of it.
Fred Drakef6669171998-05-06 19:52:49 +0000209
210When an exception is raised, an object (maybe \code{None}) is passed
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000211as the exception's ``parameter'' or ``value''; this object does not
212affect the selection of an exception handler, but is passed to the
213selected exception handler as additional information. For class
214exceptions, this object must be an instance of the exception class
215being raised.
Fred Drakef6669171998-05-06 19:52:49 +0000216
217See also the description of the \keyword{try} and \keyword{raise}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000218statements in chapter 7.