Use py_resource module
[python/dscho.git] / Doc / ref / ref4.tex
blob429302a9070e2ad8ac11e018e048f56cfbecd837
1 \chapter{Execution model}
2 \index{execution model}
4 \section{Code blocks, execution frames, and name spaces} \label{execframes}
5 \index{code block}
6 \indexii{execution}{frame}
7 \index{name space}
9 A {\em 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 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 is
22 a code block. The string argument passed to the built-in function
23 \verb@eval@ and to the \verb@exec@ statement are code blocks.
24 And finally, the
25 expression read and evaluated by the built-in function \verb@input@ is
26 a code block.
28 A code block is executed in an execution frame. An {\em execution
29 frame} contains some administrative information (used for debugging),
30 determines where and how execution continues after the code block's
31 execution has completed, and (perhaps most importantly) defines two
32 name spaces, the local and the global name space, that affect
33 execution of the code block.
34 \indexii{execution}{frame}
36 A {\em name space} is a mapping from names (identifiers) to objects.
37 A particular name space may be referenced by more than one execution
38 frame, and from other places as well. Adding a name to a name space
39 is called {\em binding} a name (to an object); changing the mapping of
40 a name is called {\em rebinding}; removing a name is {\em unbinding}.
41 Name spaces are functionally equivalent to dictionaries.
42 \index{name space}
43 \indexii{binding}{name}
44 \indexii{rebinding}{name}
45 \indexii{unbinding}{name}
47 The {\em local name space} of an execution frame determines the default
48 place where names are defined and searched. The {\em global name
49 space} determines the place where names listed in \verb@global@
50 statements are defined and searched, and where names that are not
51 explicitly bound in the current code block are searched.
52 \indexii{local}{name space}
53 \indexii{global}{name space}
54 \stindex{global}
56 Whether a name is local or global in a code block is determined by
57 static inspection of the source text for the code block: in the
58 absence of \verb@global@ statements, a name that is bound anywhere in
59 the code block is local in the entire code block; all other names are
60 considered global. The \verb@global@ statement forces global
61 interpretation of selected names throughout the code block. The
62 following constructs bind names: formal parameters, \verb@import@
63 statements, class and function definitions (these bind the class or
64 function name), and targets that are identifiers if occurring in an
65 assignment, \verb@for@ loop header, or \verb@except@ clause header.
67 A target occurring in a \verb@del@ statement is also considered bound
68 for this purpose (though the actual semantics are to ``unbind'' the
69 name).
71 When a global name is not found in the global name space, it is
72 searched in the list of ``built-in'' names (which is actually the
73 global name space of the module \verb@__builtin__@). When a name is not
74 found at all, the \verb@NameError@ exception is raised.%
75 \footnote{If the code block contains {\tt exec} statements or the
76 construct {\tt from \ldots import *}, the semantics of names not
77 explicitly mentioned in a {\tt global} statement change subtly: name
78 lookup first searches the local name space, then the global one, then
79 the built-in one.}
80 \bimodindex{__builtin__}
81 \stindex{from}
82 \stindex{exec}
83 \stindex{global}
84 \ttindex{NameError}
86 The following table lists the meaning of the local and global name
87 space for various types of code blocks. The name space for a
88 particular module is automatically created when the module is first
89 referenced. Note that in almost all cases, the global name space is
90 the name space of the containing module --- scopes in Python do not
91 nest!
93 \begin{center}
94 \begin{tabular}{|l|l|l|l|}
95 \hline
96 Code block type & Global name space & Local name space & Notes \\
97 \hline
98 Module & n.s. for this module & same as global & \\
99 Script & n.s. for \verb@__main__@ & same as global & \\
100 Interactive command & n.s. for \verb@__main__@ & same as global & \\
101 Class definition & global n.s. of containing block & new n.s. & \\
102 Function body & global n.s. of containing block & new n.s. & \\
103 String passed to \verb@exec@ statement
104 & global n.s. of cobtaining block
105 & local n.s. of containing block & (1) \\
106 String passed to \verb@eval()@
107 & global n.s. of caller & local n.s. of caller & (1) \\
108 File read by \verb@execfile()@
109 & global n.s. of caller & local n.s. of caller & (1) \\
110 Expression read by \verb@input@
111 & global n.s. of caller & local n.s. of caller & \\
112 \hline
113 \end{tabular}
114 \end{center}
115 \bimodindex{__main__}
117 Notes:
119 \begin{description}
121 \item[n.s.] means {\em name space}
123 \item[(1)] The global and local name space for these can be
124 overridden with optional extra arguments.
126 \end{description}
128 The built-in functions \verb@globals()@ and \verb@locals()@ returns a
129 dictionary representing the current global and local name space,
130 respectively. The effect of modifications to this dictionary on the
131 name space are undefined.%
132 \footnote{The current implementations return the dictionary actually
133 used to implement the name space, {\em except} for functions, where
134 the optimizer may cause the local name space to be implemented
135 differently, and \verb@locals()@ returns a read-only dictionary.}
137 \section{Exceptions}
139 Exceptions are a means of breaking out of the normal flow of control
140 of a code block in order to handle errors or other exceptional
141 conditions. An exception is {\em raised} at the point where the error
142 is detected; it may be {\em handled} by the surrounding code block or
143 by any code block that directly or indirectly invoked the code block
144 where 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}
152 The Python interpreter raises an exception when it detects an run-time
153 error (such as division by zero). A Python program can also
154 explicitly raise an exception with the \verb@raise@ statement.
155 Exception handlers are specified with the \verb@try...except@
156 statement.
158 Python uses the ``termination'' model of error handling: an exception
159 handler can find out what happened and continue execution at an outer
160 level, but it cannot repair the cause of the error and retry the
161 failing operation (except by re-entering the the offending piece of
162 code from the top).
164 When an exception is not handled at all, the interpreter terminates
165 execution of the program, or returns to its interactive main loop.
167 Exceptions are identified by string objects. Two different string
168 objects with the same value identify different exceptions.
170 When an exception is raised, an object (maybe \verb@None@) is passed
171 as the exception's ``parameter''; this object does not affect the
172 selection of an exception handler, but is passed to the selected
173 exception handler as additional information.
175 See also the description of the \verb@try@ and \verb@raise@
176 statements.