Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Doc / ref / ref4.tex
blob8df8fc2d4d47593d6b2e703671e4abe8ef7a9e94
1 \chapter{Execution model\label{execmodel}}
2 \index{execution model}
4 \section{Code blocks, execution frames, and namespaces\label{execframes}}
5 \index{code block}
6 \indexii{execution}{frame}
7 \index{namespace}
9 A \dfn{code block} is a piece of Python program text that can be
10 executed as a unit, such as a module, a class definition or a function
11 body. Some code blocks (like modules) are normally executed only once, others
12 (like function bodies) may be executed many times. Code blocks may
13 textually contain other code blocks. Code blocks may invoke other
14 code blocks (that may or may not be textually contained in them) as
15 part of their execution, e.g., by invoking (calling) a function.
16 \index{code block}
17 \indexii{code}{block}
19 The following are code blocks: A module is a code block. A function
20 body is a code block. A class definition is a code block. Each
21 command typed interactively is a separate code block; a script file (a
22 file given as standard input to the interpreter or specified on the
23 interpreter command line the first argument) is a code block; a script
24 command (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
26 function \function{execfile()} is a code block. The string argument
27 passed to the built-in function \function{eval()} and to the
28 \keyword{exec} statement is a code block. And finally, the expression
29 read and evaluated by the built-in function \function{input()} is a
30 code block.
32 A code block is executed in an execution frame. An \dfn{execution
33 frame} contains some administrative information (used for debugging),
34 determines where and how execution continues after the code block's
35 execution has completed, and (perhaps most importantly) defines two
36 namespaces, the local and the global namespace, that affect
37 execution of the code block.
38 \indexii{execution}{frame}
40 A \dfn{namespace} is a mapping from names (identifiers) to objects.
41 A particular namespace may be referenced by more than one execution
42 frame, and from other places as well. Adding a name to a namespace
43 is called \dfn{binding} a name (to an object); changing the mapping of
44 a name is called \dfn{rebinding}; removing a name is \dfn{unbinding}.
45 Namespaces are functionally equivalent to dictionaries (and often
46 implemented as dictionaries).
47 \index{namespace}
48 \indexii{binding}{name}
49 \indexii{rebinding}{name}
50 \indexii{unbinding}{name}
52 The \dfn{local namespace} of an execution frame determines the default
53 place where names are defined and searched. The \dfn{global
54 namespace} determines the place where names listed in \keyword{global}
55 statements are defined and searched, and where names that are not
56 bound anywhere in the current code block are searched.
57 \indexii{local}{namespace}
58 \indexii{global}{namespace}
59 \stindex{global}
61 Whether a name is local or global in a code block is determined by
62 static inspection of the source text for the code block: in the
63 absence of \keyword{global} statements, a name that is bound anywhere
64 in the code block is local in the entire code block; all other names
65 are considered global. The \keyword{global} statement forces global
66 interpretation of selected names throughout the code block. The
67 following constructs bind names: formal parameters to functions,
68 \keyword{import} statements, class and function definitions (these
69 bind the class or function name in the defining block), and targets
70 that are identifiers if occurring in an assignment, \keyword{for} loop
71 header, or in the second position of an \keyword{except} clause
72 header. Local names are searched only on the local namespace; global
73 names are searched only in the global and built-in
74 namespace.\footnote{
75 If the code block contains \keyword{exec} statements or the construct
76 ``\samp{from \ldots import *}'', the semantics of local names change:
77 local name lookup first searches the local namespace, then the global
78 namespace and the built-in namespace.}
80 A target occurring in a \keyword{del} statement is also considered bound
81 for this purpose (though the actual semantics are to ``unbind'' the
82 name).
84 When a global name is not found in the global namespace, it is
85 searched in the built-in namespace (which is actually the global
86 namespace of the module \module{__builtin__}). The built-in namespace
87 associated with the execution of a code block is actually found by
88 looking up the name \code{__builtins__} is its global namespace; this
89 should be a dictionary or a module (in the latter case its dictionary
90 is used). Normally, the \code{__builtins__} namespace is the
91 dictionary of the built-in module \module{__builtin__} (note: no `s');
92 if it isn't, restricted execution mode is in effect. When a name is
93 not found at all, a \exception{NameError} exception is raised.%
94 \refbimodindex{__builtin__}
95 \stindex{from}
96 \stindex{exec}
97 \stindex{global}
98 \indexii{restricted}{execution}
99 \withsubitem{(built-in exception)}{\ttindex{NameError}}
101 The following table lists the meaning of the local and global
102 namespace for various types of code blocks. The namespace for a
103 particular module is automatically created when the module is first
104 imported (i.e., when it is loaded). Note that in almost all cases,
105 the global namespace is the namespace of the containing module ---
106 scopes in Python do not nest!
108 \begin{tableiv}{l|l|l|l}{textrm}
109 {Code block type}{Global namespace}{Local namespace}{Notes}
110 \lineiv{Module}
111 {n.s. for this module}
112 {same as global}{}
113 \lineiv{Script (file or command)}
114 {n.s. for \module{__main__}}
115 {same as global}{(1)}
116 \lineiv{Interactive command}
117 {n.s. for \module{__main__}}
118 {same as global}{}
119 \lineiv{Class definition}
120 {global n.s. of containing block}
121 {new n.s.}{}
122 \lineiv{Function body}
123 {global n.s. of containing block}
124 {new n.s.}{(2)}
125 \lineiv{String passed to \keyword{exec} statement}
126 {global n.s. of containing block}
127 {local n.s. of containing block}{(2), (3)}
128 \lineiv{String passed to \function{eval()}}
129 {global n.s. of caller}
130 {local n.s. of caller}{(2), (3)}
131 \lineiv{File read by \function{execfile()}}
132 {global n.s. of caller}
133 {local n.s. of caller}{(2), (3)}
134 \lineiv{Expression read by \function{input()}}
135 {global n.s. of caller}
136 {local n.s. of caller}{}
137 \end{tableiv}
138 \refbimodindex{__main__}
140 Notes:
142 \begin{description}
144 \item[n.s.] means \emph{namespace}
146 \item[(1)] The main module for a script is always called
147 \module{__main__}; ``the filename don't enter into it.''
149 \item[(2)] The global and local namespace for these can be
150 overridden with optional extra arguments.
152 \item[(3)] The \keyword{exec} statement and the \function{eval()} and
153 \function{execfile()} functions have optional arguments to override
154 the global and local namespace. If only one namespace is specified,
155 it is used for both.
157 \end{description}
159 The built-in functions \function{globals()} and \function{locals()} returns a
160 dictionary representing the current global and local namespace,
161 respectively. The effect of modifications to this dictionary on the
162 namespace are undefined.\footnote{
163 The current implementations return the dictionary actually used to
164 implement the namespace, \emph{except} for functions, where the
165 optimizer may cause the local namespace to be implemented differently,
166 and \function{locals()} returns a read-only dictionary.}
168 \section{Exceptions\label{exceptions}}
170 Exceptions are a means of breaking out of the normal flow of control
171 of a code block in order to handle errors or other exceptional
172 conditions. An exception is \emph{raised} at the point where the error
173 is detected; it may be \emph{handled} by the surrounding code block or
174 by any code block that directly or indirectly invoked the code block
175 where 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}
183 The Python interpreter raises an exception when it detects a run-time
184 error (such as division by zero). A Python program can also
185 explicitly raise an exception with the \keyword{raise} statement.
186 Exception handlers are specified with the \keyword{try} ... \keyword{except}
187 statement. The \keyword{try} ... \keyword{finally} statement
188 specifies cleanup code which does not handle the exception, but is
189 executed whether an exception occurred or not in the preceding code.
191 Python uses the ``termination'' model of error handling: an exception
192 handler can find out what happened and continue execution at an outer
193 level, but it cannot repair the cause of the error and retry the
194 failing operation (except by re-entering the offending piece of
195 code from the top).
197 When an exception is not handled at all, the interpreter terminates
198 execution of the program, or returns to its interactive main loop. In
199 either case, it prints a stack backtrace, except when the exception is
200 \exception{SystemExit}.\ttindex{SystemExit}
202 Exceptions are identified by string objects or class instances.
203 Selection of a matching except clause is based on object identity
204 (i.e., two different string objects with the same value represent
205 different exceptions!) For string exceptions, the \keyword{except}
206 clause must reference the same string object. For class exceptions,
207 the \keyword{except} clause must reference the same class or a base
208 class of it.
210 When an exception is raised, an object (maybe \code{None}) is passed
211 as the exception's ``parameter'' or ``value''; this object does not
212 affect the selection of an exception handler, but is passed to the
213 selected exception handler as additional information. For class
214 exceptions, this object must be an instance of the exception class
215 being raised.
217 See also the description of the \keyword{try} and \keyword{raise}
218 statements in chapter 7.