blob: b1d4572aba3f7a5ac9a642a29aab735a7257fbc1 [file] [log] [blame]
Guido van Rossumda43a4a1992-08-14 09:17:29 +00001\chapter{Simple statements}
2\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
8\begin{verbatim}
9simple_stmt: expression_stmt
10 | assignment_stmt
11 | pass_stmt
12 | del_stmt
13 | print_stmt
14 | return_stmt
15 | raise_stmt
16 | break_stmt
17 | continue_stmt
18 | import_stmt
19 | global_stmt
Guido van Rossuma75d3061993-10-18 17:59:42 +000020 | access_stmt
21 | exec_stmt
Guido van Rossumda43a4a1992-08-14 09:17:29 +000022\end{verbatim}
23
24\section{Expression statements}
25\indexii{expression}{statement}
26
27Expression statements are used (mostly interactively) to compute and
28write a value, or (usually) to call a procedure (a function that
29returns no meaningful result; in Python, procedures return the value
Guido van Rossume9914961994-08-01 12:38:14 +000030\verb@None@):
Guido van Rossumda43a4a1992-08-14 09:17:29 +000031
32\begin{verbatim}
33expression_stmt: expression_list
34\end{verbatim}
35
36An expression statement evaluates the expression list (which may be a
Guido van Rossume9914961994-08-01 12:38:14 +000037single expression). If the value is not \verb@None@, it is converted
Guido van Rossumda43a4a1992-08-14 09:17:29 +000038to a string using the rules for string conversions (expressions in
39reverse quotes), and the resulting string is written to standard
40output (see section \ref{print}) on a line by itself.
41\indexii{expression}{list}
42\ttindex{None}
43\indexii{string}{conversion}
44\index{output}
45\indexii{standard}{output}
46\indexii{writing}{values}
47
Guido van Rossume9914961994-08-01 12:38:14 +000048(The exception for \verb@None@ is made so that procedure calls, which
Guido van Rossumda43a4a1992-08-14 09:17:29 +000049are syntactically equivalent to expressions, do not cause any output.
Guido van Rossume9914961994-08-01 12:38:14 +000050A tuple with only \verb@None@ items is written normally.)
Guido van Rossumda43a4a1992-08-14 09:17:29 +000051\indexii{procedure}{call}
52
53\section{Assignment statements}
54\indexii{assignment}{statement}
55
56Assignment statements are used to (re)bind names to values and to
57modify attributes or items of mutable objects:
58\indexii{binding}{name}
59\indexii{rebinding}{name}
60\obindex{mutable}
61\indexii{attribute}{assignment}
62
63\begin{verbatim}
64assignment_stmt: (target_list "=")+ expression_list
65target_list: target ("," target)* [","]
66target: identifier | "(" target_list ")" | "[" target_list "]"
67 | attributeref | subscription | slicing
68\end{verbatim}
69
70(See section \ref{primaries} for the syntax definitions for the last
71three symbols.)
72
73An assignment statement evaluates the expression list (remember that
74this can be a single expression or a comma-separated list, the latter
75yielding a tuple) and assigns the single resulting object to each of
76the target lists, from left to right.
77\indexii{expression}{list}
78
79Assignment is defined recursively depending on the form of the target
80(list). When a target is part of a mutable object (an attribute
81reference, subscription or slicing), the mutable object must
82ultimately perform the assignment and decide about its validity, and
83may raise an exception if the assignment is unacceptable. The rules
84observed by various types and the exceptions raised are given with the
85definition of the object types (see section \ref{types}).
86\index{target}
87\indexii{target}{list}
88
89Assignment of an object to a target list is recursively defined as
90follows.
91\indexiii{target}{list}{assignment}
92
93\begin{itemize}
94\item
95If the target list is a single target: the object is assigned to that
96target.
97
98\item
99If the target list is a comma-separated list of targets: the object
100must be a tuple with the same number of items as the list contains
101targets, and the items are assigned, from left to right, to the
102corresponding targets.
103
104\end{itemize}
105
106Assignment of an object to a single target is recursively defined as
107follows.
108
109\begin{itemize} % nested
110
111\item
112If the target is an identifier (name):
113
114\begin{itemize}
115
116\item
Guido van Rossume9914961994-08-01 12:38:14 +0000117If the name does not occur in a \verb@global@ statement in the current
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000118code block: the name is bound to the object in the current local name
119space.
120\stindex{global}
121
122\item
123Otherwise: the name is bound to the object in the current global name
124space.
125
126\end{itemize} % nested
127
128The name is rebound if it was already bound.
129
130\item
131If the target is a target list enclosed in parentheses: the object is
132assigned to that target list as described above.
133
134\item
135If the target is a target list enclosed in square brackets: the object
136must be a list with the same number of items as the target list
137contains targets, and its items are assigned, from left to right, to
138the corresponding targets.
139
140\item
141If the target is an attribute reference: The primary expression in the
142reference is evaluated. It should yield an object with assignable
Guido van Rossume9914961994-08-01 12:38:14 +0000143attributes; if this is not the case, \verb@TypeError@ is raised. That
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000144object is then asked to assign the assigned object to the given
145attribute; if it cannot perform the assignment, it raises an exception
Guido van Rossume9914961994-08-01 12:38:14 +0000146(usually but not necessarily \verb@AttributeError@).
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000147\indexii{attribute}{assignment}
148
149\item
150If the target is a subscription: The primary expression in the
151reference is evaluated. It should yield either a mutable sequence
152(list) object or a mapping (dictionary) object. Next, the subscript
153expression is evaluated.
154\indexii{subscription}{assignment}
155\obindex{mutable}
156
157If the primary is a mutable sequence object (a list), the subscript
158must yield a plain integer. If it is negative, the sequence's length
159is added to it. The resulting value must be a nonnegative integer
160less than the sequence's length, and the sequence is asked to assign
161the assigned object to its item with that index. If the index is out
Guido van Rossume9914961994-08-01 12:38:14 +0000162of range, \verb@IndexError@ is raised (assignment to a subscripted
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000163sequence cannot add new items to a list).
164\obindex{sequence}
165\obindex{list}
166
167If the primary is a mapping (dictionary) object, the subscript must
168have a type compatible with the mapping's key type, and the mapping is
169then asked to to create a key/datum pair which maps the subscript to
170the assigned object. This can either replace an existing key/value
171pair with the same key value, or insert a new key/value pair (if no
172key with the same value existed).
173\obindex{mapping}
174\obindex{dictionary}
175
176\item
177If the target is a slicing: The primary expression in the reference is
Guido van Rossume9914961994-08-01 12:38:14 +0000178evaluated. It should yield a mutable sequence object (e.g. a list). The
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000179assigned object should be a sequence object of the same type. Next,
180the lower and upper bound expressions are evaluated, insofar they are
181present; defaults are zero and the sequence's length. The bounds
182should evaluate to (small) integers. If either bound is negative, the
183sequence's length is added to it. The resulting bounds are clipped to
184lie between zero and the sequence's length, inclusive. Finally, the
Guido van Rossume9914961994-08-01 12:38:14 +0000185sequence object is asked to replace the slice with the items of the
186assigned sequence. The length of the slice may be different from the
187length of the assigned sequence, thus changing the length of the
188target sequence, if the object allows it.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000189\indexii{slicing}{assignment}
190
191\end{itemize}
192
193(In the original implementation, the syntax for targets is taken
194to be the same as for expressions, and invalid syntax is rejected
195during the code generation phase, causing less detailed error
196messages.)
197
198\section{The {\tt pass} statement}
199\stindex{pass}
200
201\begin{verbatim}
202pass_stmt: "pass"
203\end{verbatim}
204
Guido van Rossume9914961994-08-01 12:38:14 +0000205\verb@pass@ is a null operation --- when it is executed, nothing
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000206happens. It is useful as a placeholder when a statement is
207required syntactically, but no code needs to be executed, for example:
208\indexii{null}{operation}
209
210\begin{verbatim}
211def f(arg): pass # a function that does nothing (yet)
212
213class C: pass # an class with no methods (yet)
214\end{verbatim}
215
216\section{The {\tt del} statement}
217\stindex{del}
218
219\begin{verbatim}
220del_stmt: "del" target_list
221\end{verbatim}
222
223Deletion is recursively defined very similar to the way assignment is
224defined. Rather that spelling it out in full details, here are some
225hints.
226\indexii{deletion}{target}
227\indexiii{deletion}{target}{list}
228
229Deletion of a target list recursively deletes each target, from left
230to right.
231
232Deletion of a name removes the binding of that name (which must exist)
233from the local or global name space, depending on whether the name
Guido van Rossume9914961994-08-01 12:38:14 +0000234occurs in a \verb@global@ statement in the same code block.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000235\stindex{global}
236\indexii{unbinding}{name}
237
238Deletion of attribute references, subscriptions and slicings
239is passed to the primary object involved; deletion of a slicing
240is in general equivalent to assignment of an empty slice of the
241right type (but even this is determined by the sliced object).
242\indexii{attribute}{deletion}
243
244\section{The {\tt print} statement} \label{print}
245\stindex{print}
246
247\begin{verbatim}
248print_stmt: "print" [ condition ("," condition)* [","] ]
249\end{verbatim}
250
Guido van Rossume9914961994-08-01 12:38:14 +0000251\verb@print@ evaluates each condition in turn and writes the resulting
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000252object to standard output (see below). If an object is not a string,
253it is first converted to a string using the rules for string
254conversions. The (resulting or original) string is then written. A
255space is written before each object is (converted and) written, unless
256the output system believes it is positioned at the beginning of a
257line. This is the case: (1) when no characters have yet been written
258to standard output; or (2) when the last character written to standard
259output is \verb/\n/; or (3) when the last write operation on standard
Guido van Rossume9914961994-08-01 12:38:14 +0000260output was not a \verb@print@ statement. (In some cases it may be
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000261functional to write an empty string to standard output for this
262reason.)
263\index{output}
264\indexii{writing}{values}
265
Guido van Rossume9914961994-08-01 12:38:14 +0000266A \verb/"\n"/ character is written at the end, unless the \verb@print@
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000267statement ends with a comma. This is the only action if the statement
Guido van Rossume9914961994-08-01 12:38:14 +0000268contains just the keyword \verb@print@.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000269\indexii{trailing}{comma}
270\indexii{newline}{suppression}
271
Guido van Rossume9914961994-08-01 12:38:14 +0000272Standard output is defined as the file object named \verb@stdout@
273in the built-in module \verb@sys@. If no such object exists,
274or if it is not a writable file, a \verb@RuntimeError@ exception is raised.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000275(The original implementation attempts to write to the system's original
276standard output instead, but this is not safe, and should be fixed.)
277\indexii{standard}{output}
278\bimodindex{sys}
279\ttindex{stdout}
280\exindex{RuntimeError}
281
282\section{The {\tt return} statement}
283\stindex{return}
284
285\begin{verbatim}
286return_stmt: "return" [condition_list]
287\end{verbatim}
288
Guido van Rossume9914961994-08-01 12:38:14 +0000289\verb@return@ may only occur syntactically nested in a function
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000290definition, not within a nested class definition.
291\indexii{function}{definition}
292\indexii{class}{definition}
293
Guido van Rossume9914961994-08-01 12:38:14 +0000294If a condition list is present, it is evaluated, else \verb@None@
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000295is substituted.
296
Guido van Rossume9914961994-08-01 12:38:14 +0000297\verb@return@ leaves the current function call with the condition
298list (or \verb@None@) as return value.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000299
Guido van Rossume9914961994-08-01 12:38:14 +0000300When \verb@return@ passes control out of a \verb@try@ statement
301with a \verb@finally@ clause, that finally clause is executed
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000302before really leaving the function.
303\kwindex{finally}
304
305\section{The {\tt raise} statement}
306\stindex{raise}
307
308\begin{verbatim}
309raise_stmt: "raise" condition ["," condition]
310\end{verbatim}
311
Guido van Rossume9914961994-08-01 12:38:14 +0000312\verb@raise@ evaluates its first condition, which must yield
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000313a string object. If there is a second condition, this is evaluated,
Guido van Rossume9914961994-08-01 12:38:14 +0000314else \verb@None@ is substituted.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000315\index{exception}
316\indexii{raising}{exception}
317
318It then raises the exception identified by the first object,
Guido van Rossume9914961994-08-01 12:38:14 +0000319with the second one (or \verb@None@) as its parameter.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000320
321\section{The {\tt break} statement}
322\stindex{break}
323
324\begin{verbatim}
325break_stmt: "break"
326\end{verbatim}
327
Guido van Rossume9914961994-08-01 12:38:14 +0000328\verb@break@ may only occur syntactically nested in a \verb@for@
329or \verb@while@ loop, but not nested in a function or class definition
330within that loop.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000331\stindex{for}
332\stindex{while}
333\indexii{loop}{statement}
334
Guido van Rossume9914961994-08-01 12:38:14 +0000335It terminates the nearest enclosing loop, skipping the optional
336\verb@else@ clause if the loop has one.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000337\kwindex{else}
338
Guido van Rossume9914961994-08-01 12:38:14 +0000339If a \verb@for@ loop is terminated by \verb@break@, the loop control
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000340target keeps its current value.
341\indexii{loop control}{target}
342
Guido van Rossume9914961994-08-01 12:38:14 +0000343When \verb@break@ passes control out of a \verb@try@ statement
344with a \verb@finally@ clause, that finally clause is executed
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000345before really leaving the loop.
346\kwindex{finally}
347
348\section{The {\tt continue} statement}
349\stindex{continue}
350
351\begin{verbatim}
352continue_stmt: "continue"
353\end{verbatim}
354
Guido van Rossume9914961994-08-01 12:38:14 +0000355\verb@continue@ may only occur syntactically nested in a \verb@for@ or
356\verb@while@ loop, but not nested in a function or class definition or
357\verb@try@ statement within that loop.\footnote{Except that it may
358currently occur within an \verb@except@ clause.}
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000359\stindex{for}
360\stindex{while}
361\indexii{loop}{statement}
362\kwindex{finally}
363
364It continues with the next cycle of the nearest enclosing loop.
365
366\section{The {\tt import} statement} \label{import}
367\stindex{import}
368
369\begin{verbatim}
370import_stmt: "import" identifier ("," identifier)*
371 | "from" identifier "import" identifier ("," identifier)*
372 | "from" identifier "import" "*"
373\end{verbatim}
374
375Import statements are executed in two steps: (1) find a module, and
376initialize it if necessary; (2) define a name or names in the local
Guido van Rossume9914961994-08-01 12:38:14 +0000377name space (of the scope where the \verb@import@ statement occurs).
378The first form (without \verb@from@) repeats these steps for each
379identifier in the list, the \verb@from@ form performs them once, with
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000380the first identifier specifying the module name.
381\indexii{importing}{module}
382\indexii{name}{binding}
383\kwindex{from}
384
385The system maintains a table of modules that have been initialized,
386indexed by module name. (The current implementation makes this table
Guido van Rossume9914961994-08-01 12:38:14 +0000387accessible as \verb@sys.modules@.) When a module name is found in
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000388this table, step (1) is finished. If not, a search for a module
389definition is started. This first looks for a built-in module
390definition, and if no built-in module if the given name is found, it
391searches a user-specified list of directories for a file whose name is
Guido van Rossume9914961994-08-01 12:38:14 +0000392the module name with extension \verb@".py"@. (The current
393implementation uses the list of strings \verb@sys.path@ as the search
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000394path; it is initialized from the shell environment variable
Guido van Rossume9914961994-08-01 12:38:14 +0000395\verb@$PYTHONPATH@, with an installation-dependent default.)
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000396\ttindex{modules}
397\ttindex{sys.modules}
398\indexii{module}{name}
399\indexii{built-in}{module}
400\indexii{user-defined}{module}
401\bimodindex{sys}
402\ttindex{path}
403\ttindex{sys.path}
404\indexii{filename}{extension}
405
406If a built-in module is found, its built-in initialization code is
407executed and step (1) is finished. If no matching file is found,
Guido van Rossume9914961994-08-01 12:38:14 +0000408\verb@ImportError@ is raised. If a file is found, it is parsed,
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000409yielding an executable code block. If a syntax error occurs,
Guido van Rossume9914961994-08-01 12:38:14 +0000410\verb@SyntaxError@ is raised. Otherwise, an empty module of the given
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000411name is created and inserted in the module table, and then the code
412block is executed in the context of this module. Exceptions during
413this execution terminate step (1).
414\indexii{module}{initialization}
415\exindex{SyntaxError}
416\exindex{ImportError}
417\index{code block}
418
419When step (1) finishes without raising an exception, step (2) can
420begin.
421
Guido van Rossume9914961994-08-01 12:38:14 +0000422The first form of \verb@import@ statement binds the module name in the
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000423local name space to the module object, and then goes on to import the
Guido van Rossume9914961994-08-01 12:38:14 +0000424next identifier, if any. The \verb@from@ from does not bind the
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000425module name: it goes through the list of identifiers, looks each one
426of them up in the module found in step (1), and binds the name in the
427local name space to the object thus found. If a name is not found,
Guido van Rossume9914961994-08-01 12:38:14 +0000428\verb@ImportError@ is raised. If the list of identifiers is replaced
429by a star (\verb@*@), all names defined in the module are bound,
430except those beginning with an underscore(\verb@_@).
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000431\indexii{name}{binding}
432\exindex{ImportError}
433
Guido van Rossume9914961994-08-01 12:38:14 +0000434Names bound by import statements may not occur in \verb@global@
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000435statements in the same scope.
436\stindex{global}
437
Guido van Rossume9914961994-08-01 12:38:14 +0000438The \verb@from@ form with \verb@*@ may only occur in a module scope.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000439\kwindex{from}
440\ttindex{from ... import *}
441
442(The current implementation does not enforce the latter two
443restrictions, but programs should not abuse this freedom, as future
444implementations may enforce them or silently change the meaning of the
445program.)
446
447\section{The {\tt global} statement} \label{global}
448\stindex{global}
449
450\begin{verbatim}
451global_stmt: "global" identifier ("," identifier)*
452\end{verbatim}
453
Guido van Rossume9914961994-08-01 12:38:14 +0000454The \verb@global@ statement is a declaration which holds for the
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000455entire current scope. It means that the listed identifiers are to be
456interpreted as globals. While {\em using} global names is automatic
457if they are not defined in the local scope, {\em assigning} to global
Guido van Rossume9914961994-08-01 12:38:14 +0000458names would be impossible without \verb@global@.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000459\indexiii{global}{name}{binding}
460
Guido van Rossume9914961994-08-01 12:38:14 +0000461Names listed in a \verb@global@ statement must not be used in the same
462scope before that \verb@global@ statement is executed.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000463
Guido van Rossume9914961994-08-01 12:38:14 +0000464Names listed in a \verb@global@ statement must not be defined as formal
465parameters or in a \verb@for@ loop control target, \verb@class@
466definition, function definition, or \verb@import@ statement.
Guido van Rossumda43a4a1992-08-14 09:17:29 +0000467
468(The current implementation does not enforce the latter two
469restrictions, but programs should not abuse this freedom, as future
470implementations may enforce them or silently change the meaning of the
471program.)
Guido van Rossuma75d3061993-10-18 17:59:42 +0000472
473\section{The {\tt access} statement} \label{access}
474\stindex{access}
475
476\begin{verbatim}
477access_stmt: "access" ...
478\end{verbatim}
479
480This statement will be used in the future to control access to
481instance and class variables. Currently its syntax and effects are
Guido van Rossume9914961994-08-01 12:38:14 +0000482undefined; however the keyword \verb@access@ is a reserved word for
Guido van Rossuma75d3061993-10-18 17:59:42 +0000483the parser.
484
485\section{The {\tt exec} statement} \label{exec}
486\stindex{exec}
487
488\begin{verbatim}
489exec_stmt: "exec" expression ["in" expression ["," expression]]
490\end{verbatim}
491
492This statement supports dynamic execution of Python code. The first
493expression should evaluate to either a string, an open file object, or
494a code object. If it is a string, the string is parsed as a suite of
495Python statements which is then executed (unless a syntax error
496occurs). If it is an open file, the file is parsed until EOF and
497executed. If it is a code object, it is simply executed.
498
499In all cases, if the optional parts are omitted, the code is executed
Guido van Rossume9914961994-08-01 12:38:14 +0000500in the current scope. If only the first expression after \verb@in@ is
Guido van Rossuma75d3061993-10-18 17:59:42 +0000501specified, it should be a dictionary, which will be used for both the
502global and the local variables. If two expressions are given, both
503must be dictionaries and they are used for the global and local
504variables, respectively.
505
506Note: dynamic evaluation of expressions is supported by the built-in
Guido van Rossume9914961994-08-01 12:38:14 +0000507function \verb@eval@.
Guido van Rossuma75d3061993-10-18 17:59:42 +0000508