blob: 8eee5b8167db6c0657f790255cd89e6e1e22f6f3 [file] [log] [blame]
Fred Drake011f6fc1999-04-14 12:52:14 +00001\chapter{Simple statements \label{simple}}
Fred Drakef6669171998-05-06 19:52:49 +00002\indexii{simple}{statement}
3
4Simple statements are comprised within a single logical line.
5Several simple statements may occur on a single line separated
6by semicolons. The syntax for simple statements is:
7
Fred Drakecb4638a2001-07-06 22:49:53 +00008\begin{productionlist}
Fred Drake53815882002-03-15 23:21:37 +00009 \production{simple_stmt}{\token{expression_stmt}}
10 \productioncont{| \token{assert_stmt}}
11 \productioncont{| \token{assignment_stmt}}
12 \productioncont{| \token{augmented_assignment_stmt}}
13 \productioncont{| \token{pass_stmt}}
14 \productioncont{| \token{del_stmt}}
15 \productioncont{| \token{print_stmt}}
16 \productioncont{| \token{return_stmt}}
17 \productioncont{| \token{yield_stmt}}
18 \productioncont{| \token{raise_stmt}}
19 \productioncont{| \token{break_stmt}}
20 \productioncont{| \token{continue_stmt}}
21 \productioncont{| \token{import_stmt}}
22 \productioncont{| \token{global_stmt}}
23 \productioncont{| \token{exec_stmt}}
Fred Drakecb4638a2001-07-06 22:49:53 +000024\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000025
Fred Drake2829f1c2001-06-23 05:27:20 +000026
Fred Drake011f6fc1999-04-14 12:52:14 +000027\section{Expression statements \label{exprstmts}}
Fred Drakef6669171998-05-06 19:52:49 +000028\indexii{expression}{statement}
29
30Expression statements are used (mostly interactively) to compute and
31write a value, or (usually) to call a procedure (a function that
32returns no meaningful result; in Python, procedures return the value
Guido van Rossum56c20131998-07-24 18:25:38 +000033\code{None}). Other uses of expression statements are allowed and
34occasionally useful. The syntax for an expression statement is:
Fred Drakef6669171998-05-06 19:52:49 +000035
Fred Drakecb4638a2001-07-06 22:49:53 +000036\begin{productionlist}
37 \production{expression_stmt}
38 {\token{expression_list}}
39\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +000040
Guido van Rossum56c20131998-07-24 18:25:38 +000041An expression statement evaluates the expression list (which may be a
42single expression).
Fred Drakef6669171998-05-06 19:52:49 +000043\indexii{expression}{list}
44
45In interactive mode, if the value is not \code{None}, it is converted
Guido van Rossum56c20131998-07-24 18:25:38 +000046to a string using the built-in \function{repr()}\bifuncindex{repr}
47function and the resulting string is written to standard output (see
Fred Drakec2f496a2001-12-05 05:46:25 +000048section~\ref{print}) on a line by itself. (Expression statements
49yielding \code{None} are not written, so that procedure calls do not
50cause any output.)
Fred Drake7a700b82004-01-01 05:43:53 +000051\obindex{None}
Fred Drakef6669171998-05-06 19:52:49 +000052\indexii{string}{conversion}
53\index{output}
54\indexii{standard}{output}
55\indexii{writing}{values}
56\indexii{procedure}{call}
57
Fred Drake2829f1c2001-06-23 05:27:20 +000058
Fred Drake011f6fc1999-04-14 12:52:14 +000059\section{Assert statements \label{assert}}
Guido van Rossum56c20131998-07-24 18:25:38 +000060
Fred Drake011f6fc1999-04-14 12:52:14 +000061Assert statements\stindex{assert} are a convenient way to insert
62debugging assertions\indexii{debugging}{assertions} into a program:
Guido van Rossum56c20131998-07-24 18:25:38 +000063
Fred Drakecb4638a2001-07-06 22:49:53 +000064\begin{productionlist}
Fred Drake007fadd2003-03-31 14:53:03 +000065 \production{assert_stmt}
Fred Drakecb4638a2001-07-06 22:49:53 +000066 {"assert" \token{expression} ["," \token{expression}]}
67\end{productionlist}
Guido van Rossum56c20131998-07-24 18:25:38 +000068
Fred Drake011f6fc1999-04-14 12:52:14 +000069The simple form, \samp{assert expression}, is equivalent to
Guido van Rossum56c20131998-07-24 18:25:38 +000070
71\begin{verbatim}
72if __debug__:
73 if not expression: raise AssertionError
74\end{verbatim}
75
Fred Drake011f6fc1999-04-14 12:52:14 +000076The extended form, \samp{assert expression1, expression2}, is
Guido van Rossum56c20131998-07-24 18:25:38 +000077equivalent to
78
79\begin{verbatim}
80if __debug__:
81 if not expression1: raise AssertionError, expression2
82\end{verbatim}
83
84These equivalences assume that \code{__debug__}\ttindex{__debug__} and
Fred Drake011f6fc1999-04-14 12:52:14 +000085\exception{AssertionError}\exindex{AssertionError} refer to the built-in
Guido van Rossum56c20131998-07-24 18:25:38 +000086variables with those names. In the current implementation, the
Johannes Gijsbersf4a70f32004-12-12 16:52:40 +000087built-in variable \code{__debug__} is \code{True} under normal
88circumstances, \code{False} when optimization is requested (command line
89option -O). The current code generator emits no code for an assert
90statement when optimization is requested at compile time. Note that it
91is unnecessary to include the source code for the expression that failed
92in the error message;
Guido van Rossum56c20131998-07-24 18:25:38 +000093it will be displayed as part of the stack trace.
94
Jeremy Hylton2c84fc82001-03-23 14:34:06 +000095Assignments to \code{__debug__} are illegal. The value for the
96built-in variable is determined when the interpreter starts.
Guido van Rossum56c20131998-07-24 18:25:38 +000097
Fred Drake2829f1c2001-06-23 05:27:20 +000098
Fred Drake011f6fc1999-04-14 12:52:14 +000099\section{Assignment statements \label{assignment}}
Fred Drakef6669171998-05-06 19:52:49 +0000100
Fred Drake011f6fc1999-04-14 12:52:14 +0000101Assignment statements\indexii{assignment}{statement} are used to
102(re)bind names to values and to modify attributes or items of mutable
103objects:
Fred Drakef6669171998-05-06 19:52:49 +0000104\indexii{binding}{name}
105\indexii{rebinding}{name}
106\obindex{mutable}
107\indexii{attribute}{assignment}
108
Fred Drakecb4638a2001-07-06 22:49:53 +0000109\begin{productionlist}
110 \production{assignment_stmt}
111 {(\token{target_list} "=")+ \token{expression_list}}
112 \production{target_list}
113 {\token{target} ("," \token{target})* [","]}
114 \production{target}
Fred Drake53815882002-03-15 23:21:37 +0000115 {\token{identifier}}
116 \productioncont{| "(" \token{target_list} ")"}
117 \productioncont{| "[" \token{target_list} "]"}
118 \productioncont{| \token{attributeref}}
119 \productioncont{| \token{subscription}}
120 \productioncont{| \token{slicing}}
Fred Drakecb4638a2001-07-06 22:49:53 +0000121\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000122
Fred Drakec2f496a2001-12-05 05:46:25 +0000123(See section~\ref{primaries} for the syntax definitions for the last
Fred Drakef6669171998-05-06 19:52:49 +0000124three symbols.)
125
126An assignment statement evaluates the expression list (remember that
127this can be a single expression or a comma-separated list, the latter
128yielding a tuple) and assigns the single resulting object to each of
129the target lists, from left to right.
130\indexii{expression}{list}
131
132Assignment is defined recursively depending on the form of the target
133(list). When a target is part of a mutable object (an attribute
134reference, subscription or slicing), the mutable object must
135ultimately perform the assignment and decide about its validity, and
136may raise an exception if the assignment is unacceptable. The rules
137observed by various types and the exceptions raised are given with the
Fred Drakec2f496a2001-12-05 05:46:25 +0000138definition of the object types (see section~\ref{types}).
Fred Drakef6669171998-05-06 19:52:49 +0000139\index{target}
140\indexii{target}{list}
141
142Assignment of an object to a target list is recursively defined as
143follows.
144\indexiii{target}{list}{assignment}
145
146\begin{itemize}
147\item
Guido van Rossum56c20131998-07-24 18:25:38 +0000148If the target list is a single target: The object is assigned to that
Fred Drakef6669171998-05-06 19:52:49 +0000149target.
150
151\item
Guido van Rossum56c20131998-07-24 18:25:38 +0000152If the target list is a comma-separated list of targets: The object
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000153must be a sequence with the same number of items as there are
Guido van Rossum56c20131998-07-24 18:25:38 +0000154targets in the target list, and the items are assigned, from left to
155right, to the corresponding targets. (This rule is relaxed as of
156Python 1.5; in earlier versions, the object had to be a tuple. Since
Fred Drake011f6fc1999-04-14 12:52:14 +0000157strings are sequences, an assignment like \samp{a, b = "xy"} is
Guido van Rossum56c20131998-07-24 18:25:38 +0000158now legal as long as the string has the right length.)
Fred Drakef6669171998-05-06 19:52:49 +0000159
160\end{itemize}
161
162Assignment of an object to a single target is recursively defined as
163follows.
164
165\begin{itemize} % nested
166
167\item
168If the target is an identifier (name):
169
170\begin{itemize}
171
172\item
173If the name does not occur in a \keyword{global} statement in the current
Guido van Rossum56c20131998-07-24 18:25:38 +0000174code block: the name is bound to the object in the current local
175namespace.
Fred Drakef6669171998-05-06 19:52:49 +0000176\stindex{global}
177
178\item
Guido van Rossum56c20131998-07-24 18:25:38 +0000179Otherwise: the name is bound to the object in the current global
180namespace.
Fred Drakef6669171998-05-06 19:52:49 +0000181
182\end{itemize} % nested
183
Guido van Rossum56c20131998-07-24 18:25:38 +0000184The name is rebound if it was already bound. This may cause the
185reference count for the object previously bound to the name to reach
186zero, causing the object to be deallocated and its
187destructor\index{destructor} (if it has one) to be called.
Fred Drakef6669171998-05-06 19:52:49 +0000188
189\item
Guido van Rossum56c20131998-07-24 18:25:38 +0000190If the target is a target list enclosed in parentheses or in square
191brackets: The object must be a sequence with the same number of items
192as there are targets in the target list, and its items are assigned,
193from left to right, to the corresponding targets.
Fred Drakef6669171998-05-06 19:52:49 +0000194
195\item
196If the target is an attribute reference: The primary expression in the
197reference is evaluated. It should yield an object with assignable
198attributes; if this is not the case, \exception{TypeError} is raised. That
199object is then asked to assign the assigned object to the given
200attribute; if it cannot perform the assignment, it raises an exception
201(usually but not necessarily \exception{AttributeError}).
202\indexii{attribute}{assignment}
203
204\item
205If the target is a subscription: The primary expression in the
206reference is evaluated. It should yield either a mutable sequence
Guido van Rossum56c20131998-07-24 18:25:38 +0000207object (e.g., a list) or a mapping object (e.g., a dictionary). Next,
208the subscript expression is evaluated.
Fred Drakef6669171998-05-06 19:52:49 +0000209\indexii{subscription}{assignment}
210\obindex{mutable}
211
Guido van Rossum56c20131998-07-24 18:25:38 +0000212If the primary is a mutable sequence object (e.g., a list), the subscript
Fred Drakef6669171998-05-06 19:52:49 +0000213must yield a plain integer. If it is negative, the sequence's length
214is added to it. The resulting value must be a nonnegative integer
215less than the sequence's length, and the sequence is asked to assign
216the assigned object to its item with that index. If the index is out
217of range, \exception{IndexError} is raised (assignment to a subscripted
218sequence cannot add new items to a list).
219\obindex{sequence}
220\obindex{list}
221
Guido van Rossum56c20131998-07-24 18:25:38 +0000222If the primary is a mapping object (e.g., a dictionary), the subscript must
Fred Drakef6669171998-05-06 19:52:49 +0000223have a type compatible with the mapping's key type, and the mapping is
224then asked to create a key/datum pair which maps the subscript to
225the assigned object. This can either replace an existing key/value
226pair with the same key value, or insert a new key/value pair (if no
227key with the same value existed).
228\obindex{mapping}
229\obindex{dictionary}
230
231\item
232If the target is a slicing: The primary expression in the reference is
Guido van Rossum56c20131998-07-24 18:25:38 +0000233evaluated. It should yield a mutable sequence object (e.g., a list). The
Fred Drakef6669171998-05-06 19:52:49 +0000234assigned object should be a sequence object of the same type. Next,
235the lower and upper bound expressions are evaluated, insofar they are
236present; defaults are zero and the sequence's length. The bounds
237should evaluate to (small) integers. If either bound is negative, the
238sequence's length is added to it. The resulting bounds are clipped to
239lie between zero and the sequence's length, inclusive. Finally, the
240sequence object is asked to replace the slice with the items of the
241assigned sequence. The length of the slice may be different from the
242length of the assigned sequence, thus changing the length of the
243target sequence, if the object allows it.
244\indexii{slicing}{assignment}
245
246\end{itemize}
Greg Ward38c28e32000-04-27 18:32:02 +0000247
Fred Drakef6669171998-05-06 19:52:49 +0000248(In the current implementation, the syntax for targets is taken
249to be the same as for expressions, and invalid syntax is rejected
250during the code generation phase, causing less detailed error
251messages.)
252
253WARNING: Although the definition of assignment implies that overlaps
Guido van Rossum56c20131998-07-24 18:25:38 +0000254between the left-hand side and the right-hand side are `safe' (e.g.,
Fred Drake011f6fc1999-04-14 12:52:14 +0000255\samp{a, b = b, a} swaps two variables), overlaps \emph{within} the
Fred Drakef6669171998-05-06 19:52:49 +0000256collection of assigned-to variables are not safe! For instance, the
Fred Drake011f6fc1999-04-14 12:52:14 +0000257following program prints \samp{[0, 2]}:
Fred Drakef6669171998-05-06 19:52:49 +0000258
259\begin{verbatim}
260x = [0, 1]
261i = 0
262i, x[i] = 1, 2
263print x
264\end{verbatim}
265
266
Fred Drake53815882002-03-15 23:21:37 +0000267\subsection{Augmented assignment statements \label{augassign}}
Fred Drake31f55502000-09-12 20:32:18 +0000268
269Augmented assignment is the combination, in a single statement, of a binary
270operation and an assignment statement:
271\indexii{augmented}{assignment}
272\index{statement!assignment, augmented}
273
Fred Drakecb4638a2001-07-06 22:49:53 +0000274\begin{productionlist}
275 \production{augmented_assignment_stmt}
276 {\token{target} \token{augop} \token{expression_list}}
277 \production{augop}
Fred Drake53815882002-03-15 23:21:37 +0000278 {"+=" | "-=" | "*=" | "/=" | "\%=" | "**="}
Fred Drake2269d862004-11-11 06:14:05 +0000279 % The empty groups below prevent conversion to guillemets.
280 \productioncont{| ">{}>=" | "<{}<=" | "\&=" | "\textasciicircum=" | "|="}
Fred Drakecb4638a2001-07-06 22:49:53 +0000281\end{productionlist}
Fred Drake31f55502000-09-12 20:32:18 +0000282
Fred Drakec2f496a2001-12-05 05:46:25 +0000283(See section~\ref{primaries} for the syntax definitions for the last
Fred Drake31f55502000-09-12 20:32:18 +0000284three symbols.)
285
Fred Draked68442b2000-09-21 22:01:36 +0000286An augmented assignment evaluates the target (which, unlike normal
287assignment statements, cannot be an unpacking) and the expression
288list, performs the binary operation specific to the type of assignment
289on the two operands, and assigns the result to the original
290target. The target is only evaluated once.
Fred Drake31f55502000-09-12 20:32:18 +0000291
292An augmented assignment expression like \code{x += 1} can be rewritten as
293\code{x = x + 1} to achieve a similar, but not exactly equal effect. In the
294augmented version, \code{x} is only evaluated once. Also, when possible, the
295actual operation is performed \emph{in-place}, meaning that rather than
296creating a new object and assigning that to the target, the old object is
297modified instead.
298
299With the exception of assigning to tuples and multiple targets in a single
300statement, the assignment done by augmented assignment statements is handled
301the same way as normal assignments. Similarly, with the exception of the
Fred Drakec2f496a2001-12-05 05:46:25 +0000302possible \emph{in-place} behavior, the binary operation performed by
Fred Drake31f55502000-09-12 20:32:18 +0000303augmented assignment is the same as the normal binary operations.
304
Raymond Hettinger04e7e0c2002-06-25 13:36:41 +0000305For targets which are attribute references, the initial value is
306retrieved with a \method{getattr()} and the result is assigned with a
307\method{setattr()}. Notice that the two methods do not necessarily
308refer to the same variable. When \method{getattr()} refers to a class
309variable, \method{setattr()} still writes to an instance variable.
310For example:
311
312\begin{verbatim}
313class A:
314 x = 3 # class variable
315a = A()
316a.x += 1 # writes a.x as 4 leaving A.x as 3
317\end{verbatim}
318
Fred Drake31f55502000-09-12 20:32:18 +0000319
Fred Drake011f6fc1999-04-14 12:52:14 +0000320\section{The \keyword{pass} statement \label{pass}}
Fred Drakef6669171998-05-06 19:52:49 +0000321\stindex{pass}
322
Fred Drakecb4638a2001-07-06 22:49:53 +0000323\begin{productionlist}
324 \production{pass_stmt}
325 {"pass"}
326\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000327
328\keyword{pass} is a null operation --- when it is executed, nothing
329happens. It is useful as a placeholder when a statement is
330required syntactically, but no code needs to be executed, for example:
331\indexii{null}{operation}
332
333\begin{verbatim}
334def f(arg): pass # a function that does nothing (yet)
335
336class C: pass # a class with no methods (yet)
337\end{verbatim}
338
Fred Drake2829f1c2001-06-23 05:27:20 +0000339
Fred Drake011f6fc1999-04-14 12:52:14 +0000340\section{The \keyword{del} statement \label{del}}
Fred Drakef6669171998-05-06 19:52:49 +0000341\stindex{del}
342
Fred Drakecb4638a2001-07-06 22:49:53 +0000343\begin{productionlist}
344 \production{del_stmt}
345 {"del" \token{target_list}}
346\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000347
348Deletion is recursively defined very similar to the way assignment is
349defined. Rather that spelling it out in full details, here are some
350hints.
351\indexii{deletion}{target}
352\indexiii{deletion}{target}{list}
353
354Deletion of a target list recursively deletes each target, from left
355to right.
356
Jeremy Hyltond09ed682002-04-01 21:15:14 +0000357Deletion of a name removes the binding of that name
Guido van Rossum56c20131998-07-24 18:25:38 +0000358from the local or global namespace, depending on whether the name
Jeremy Hyltond09ed682002-04-01 21:15:14 +0000359occurs in a \keyword{global} statement in the same code block. If the
360name is unbound, a \exception{NameError} exception will be raised.
Fred Drakef6669171998-05-06 19:52:49 +0000361\stindex{global}
362\indexii{unbinding}{name}
363
Jeremy Hyltond09ed682002-04-01 21:15:14 +0000364It is illegal to delete a name from the local namespace if it occurs
Michael W. Hudson495afea2002-06-17 12:51:57 +0000365as a free variable\indexii{free}{variable} in a nested block.
Jeremy Hyltond09ed682002-04-01 21:15:14 +0000366
Fred Drakef6669171998-05-06 19:52:49 +0000367Deletion of attribute references, subscriptions and slicings
368is passed to the primary object involved; deletion of a slicing
369is in general equivalent to assignment of an empty slice of the
370right type (but even this is determined by the sliced object).
371\indexii{attribute}{deletion}
372
Fred Drake2829f1c2001-06-23 05:27:20 +0000373
Fred Drake011f6fc1999-04-14 12:52:14 +0000374\section{The \keyword{print} statement \label{print}}
Fred Drakef6669171998-05-06 19:52:49 +0000375\stindex{print}
376
Fred Drakecb4638a2001-07-06 22:49:53 +0000377\begin{productionlist}
378 \production{print_stmt}
Fred Drake53815882002-03-15 23:21:37 +0000379 {"print" ( \optional{\token{expression} ("," \token{expression})* \optional{","}}}
380 \productioncont{| ">\code{>}" \token{expression}
381 \optional{("," \token{expression})+ \optional{","}} )}
Fred Drakecb4638a2001-07-06 22:49:53 +0000382\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000383
Fred Draked4c33521998-10-01 20:39:47 +0000384\keyword{print} evaluates each expression in turn and writes the
385resulting object to standard output (see below). If an object is not
Fred Drakebe9d10e2001-06-23 06:16:52 +0000386a string, it is first converted to a string using the rules for string
Fred Drakef6669171998-05-06 19:52:49 +0000387conversions. The (resulting or original) string is then written. A
Fred Drakebe9d10e2001-06-23 06:16:52 +0000388space is written before each object is (converted and) written, unless
Fred Drakef6669171998-05-06 19:52:49 +0000389the output system believes it is positioned at the beginning of a
Guido van Rossum56c20131998-07-24 18:25:38 +0000390line. This is the case (1) when no characters have yet been written
391to standard output, (2) when the last character written to standard
Fred Draked4c33521998-10-01 20:39:47 +0000392output is \character{\e n}, or (3) when the last write operation on
393standard output was not a \keyword{print} statement. (In some cases
394it may be functional to write an empty string to standard output for
Fred Drakec2f496a2001-12-05 05:46:25 +0000395this reason.) \note{Objects which act like file objects but which are
396not the built-in file objects often do not properly emulate this
397aspect of the file object's behavior, so it is best not to rely on
398this.}
Fred Drakef6669171998-05-06 19:52:49 +0000399\index{output}
400\indexii{writing}{values}
401
Fred Draked4c33521998-10-01 20:39:47 +0000402A \character{\e n} character is written at the end, unless the
403\keyword{print} statement ends with a comma. This is the only action
404if the statement contains just the keyword \keyword{print}.
Fred Drakef6669171998-05-06 19:52:49 +0000405\indexii{trailing}{comma}
406\indexii{newline}{suppression}
407
Fred Drakedde91f01998-05-06 20:59:46 +0000408Standard output is defined as the file object named \code{stdout}
Guido van Rossum56c20131998-07-24 18:25:38 +0000409in the built-in module \module{sys}. If no such object exists, or if
410it does not have a \method{write()} method, a \exception{RuntimeError}
411exception is raised.
Fred Drakef6669171998-05-06 19:52:49 +0000412\indexii{standard}{output}
413\refbimodindex{sys}
Fred Drake2b3730e1998-11-25 17:40:00 +0000414\withsubitem{(in module sys)}{\ttindex{stdout}}
Fred Drakef6669171998-05-06 19:52:49 +0000415\exindex{RuntimeError}
416
Fred Drakecb4638a2001-07-06 22:49:53 +0000417\keyword{print} also has an extended\index{extended print statement}
418form, defined by the second portion of the syntax described above.
419This form is sometimes referred to as ``\keyword{print} chevron.''
Fred Drake62effc12001-04-13 15:55:25 +0000420In this form, the first expression after the \code{>}\code{>} must
Barry Warsaw8c0a2422000-08-21 15:45:16 +0000421evaluate to a ``file-like'' object, specifically an object that has a
Barry Warsaw33f785f2000-08-29 04:57:34 +0000422\method{write()} method as described above. With this extended form,
423the subsequent expressions are printed to this file object. If the
424first expression evaluates to \code{None}, then \code{sys.stdout} is
425used as the file for output.
Barry Warsaw8c0a2422000-08-21 15:45:16 +0000426
Fred Drake2829f1c2001-06-23 05:27:20 +0000427
Fred Drake011f6fc1999-04-14 12:52:14 +0000428\section{The \keyword{return} statement \label{return}}
Fred Drakef6669171998-05-06 19:52:49 +0000429\stindex{return}
430
Fred Drakecb4638a2001-07-06 22:49:53 +0000431\begin{productionlist}
432 \production{return_stmt}
433 {"return" [\token{expression_list}]}
434\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000435
436\keyword{return} may only occur syntactically nested in a function
437definition, not within a nested class definition.
438\indexii{function}{definition}
439\indexii{class}{definition}
440
Guido van Rossum56c20131998-07-24 18:25:38 +0000441If an expression list is present, it is evaluated, else \code{None}
Fred Drakef6669171998-05-06 19:52:49 +0000442is substituted.
443
Guido van Rossum56c20131998-07-24 18:25:38 +0000444\keyword{return} leaves the current function call with the expression
Fred Drakef6669171998-05-06 19:52:49 +0000445list (or \code{None}) as return value.
446
447When \keyword{return} passes control out of a \keyword{try} statement
Guido van Rossum56c20131998-07-24 18:25:38 +0000448with a \keyword{finally} clause, that \keyword{finally} clause is executed
Fred Drakef6669171998-05-06 19:52:49 +0000449before really leaving the function.
450\kwindex{finally}
451
Fred Drakee31e9ce2001-12-11 21:10:08 +0000452In a generator function, the \keyword{return} statement is not allowed
453to include an \grammartoken{expression_list}. In that context, a bare
454\keyword{return} indicates that the generator is done and will cause
455\exception{StopIteration} to be raised.
456
457
458\section{The \keyword{yield} statement \label{yield}}
459\stindex{yield}
460
461\begin{productionlist}
462 \production{yield_stmt}
463 {"yield" \token{expression_list}}
464\end{productionlist}
465
466\index{generator!function}
467\index{generator!iterator}
468\index{function!generator}
469\exindex{StopIteration}
470
471The \keyword{yield} statement is only used when defining a generator
472function, and is only used in the body of the generator function.
473Using a \keyword{yield} statement in a function definition is
474sufficient to cause that definition to create a generator function
475instead of a normal function.
476
477When a generator function is called, it returns an iterator known as a
478generator iterator, or more commonly, a generator. The body of the
479generator function is executed by calling the generator's
480\method{next()} method repeatedly until it raises an exception.
481
482When a \keyword{yield} statement is executed, the state of the
483generator is frozen and the value of \grammartoken{expression_list} is
484returned to \method{next()}'s caller. By ``frozen'' we mean that all
485local state is retained, including the current bindings of local
486variables, the instruction pointer, and the internal evaluation stack:
487enough information is saved so that the next time \method{next()} is
488invoked, the function can proceed exactly as if the \keyword{yield}
489statement were just another external call.
490
Fred Drake3a8e59e2001-12-11 21:58:35 +0000491The \keyword{yield} statement is not allowed in the \keyword{try}
492clause of a \keyword{try} ...\ \keyword{finally} construct. The
493difficulty is that there's no guarantee the generator will ever be
494resumed, hence no guarantee that the \keyword{finally} block will ever
495get executed.
Fred Drakee31e9ce2001-12-11 21:10:08 +0000496
Fred Drake08d752c2001-12-14 22:55:14 +0000497\begin{notice}
498In Python 2.2, the \keyword{yield} statement is only allowed
Fred Drake8d0645c2001-12-12 06:06:43 +0000499when the \code{generators} feature has been enabled. It will always
Raymond Hettinger68804312005-01-01 00:28:46 +0000500be enabled in Python 2.3. This \code{__future__} import statement can
Fred Drake08d752c2001-12-14 22:55:14 +0000501be used to enable the feature:
Fred Drake8d0645c2001-12-12 06:06:43 +0000502
503\begin{verbatim}
504from __future__ import generators
505\end{verbatim}
Fred Drake08d752c2001-12-14 22:55:14 +0000506\end{notice}
Fred Drake8d0645c2001-12-12 06:06:43 +0000507
508
Fred Drakee31e9ce2001-12-11 21:10:08 +0000509\begin{seealso}
510 \seepep{0255}{Simple Generators}
511 {The proposal for adding generators and the \keyword{yield}
512 statement to Python.}
513\end{seealso}
514
Fred Drake2829f1c2001-06-23 05:27:20 +0000515
Fred Drake011f6fc1999-04-14 12:52:14 +0000516\section{The \keyword{raise} statement \label{raise}}
Fred Drakef6669171998-05-06 19:52:49 +0000517\stindex{raise}
518
Fred Drakecb4638a2001-07-06 22:49:53 +0000519\begin{productionlist}
520 \production{raise_stmt}
521 {"raise" [\token{expression} ["," \token{expression}
522 ["," \token{expression}]]]}
523\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000524
Guido van Rossum56c20131998-07-24 18:25:38 +0000525If no expressions are present, \keyword{raise} re-raises the last
Neal Norwitz847207a2003-05-29 02:17:23 +0000526expression that was active in the current scope. If no exception is
527active in the current scope, an exception is raised indicating this error.
Fred Drakef6669171998-05-06 19:52:49 +0000528\index{exception}
529\indexii{raising}{exception}
530
Fred Drake81932e22002-06-20 20:55:29 +0000531Otherwise, \keyword{raise} evaluates the expressions to get three
532objects, using \code{None} as the value of omitted expressions. The
533first two objects are used to determine the \emph{type} and
534\emph{value} of the exception.
Fred Drakef6669171998-05-06 19:52:49 +0000535
Fred Drake81932e22002-06-20 20:55:29 +0000536If the first object is an instance, the type of the exception is the
Fred Drake8bd62af2003-01-25 03:47:35 +0000537class of the instance, the instance itself is the value, and the
Fred Drake81932e22002-06-20 20:55:29 +0000538second object must be \code{None}.
539
540If the first object is a class, it becomes the type of the exception.
541The second object is used to determine the exception value: If it is
542an instance of the class, the instance becomes the exception value.
543If the second object is a tuple, it is used as the argument list for
544the class constructor; if it is \code{None}, an empty argument list is
545used, and any other object is treated as a single argument to the
546constructor. The instance so created by calling the constructor is
547used as the exception value.
548
Fred Drake81932e22002-06-20 20:55:29 +0000549If a third object is present and not \code{None}, it must be a
550traceback\obindex{traceback} object (see section~\ref{traceback}), and
551it is substituted instead of the current location as the place where
552the exception occurred. If the third object is present and not a
553traceback object or \code{None}, a \exception{TypeError} exception is
554raised. The three-expression form of \keyword{raise} is useful to
555re-raise an exception transparently in an except clause, but
556\keyword{raise} with no expressions should be preferred if the
557exception to be re-raised was the most recently active exception in
558the current scope.
559
Fred Drakee7097e02002-10-18 15:18:18 +0000560Additional information on exceptions can be found in
561section~\ref{exceptions}, and information about handling exceptions is
562in section~\ref{try}.
Fred Drakef6669171998-05-06 19:52:49 +0000563
Fred Drake2829f1c2001-06-23 05:27:20 +0000564
Fred Drake011f6fc1999-04-14 12:52:14 +0000565\section{The \keyword{break} statement \label{break}}
Fred Drakef6669171998-05-06 19:52:49 +0000566\stindex{break}
567
Fred Drakecb4638a2001-07-06 22:49:53 +0000568\begin{productionlist}
569 \production{break_stmt}
570 {"break"}
571\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000572
573\keyword{break} may only occur syntactically nested in a \keyword{for}
574or \keyword{while} loop, but not nested in a function or class definition
575within that loop.
576\stindex{for}
577\stindex{while}
578\indexii{loop}{statement}
579
580It terminates the nearest enclosing loop, skipping the optional
Guido van Rossum56c20131998-07-24 18:25:38 +0000581\keyword{else} clause if the loop has one.
Fred Drakef6669171998-05-06 19:52:49 +0000582\kwindex{else}
583
584If a \keyword{for} loop is terminated by \keyword{break}, the loop control
585target keeps its current value.
586\indexii{loop control}{target}
587
588When \keyword{break} passes control out of a \keyword{try} statement
Guido van Rossum56c20131998-07-24 18:25:38 +0000589with a \keyword{finally} clause, that \keyword{finally} clause is executed
Fred Drakef6669171998-05-06 19:52:49 +0000590before really leaving the loop.
591\kwindex{finally}
592
Fred Drake2829f1c2001-06-23 05:27:20 +0000593
Fred Drake011f6fc1999-04-14 12:52:14 +0000594\section{The \keyword{continue} statement \label{continue}}
Fred Drakef6669171998-05-06 19:52:49 +0000595\stindex{continue}
596
Fred Drakecb4638a2001-07-06 22:49:53 +0000597\begin{productionlist}
598 \production{continue_stmt}
599 {"continue"}
600\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000601
602\keyword{continue} may only occur syntactically nested in a \keyword{for} or
603\keyword{while} loop, but not nested in a function or class definition or
Guido van Rossum56c20131998-07-24 18:25:38 +0000604\keyword{try} statement within that loop.\footnote{It may
605occur within an \keyword{except} or \keyword{else} clause. The
Thomas Woutersf9b526d2000-07-16 19:05:38 +0000606restriction on occurring in the \keyword{try} clause is implementor's
Guido van Rossum56c20131998-07-24 18:25:38 +0000607laziness and will eventually be lifted.}
608It continues with the next cycle of the nearest enclosing loop.
Fred Drakef6669171998-05-06 19:52:49 +0000609\stindex{for}
610\stindex{while}
611\indexii{loop}{statement}
612\kwindex{finally}
613
Fred Drake2829f1c2001-06-23 05:27:20 +0000614
Fred Drake011f6fc1999-04-14 12:52:14 +0000615\section{The \keyword{import} statement \label{import}}
Fred Drakef6669171998-05-06 19:52:49 +0000616\stindex{import}
Fred Drakeb3be52e2003-07-15 21:37:58 +0000617\index{module!importing}
618\indexii{name}{binding}
619\kwindex{from}
Fred Drakef6669171998-05-06 19:52:49 +0000620
Fred Drakecb4638a2001-07-06 22:49:53 +0000621\begin{productionlist}
622 \production{import_stmt}
623 {"import" \token{module} ["as" \token{name}]
Fred Drake53815882002-03-15 23:21:37 +0000624 ( "," \token{module} ["as" \token{name}] )*}
625 \productioncont{| "from" \token{module} "import" \token{identifier}
626 ["as" \token{name}]}
627 \productioncont{ ( "," \token{identifier} ["as" \token{name}] )*}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000628 \productioncont{| "from" \token{module} "import" "(" \token{identifier}
629 ["as" \token{name}]}
630 \productioncont{ ( "," \token{identifier} ["as" \token{name}] )* [","] ")"}
Fred Drake53815882002-03-15 23:21:37 +0000631 \productioncont{| "from" \token{module} "import" "*"}
Fred Drakecb4638a2001-07-06 22:49:53 +0000632 \production{module}
633 {(\token{identifier} ".")* \token{identifier}}
634\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000635
636Import statements are executed in two steps: (1) find a module, and
637initialize it if necessary; (2) define a name or names in the local
Guido van Rossum56c20131998-07-24 18:25:38 +0000638namespace (of the scope where the \keyword{import} statement occurs).
Fred Drakef6669171998-05-06 19:52:49 +0000639The first form (without \keyword{from}) repeats these steps for each
Guido van Rossum56c20131998-07-24 18:25:38 +0000640identifier in the list. The form with \keyword{from} performs step
641(1) once, and then performs step (2) repeatedly.
Fred Drakef6669171998-05-06 19:52:49 +0000642
Raymond Hettingere701dcb2003-01-19 13:08:18 +0000643In this context, to ``initialize'' a built-in or extension module means to
644call an initialization function that the module must provide for the purpose
645(in the reference implementation, the function's name is obtained by
646prepending string ``init'' to the module's name); to ``initialize'' a
647Python-coded module means to execute the module's body.
648
649The system maintains a table of modules that have been or are being
650initialized,
Fred Drake191a2822000-07-06 00:50:42 +0000651indexed by module name. This table is
Guido van Rossum56c20131998-07-24 18:25:38 +0000652accessible as \code{sys.modules}. When a module name is found in
Fred Drakef6669171998-05-06 19:52:49 +0000653this table, step (1) is finished. If not, a search for a module
Guido van Rossum56c20131998-07-24 18:25:38 +0000654definition is started. When a module is found, it is loaded. Details
655of the module searching and loading process are implementation and
656platform specific. It generally involves searching for a ``built-in''
657module with the given name and then searching a list of locations
658given as \code{sys.path}.
Fred Drake2b3730e1998-11-25 17:40:00 +0000659\withsubitem{(in module sys)}{\ttindex{modules}}
Fred Drakef6669171998-05-06 19:52:49 +0000660\ttindex{sys.modules}
661\indexii{module}{name}
662\indexii{built-in}{module}
663\indexii{user-defined}{module}
664\refbimodindex{sys}
Fred Drakef6669171998-05-06 19:52:49 +0000665\indexii{filename}{extension}
Fred Drakedde91f01998-05-06 20:59:46 +0000666\indexiii{module}{search}{path}
Fred Drakef6669171998-05-06 19:52:49 +0000667
Fred Draked51ce7d2003-07-15 22:03:00 +0000668If a built-in module is found,\indexii{module}{initialization} its
669built-in initialization code is executed and step (1) is finished. If
670no matching file is found,
671\exception{ImportError}\exindex{ImportError} is raised.
672\index{code block}If a file is found, it is parsed,
Fred Drakef6669171998-05-06 19:52:49 +0000673yielding an executable code block. If a syntax error occurs,
Fred Draked51ce7d2003-07-15 22:03:00 +0000674\exception{SyntaxError}\exindex{SyntaxError} is raised. Otherwise, an
675empty module of the given name is created and inserted in the module
676table, and then the code block is executed in the context of this
677module. Exceptions during this execution terminate step (1).
Fred Drakef6669171998-05-06 19:52:49 +0000678
679When step (1) finishes without raising an exception, step (2) can
680begin.
681
Fred Drake859eb622001-03-06 07:34:00 +0000682The first form of \keyword{import} statement binds the module name in
683the local namespace to the module object, and then goes on to import
684the next identifier, if any. If the module name is followed by
685\keyword{as}, the name following \keyword{as} is used as the local
Martin v. Löwis13dd9d92003-01-16 11:30:08 +0000686name for the module.
Thomas Wouters8bad6122000-08-19 20:55:02 +0000687
Thomas Wouters52152252000-08-17 22:55:00 +0000688The \keyword{from} form does not bind the module name: it goes through the
689list of identifiers, looks each one of them up in the module found in step
690(1), and binds the name in the local namespace to the object thus found.
Fred Draked68442b2000-09-21 22:01:36 +0000691As with the first form of \keyword{import}, an alternate local name can be
Thomas Wouters52152252000-08-17 22:55:00 +0000692supplied by specifying "\keyword{as} localname". If a name is not found,
Fred Drakef6669171998-05-06 19:52:49 +0000693\exception{ImportError} is raised. If the list of identifiers is replaced
Fred Drake08fd5152001-10-24 19:50:31 +0000694by a star (\character{*}), all public names defined in the module are
695bound in the local namespace of the \keyword{import} statement..
Fred Drakef6669171998-05-06 19:52:49 +0000696\indexii{name}{binding}
697\exindex{ImportError}
698
Fred Drake08fd5152001-10-24 19:50:31 +0000699The \emph{public names} defined by a module are determined by checking
700the module's namespace for a variable named \code{__all__}; if
701defined, it must be a sequence of strings which are names defined or
702imported by that module. The names given in \code{__all__} are all
703considered public and are required to exist. If \code{__all__} is not
704defined, the set of public names includes all names found in the
705module's namespace which do not begin with an underscore character
Raymond Hettinger1772f172003-01-06 12:54:54 +0000706(\character{_}). \code{__all__} should contain the entire public API.
707It is intended to avoid accidentally exporting items that are not part
708of the API (such as library modules which were imported and used within
709the module).
Fred Drake27cae1f2002-12-07 16:00:00 +0000710\withsubitem{(optional module attribute)}{\ttindex{__all__}}
Fred Drake08fd5152001-10-24 19:50:31 +0000711
Jeremy Hyltonf0c1f1b2002-04-01 21:19:44 +0000712The \keyword{from} form with \samp{*} may only occur in a module
713scope. If the wild card form of import --- \samp{import *} --- is
714used in a function and the function contains or is a nested block with
715free variables, the compiler will raise a \exception{SyntaxError}.
716
Fred Drakef6669171998-05-06 19:52:49 +0000717\kwindex{from}
Fred Drake2b3730e1998-11-25 17:40:00 +0000718\stindex{from}
Fred Drakef6669171998-05-06 19:52:49 +0000719
Fred Drake246837d1998-07-24 20:28:22 +0000720\strong{Hierarchical module names:}\indexiii{hierarchical}{module}{names}
Guido van Rossum56c20131998-07-24 18:25:38 +0000721when the module names contains one or more dots, the module search
722path is carried out differently. The sequence of identifiers up to
723the last dot is used to find a ``package''\index{packages}; the final
724identifier is then searched inside the package. A package is
725generally a subdirectory of a directory on \code{sys.path} that has a
726file \file{__init__.py}.\ttindex{__init__.py}
727%
728[XXX Can't be bothered to spell this out right now; see the URL
Fred Drake1a0b8721998-08-07 17:40:20 +0000729\url{http://www.python.org/doc/essays/packages.html} for more details, also
Guido van Rossum56c20131998-07-24 18:25:38 +0000730about how the module search works from inside a package.]
731
Fred Drake08fd5152001-10-24 19:50:31 +0000732The built-in function \function{__import__()} is provided to support
733applications that determine which modules need to be loaded
734dynamically; refer to \ulink{Built-in
735Functions}{../lib/built-in-funcs.html} in the
736\citetitle[../lib/lib.html]{Python Library Reference} for additional
737information.
Guido van Rossum56c20131998-07-24 18:25:38 +0000738\bifuncindex{__import__}
739
Jeremy Hylton8bea5dc2003-05-21 21:43:00 +0000740\subsection{Future statements \label{future}}
741
742A \dfn{future statement}\indexii{future}{statement} is a directive to
743the compiler that a particular module should be compiled using syntax
744or semantics that will be available in a specified future release of
745Python. The future statement is intended to ease migration to future
746versions of Python that introduce incompatible changes to the
747language. It allows use of the new features on a per-module basis
748before the release in which the feature becomes standard.
749
750\begin{productionlist}[*]
751 \production{future_statement}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000752 {"from" "__future__" "import" feature ["as" name] ("," feature ["as" name])*}
753 \productioncont{| "from" "__future__" "import" "(" feature ["as" name] ("," feature ["as" name])* [","] ")"}
Jeremy Hylton8bea5dc2003-05-21 21:43:00 +0000754 \production{feature}{identifier}
755 \production{name}{identifier}
756\end{productionlist}
757
758A future statement must appear near the top of the module. The only
759lines that can appear before a future statement are:
760
761\begin{itemize}
762
763\item the module docstring (if any),
764\item comments,
765\item blank lines, and
766\item other future statements.
767
768\end{itemize}
769
770The features recognized by Python 2.3 are \samp{generators},
771\samp{division} and \samp{nested_scopes}. \samp{generators} and
772\samp{nested_scopes} are redundant in 2.3 because they are always
773enabled.
774
775A future statement is recognized and treated specially at compile
776time: Changes to the semantics of core constructs are often
777implemented by generating different code. It may even be the case
778that a new feature introduces new incompatible syntax (such as a new
779reserved word), in which case the compiler may need to parse the
780module differently. Such decisions cannot be pushed off until
781runtime.
782
783For any given release, the compiler knows which feature names have been
784defined, and raises a compile-time error if a future statement contains
785a feature not known to it.
786
787The direct runtime semantics are the same as for any import statement:
788there is a standard module \module{__future__}, described later, and
789it will be imported in the usual way at the time the future statement
790is executed.
791
792The interesting runtime semantics depend on the specific feature
793enabled by the future statement.
794
795Note that there is nothing special about the statement:
796
797\begin{verbatim}
798import __future__ [as name]
799\end{verbatim}
800
801That is not a future statement; it's an ordinary import statement with
802no special semantics or syntax restrictions.
803
804Code compiled by an exec statement or calls to the builtin functions
805\function{compile()} and \function{execfile()} that occur in a module
806\module{M} containing a future statement will, by default, use the new
807syntax or semantics associated with the future statement. This can,
808starting with Python 2.2 be controlled by optional arguments to
809\function{compile()} --- see the documentation of that function in the
810library reference for details.
811
812A future statement typed at an interactive interpreter prompt will
813take effect for the rest of the interpreter session. If an
814interpreter is started with the \programopt{-i} option, is passed a
815script name to execute, and the script includes a future statement, it
816will be in effect in the interactive session started after the script
817is executed.
Fred Drake2829f1c2001-06-23 05:27:20 +0000818
Fred Drake011f6fc1999-04-14 12:52:14 +0000819\section{The \keyword{global} statement \label{global}}
Fred Drakef6669171998-05-06 19:52:49 +0000820\stindex{global}
821
Fred Drakecb4638a2001-07-06 22:49:53 +0000822\begin{productionlist}
823 \production{global_stmt}
824 {"global" \token{identifier} ("," \token{identifier})*}
825\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000826
827The \keyword{global} statement is a declaration which holds for the
828entire current code block. It means that the listed identifiers are to be
Jeremy Hyltonf3255c82002-04-01 21:25:32 +0000829interpreted as globals. It would be impossible to assign to a global
830variable without \keyword{global}, although free variables may refer
831to globals without being declared global.
Fred Drakef6669171998-05-06 19:52:49 +0000832\indexiii{global}{name}{binding}
833
834Names listed in a \keyword{global} statement must not be used in the same
Guido van Rossumb1f97d61998-12-21 18:57:36 +0000835code block textually preceding that \keyword{global} statement.
Fred Drakef6669171998-05-06 19:52:49 +0000836
837Names listed in a \keyword{global} statement must not be defined as formal
838parameters or in a \keyword{for} loop control target, \keyword{class}
839definition, function definition, or \keyword{import} statement.
840
841(The current implementation does not enforce the latter two
842restrictions, but programs should not abuse this freedom, as future
843implementations may enforce them or silently change the meaning of the
844program.)
845
Guido van Rossum56c20131998-07-24 18:25:38 +0000846\strong{Programmer's note:}
847the \keyword{global} is a directive to the parser. It
Fred Drakef6669171998-05-06 19:52:49 +0000848applies only to code parsed at the same time as the \keyword{global}
849statement. In particular, a \keyword{global} statement contained in an
Fred Drakedde91f01998-05-06 20:59:46 +0000850\keyword{exec} statement does not affect the code block \emph{containing}
Fred Drakef6669171998-05-06 19:52:49 +0000851the \keyword{exec} statement, and code contained in an \keyword{exec}
852statement is unaffected by \keyword{global} statements in the code
853containing the \keyword{exec} statement. The same applies to the
854\function{eval()}, \function{execfile()} and \function{compile()} functions.
855\stindex{exec}
856\bifuncindex{eval}
857\bifuncindex{execfile}
858\bifuncindex{compile}
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000859
Fred Drake2829f1c2001-06-23 05:27:20 +0000860
Fred Drake011f6fc1999-04-14 12:52:14 +0000861\section{The \keyword{exec} statement \label{exec}}
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000862\stindex{exec}
863
Fred Drakecb4638a2001-07-06 22:49:53 +0000864\begin{productionlist}
865 \production{exec_stmt}
866 {"exec" \token{expression}
867 ["in" \token{expression} ["," \token{expression}]]}
868\end{productionlist}
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000869
870This statement supports dynamic execution of Python code. The first
871expression should evaluate to either a string, an open file object, or
872a code object. If it is a string, the string is parsed as a suite of
873Python statements which is then executed (unless a syntax error
Fred Drake93852ef2001-06-23 06:06:52 +0000874occurs). If it is an open file, the file is parsed until \EOF{} and
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000875executed. If it is a code object, it is simply executed.
876
877In all cases, if the optional parts are omitted, the code is executed
878in the current scope. If only the first expression after \keyword{in}
879is specified, it should be a dictionary, which will be used for both
880the global and the local variables. If two expressions are given,
Raymond Hettinger70fcdb82004-08-03 05:17:58 +0000881they are used for the global and local variables, respectively.
882If provided, \var{locals} can be any mapping object.
883\versionchanged[formerly \var{locals} was required to be a dictionary]{2.4}
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000884
885As a side effect, an implementation may insert additional keys into
886the dictionaries given besides those corresponding to variable names
887set by the executed code. For example, the current implementation
888may add a reference to the dictionary of the built-in module
889\module{__builtin__} under the key \code{__builtins__} (!).
890\ttindex{__builtins__}
891\refbimodindex{__builtin__}
892
Guido van Rossum56c20131998-07-24 18:25:38 +0000893\strong{Programmer's hints:}
894dynamic evaluation of expressions is supported by the built-in
Guido van Rossum5f574aa1998-07-06 13:18:39 +0000895function \function{eval()}. The built-in functions
896\function{globals()} and \function{locals()} return the current global
897and local dictionary, respectively, which may be useful to pass around
898for use by \keyword{exec}.
899\bifuncindex{eval}
900\bifuncindex{globals}
901\bifuncindex{locals}
Greg Ward38c28e32000-04-27 18:32:02 +0000902
Greg Ward38c28e32000-04-27 18:32:02 +0000903
904