blob: 4d5f8d041af25e72e9826f949cbc707542001c8f [file] [log] [blame]
Fred Drakef6669171998-05-06 19:52:49 +00001\chapter{Execution model}
2\index{execution model}
3
Guido van Rossumb18a93b1998-07-23 19:36:00 +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
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
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
32A code block is executed in an execution frame. An {\em execution
33frame} 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
Guido van Rossumb18a93b1998-07-23 19:36:00 +000040A {\em namespace} is a mapping from names (identifiers) to objects.
41A particular namespace may be referenced by more than one execution
42frame, and from other places as well. Adding a name to a namespace
Fred Drakef6669171998-05-06 19:52:49 +000043is called {\em binding} a name (to an object); changing the mapping of
44a name is called {\em rebinding}; removing a name is {\em 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
Guido van Rossumb18a93b1998-07-23 19:36:00 +000052The {\em local namespace} of an execution frame determines the default
53place where names are defined and searched. The {\em global
54namespace} 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
73names are searched only in the global and built-in namespace.%
74%
75\footnote{If the code block contains \keyword{exec} statements or the
76construct ``\samp{from \ldots import *}'', the semantics of local
77names change: local name lookup first searches the local namespace,
78then the global namespace 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
108\begin{center}
109\begin{tabular}{|l|l|l|l|}
110\hline
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000111Code block type & Global namespace & Local namespace & Notes \\
Fred Drakef6669171998-05-06 19:52:49 +0000112\hline
113Module & n.s. for this module & same as global & \\
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000114Script (file or command) & n.s. for \module{__main__} & same as global
115 & (1) \\
Fred Drakef6669171998-05-06 19:52:49 +0000116Interactive command & n.s. for \module{__main__} & same as global & \\
117Class definition & global n.s. of containing block & new n.s. & \\
118Function body & global n.s. of containing block & new n.s. & (2) \\
119String passed to \keyword{exec} statement
120 & global n.s. of containing block
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000121 & local n.s. of containing block & (2), (3) \\
Fred Drakef6669171998-05-06 19:52:49 +0000122String passed to \function{eval()}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000123 & global n.s. of caller & local n.s. of caller & (2), (3) \\
Fred Drakef6669171998-05-06 19:52:49 +0000124File read by \function{execfile()}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000125 & global n.s. of caller & local n.s. of caller & (2), (3) \\
Fred Drakef6669171998-05-06 19:52:49 +0000126Expression read by \function{input()}
127 & global n.s. of caller & local n.s. of caller & \\
128\hline
129\end{tabular}
130\end{center}
131\refbimodindex{__main__}
132
133Notes:
134
135\begin{description}
136
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000137\item[n.s.] means {\em namespace}
Fred Drakef6669171998-05-06 19:52:49 +0000138
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000139\item[(1)] The main module for a script is always called
140\module{__main__}; ``the filename don't enter into it.''
141
142\item[(2)] The global and local namespace for these can be
Fred Drakef6669171998-05-06 19:52:49 +0000143overridden with optional extra arguments.
144
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000145\item[(3)] The \keyword{exec} statement and the \function{eval()} and
146\function{execfile()} functions have optional arguments to override
147the global and local namespace. If only one namespace is specified,
148it is used for both.
Fred Drakef6669171998-05-06 19:52:49 +0000149
150\end{description}
151
152The built-in functions \function{globals()} and \function{locals()} returns a
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000153dictionary representing the current global and local namespace,
Fred Drakef6669171998-05-06 19:52:49 +0000154respectively. The effect of modifications to this dictionary on the
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000155namespace are undefined.%
Fred Drakef6669171998-05-06 19:52:49 +0000156\footnote{The current implementations return the dictionary actually
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000157used to implement the namespace, {\em except} for functions, where
158the optimizer may cause the local namespace to be implemented
Fred Drakef6669171998-05-06 19:52:49 +0000159differently, and \function{locals()} returns a read-only dictionary.}
160
161\section{Exceptions}
162
163Exceptions are a means of breaking out of the normal flow of control
164of a code block in order to handle errors or other exceptional
165conditions. An exception is {\em raised} at the point where the error
166is detected; it may be {\em handled} by the surrounding code block or
167by any code block that directly or indirectly invoked the code block
168where the error occurred.
169\index{exception}
170\index{raise an exception}
171\index{handle an exception}
172\index{exception handler}
173\index{errors}
174\index{error handling}
175
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000176The Python interpreter raises an exception when it detects a run-time
Fred Drakef6669171998-05-06 19:52:49 +0000177error (such as division by zero). A Python program can also
178explicitly raise an exception with the \keyword{raise} statement.
179Exception handlers are specified with the \keyword{try} ... \keyword{except}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000180statement. The \keyword{try} ... \keyword{finally} statement
181specifies cleanup code which does not handle the exception, but is
182executed whether an exception occurred or not in the preceding code.
Fred Drakef6669171998-05-06 19:52:49 +0000183
184Python uses the ``termination'' model of error handling: an exception
185handler can find out what happened and continue execution at an outer
186level, but it cannot repair the cause of the error and retry the
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000187failing operation (except by re-entering the offending piece of
Fred Drakef6669171998-05-06 19:52:49 +0000188code from the top).
189
190When an exception is not handled at all, the interpreter terminates
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000191execution of the program, or returns to its interactive main loop. In
192either case, it prints a stack backtrace, except when the exception is
193\exception{SystemExit}.\ttindex{SystemExit}
Fred Drakef6669171998-05-06 19:52:49 +0000194
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000195Exceptions are identified by string objects or class instances.
196Selection of a matching except clause is based on object identity
197(i.e., two different string objects with the same value represent
198different exceptions!) For string exceptions, the \keyword{except}
199clause must reference the same string object. For class exceptions,
200the \keyword{except} clause must reference the same class or a base
201class of it.
Fred Drakef6669171998-05-06 19:52:49 +0000202
203When an exception is raised, an object (maybe \code{None}) is passed
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000204as the exception's ``parameter'' or ``value''; this object does not
205affect the selection of an exception handler, but is passed to the
206selected exception handler as additional information. For class
207exceptions, this object must be an instance of the exception class
208being raised.
Fred Drakef6669171998-05-06 19:52:49 +0000209
210See also the description of the \keyword{try} and \keyword{raise}
Guido van Rossumb18a93b1998-07-23 19:36:00 +0000211statements in chapter 7.