| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 1 | \chapter{Simple statements \label{simple}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | \indexii{simple}{statement} | 
|  | 3 |  | 
|  | 4 | Simple statements are comprised within a single logical line. | 
|  | 5 | Several simple statements may occur on a single line separated | 
|  | 6 | by semicolons.  The syntax for simple statements is: | 
|  | 7 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 8 | \begin{productionlist} | 
|  | 9 | \production{simple_stmt} | 
|  | 10 | {\token{expression_stmt} | 
|  | 11 | | \token{assert_stmt} | 
|  | 12 | | \token{assignment_stmt} | 
|  | 13 | | \token{augmented_assignment_stmt} | 
|  | 14 | | \token{pass_stmt} | 
|  | 15 | | \token{del_stmt} | 
|  | 16 | | \token{print_stmt} | 
|  | 17 | | \token{return_stmt} | 
| Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 18 | | \token{yield_stmt} | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 19 | | \token{raise_stmt} | 
|  | 20 | | \token{break_stmt} | 
|  | 21 | | \token{continue_stmt} | 
|  | 22 | | \token{import_stmt} | 
|  | 23 | | \token{global_stmt} | 
|  | 24 | | \token{exec_stmt}} | 
|  | 25 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 26 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 27 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 28 | \section{Expression statements \label{exprstmts}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 29 | \indexii{expression}{statement} | 
|  | 30 |  | 
|  | 31 | Expression statements are used (mostly interactively) to compute and | 
|  | 32 | write a value, or (usually) to call a procedure (a function that | 
|  | 33 | returns no meaningful result; in Python, procedures return the value | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 34 | \code{None}).  Other uses of expression statements are allowed and | 
|  | 35 | occasionally useful.  The syntax for an expression statement is: | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 36 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 37 | \begin{productionlist} | 
|  | 38 | \production{expression_stmt} | 
|  | 39 | {\token{expression_list}} | 
|  | 40 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 41 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 42 | An expression statement evaluates the expression list (which may be a | 
|  | 43 | single expression). | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 44 | \indexii{expression}{list} | 
|  | 45 |  | 
|  | 46 | In interactive mode, if the value is not \code{None}, it is converted | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 47 | to a string using the built-in \function{repr()}\bifuncindex{repr} | 
|  | 48 | function and the resulting string is written to standard output (see | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 49 | section~\ref{print}) on a line by itself.  (Expression statements | 
|  | 50 | yielding \code{None} are not written, so that procedure calls do not | 
|  | 51 | cause any output.) | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 52 | \ttindex{None} | 
|  | 53 | \indexii{string}{conversion} | 
|  | 54 | \index{output} | 
|  | 55 | \indexii{standard}{output} | 
|  | 56 | \indexii{writing}{values} | 
|  | 57 | \indexii{procedure}{call} | 
|  | 58 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 59 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 60 | \section{Assert statements \label{assert}} | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 61 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 62 | Assert statements\stindex{assert} are a convenient way to insert | 
|  | 63 | debugging assertions\indexii{debugging}{assertions} into a program: | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 64 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 65 | \begin{productionlist} | 
|  | 66 | \production{assert_statement} | 
|  | 67 | {"assert" \token{expression} ["," \token{expression}]} | 
|  | 68 | \end{productionlist} | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 69 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 70 | The simple form, \samp{assert expression}, is equivalent to | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | \begin{verbatim} | 
|  | 73 | if __debug__: | 
|  | 74 | if not expression: raise AssertionError | 
|  | 75 | \end{verbatim} | 
|  | 76 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 77 | The extended form, \samp{assert expression1, expression2}, is | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 78 | equivalent to | 
|  | 79 |  | 
|  | 80 | \begin{verbatim} | 
|  | 81 | if __debug__: | 
|  | 82 | if not expression1: raise AssertionError, expression2 | 
|  | 83 | \end{verbatim} | 
|  | 84 |  | 
|  | 85 | These equivalences assume that \code{__debug__}\ttindex{__debug__} and | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 86 | \exception{AssertionError}\exindex{AssertionError} refer to the built-in | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 87 | variables with those names.  In the current implementation, the | 
|  | 88 | built-in variable \code{__debug__} is 1 under normal circumstances, 0 | 
|  | 89 | when optimization is requested (command line option -O).  The current | 
|  | 90 | code generator emits no code for an assert statement when optimization | 
|  | 91 | is requested at compile time.  Note that it is unnecessary to include | 
|  | 92 | the source code for the expression that failed in the error message; | 
|  | 93 | it will be displayed as part of the stack trace. | 
|  | 94 |  | 
| Jeremy Hylton | 2c84fc8 | 2001-03-23 14:34:06 +0000 | [diff] [blame] | 95 | Assignments to \code{__debug__} are illegal.  The value for the | 
|  | 96 | built-in variable is determined when the interpreter starts. | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 97 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 98 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 99 | \section{Assignment statements \label{assignment}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 100 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 101 | Assignment statements\indexii{assignment}{statement} are used to | 
|  | 102 | (re)bind names to values and to modify attributes or items of mutable | 
|  | 103 | objects: | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 104 | \indexii{binding}{name} | 
|  | 105 | \indexii{rebinding}{name} | 
|  | 106 | \obindex{mutable} | 
|  | 107 | \indexii{attribute}{assignment} | 
|  | 108 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 109 | \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} | 
|  | 115 | {\token{identifier} | 
|  | 116 | | "(" \token{target_list} ")" | 
|  | 117 | | "[" \token{target_list} "]" | 
|  | 118 | | \token{attributeref} | 
|  | 119 | | \token{subscription} | 
|  | 120 | | \token{slicing}} | 
|  | 121 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 122 |  | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 123 | (See section~\ref{primaries} for the syntax definitions for the last | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 124 | three symbols.) | 
|  | 125 |  | 
|  | 126 | An assignment statement evaluates the expression list (remember that | 
|  | 127 | this can be a single expression or a comma-separated list, the latter | 
|  | 128 | yielding a tuple) and assigns the single resulting object to each of | 
|  | 129 | the target lists, from left to right. | 
|  | 130 | \indexii{expression}{list} | 
|  | 131 |  | 
|  | 132 | Assignment is defined recursively depending on the form of the target | 
|  | 133 | (list).  When a target is part of a mutable object (an attribute | 
|  | 134 | reference, subscription or slicing), the mutable object must | 
|  | 135 | ultimately perform the assignment and decide about its validity, and | 
|  | 136 | may raise an exception if the assignment is unacceptable.  The rules | 
|  | 137 | observed by various types and the exceptions raised are given with the | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 138 | definition of the object types (see section~\ref{types}). | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 139 | \index{target} | 
|  | 140 | \indexii{target}{list} | 
|  | 141 |  | 
|  | 142 | Assignment of an object to a target list is recursively defined as | 
|  | 143 | follows. | 
|  | 144 | \indexiii{target}{list}{assignment} | 
|  | 145 |  | 
|  | 146 | \begin{itemize} | 
|  | 147 | \item | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 148 | If the target list is a single target: The object is assigned to that | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 149 | target. | 
|  | 150 |  | 
|  | 151 | \item | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 152 | If the target list is a comma-separated list of targets: The object | 
|  | 153 | must be a sequence with the same number of items as the there are | 
|  | 154 | targets in the target list, and the items are assigned, from left to | 
|  | 155 | right, to the corresponding targets.  (This rule is relaxed as of | 
|  | 156 | Python 1.5; in earlier versions, the object had to be a tuple.  Since | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 157 | strings are sequences, an assignment like \samp{a, b = "xy"} is | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 158 | now legal as long as the string has the right length.) | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | \end{itemize} | 
|  | 161 |  | 
|  | 162 | Assignment of an object to a single target is recursively defined as | 
|  | 163 | follows. | 
|  | 164 |  | 
|  | 165 | \begin{itemize} % nested | 
|  | 166 |  | 
|  | 167 | \item | 
|  | 168 | If the target is an identifier (name): | 
|  | 169 |  | 
|  | 170 | \begin{itemize} | 
|  | 171 |  | 
|  | 172 | \item | 
|  | 173 | If the name does not occur in a \keyword{global} statement in the current | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 174 | code block: the name is bound to the object in the current local | 
|  | 175 | namespace. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 176 | \stindex{global} | 
|  | 177 |  | 
|  | 178 | \item | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 179 | Otherwise: the name is bound to the object in the current global | 
|  | 180 | namespace. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 181 |  | 
|  | 182 | \end{itemize} % nested | 
|  | 183 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 184 | The name is rebound if it was already bound.  This may cause the | 
|  | 185 | reference count for the object previously bound to the name to reach | 
|  | 186 | zero, causing the object to be deallocated and its | 
|  | 187 | destructor\index{destructor} (if it has one) to be called. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | \item | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 190 | If the target is a target list enclosed in parentheses or in square | 
|  | 191 | brackets: The object must be a sequence with the same number of items | 
|  | 192 | as there are targets in the target list, and its items are assigned, | 
|  | 193 | from left to right, to the corresponding targets. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 194 |  | 
|  | 195 | \item | 
|  | 196 | If the target is an attribute reference: The primary expression in the | 
|  | 197 | reference is evaluated.  It should yield an object with assignable | 
|  | 198 | attributes; if this is not the case, \exception{TypeError} is raised.  That | 
|  | 199 | object is then asked to assign the assigned object to the given | 
|  | 200 | attribute; if it cannot perform the assignment, it raises an exception | 
|  | 201 | (usually but not necessarily \exception{AttributeError}). | 
|  | 202 | \indexii{attribute}{assignment} | 
|  | 203 |  | 
|  | 204 | \item | 
|  | 205 | If the target is a subscription: The primary expression in the | 
|  | 206 | reference is evaluated.  It should yield either a mutable sequence | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 207 | object (e.g., a list) or a mapping object (e.g., a dictionary).  Next, | 
|  | 208 | the subscript expression is evaluated. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 209 | \indexii{subscription}{assignment} | 
|  | 210 | \obindex{mutable} | 
|  | 211 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 212 | If the primary is a mutable sequence object (e.g., a list), the subscript | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 213 | must yield a plain integer.  If it is negative, the sequence's length | 
|  | 214 | is added to it.  The resulting value must be a nonnegative integer | 
|  | 215 | less than the sequence's length, and the sequence is asked to assign | 
|  | 216 | the assigned object to its item with that index.  If the index is out | 
|  | 217 | of range, \exception{IndexError} is raised (assignment to a subscripted | 
|  | 218 | sequence cannot add new items to a list). | 
|  | 219 | \obindex{sequence} | 
|  | 220 | \obindex{list} | 
|  | 221 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 222 | If the primary is a mapping object (e.g., a dictionary), the subscript must | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 223 | have a type compatible with the mapping's key type, and the mapping is | 
|  | 224 | then asked to create a key/datum pair which maps the subscript to | 
|  | 225 | the assigned object.  This can either replace an existing key/value | 
|  | 226 | pair with the same key value, or insert a new key/value pair (if no | 
|  | 227 | key with the same value existed). | 
|  | 228 | \obindex{mapping} | 
|  | 229 | \obindex{dictionary} | 
|  | 230 |  | 
|  | 231 | \item | 
|  | 232 | If the target is a slicing: The primary expression in the reference is | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 233 | evaluated.  It should yield a mutable sequence object (e.g., a list).  The | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 234 | assigned object should be a sequence object of the same type.  Next, | 
|  | 235 | the lower and upper bound expressions are evaluated, insofar they are | 
|  | 236 | present; defaults are zero and the sequence's length.  The bounds | 
|  | 237 | should evaluate to (small) integers.  If either bound is negative, the | 
|  | 238 | sequence's length is added to it.  The resulting bounds are clipped to | 
|  | 239 | lie between zero and the sequence's length, inclusive.  Finally, the | 
|  | 240 | sequence object is asked to replace the slice with the items of the | 
|  | 241 | assigned sequence.  The length of the slice may be different from the | 
|  | 242 | length of the assigned sequence, thus changing the length of the | 
|  | 243 | target sequence, if the object allows it. | 
|  | 244 | \indexii{slicing}{assignment} | 
|  | 245 |  | 
|  | 246 | \end{itemize} | 
| Greg Ward | 38c28e3 | 2000-04-27 18:32:02 +0000 | [diff] [blame] | 247 |  | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 248 | (In the current implementation, the syntax for targets is taken | 
|  | 249 | to be the same as for expressions, and invalid syntax is rejected | 
|  | 250 | during the code generation phase, causing less detailed error | 
|  | 251 | messages.) | 
|  | 252 |  | 
|  | 253 | WARNING: Although the definition of assignment implies that overlaps | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 254 | between the left-hand side and the right-hand side are `safe' (e.g., | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 255 | \samp{a, b = b, a} swaps two variables), overlaps \emph{within} the | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 256 | collection of assigned-to variables are not safe!  For instance, the | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 257 | following program prints \samp{[0, 2]}: | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 258 |  | 
|  | 259 | \begin{verbatim} | 
|  | 260 | x = [0, 1] | 
|  | 261 | i = 0 | 
|  | 262 | i, x[i] = 1, 2 | 
|  | 263 | print x | 
|  | 264 | \end{verbatim} | 
|  | 265 |  | 
|  | 266 |  | 
| Fred Drake | 31f5550 | 2000-09-12 20:32:18 +0000 | [diff] [blame] | 267 | \subsection{Augmented Assignment statements \label{augassign}} | 
|  | 268 |  | 
|  | 269 | Augmented assignment is the combination, in a single statement, of a binary | 
|  | 270 | operation and an assignment statement: | 
|  | 271 | \indexii{augmented}{assignment} | 
|  | 272 | \index{statement!assignment, augmented} | 
|  | 273 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 274 | \begin{productionlist} | 
|  | 275 | \production{augmented_assignment_stmt} | 
|  | 276 | {\token{target} \token{augop} \token{expression_list}} | 
|  | 277 | \production{augop} | 
|  | 278 | {"+=" | "-=" | "*=" | "/=" | "\%=" | "**=" | 
|  | 279 | | ">>=" | "<<=" | "\&=" | "\textasciicircum=" | "|="} | 
|  | 280 | \production{target} | 
|  | 281 | {\token{identifier} | 
|  | 282 | | "(" \token{target_list} ")" | 
|  | 283 | | "[" \token{target_list} "]" | 
|  | 284 | | \token{attributeref} | 
|  | 285 | | \token{subscription} | 
|  | 286 | | \token{slicing}} | 
|  | 287 | \end{productionlist} | 
| Fred Drake | 31f5550 | 2000-09-12 20:32:18 +0000 | [diff] [blame] | 288 |  | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 289 | (See section~\ref{primaries} for the syntax definitions for the last | 
| Fred Drake | 31f5550 | 2000-09-12 20:32:18 +0000 | [diff] [blame] | 290 | three symbols.) | 
|  | 291 |  | 
| Fred Drake | d68442b | 2000-09-21 22:01:36 +0000 | [diff] [blame] | 292 | An augmented assignment evaluates the target (which, unlike normal | 
|  | 293 | assignment statements, cannot be an unpacking) and the expression | 
|  | 294 | list, performs the binary operation specific to the type of assignment | 
|  | 295 | on the two operands, and assigns the result to the original | 
|  | 296 | target.  The target is only evaluated once. | 
| Fred Drake | 31f5550 | 2000-09-12 20:32:18 +0000 | [diff] [blame] | 297 |  | 
|  | 298 | An augmented assignment expression like \code{x += 1} can be rewritten as | 
|  | 299 | \code{x = x + 1} to achieve a similar, but not exactly equal effect. In the | 
|  | 300 | augmented version, \code{x} is only evaluated once. Also, when possible, the | 
|  | 301 | actual operation is performed \emph{in-place}, meaning that rather than | 
|  | 302 | creating a new object and assigning that to the target, the old object is | 
|  | 303 | modified instead. | 
|  | 304 |  | 
|  | 305 | With the exception of assigning to tuples and multiple targets in a single | 
|  | 306 | statement, the assignment done by augmented assignment statements is handled | 
|  | 307 | the same way as normal assignments. Similarly, with the exception of the | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 308 | possible \emph{in-place} behavior, the binary operation performed by | 
| Fred Drake | 31f5550 | 2000-09-12 20:32:18 +0000 | [diff] [blame] | 309 | augmented assignment is the same as the normal binary operations. | 
|  | 310 |  | 
|  | 311 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 312 | \section{The \keyword{pass} statement \label{pass}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 313 | \stindex{pass} | 
|  | 314 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 315 | \begin{productionlist} | 
|  | 316 | \production{pass_stmt} | 
|  | 317 | {"pass"} | 
|  | 318 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 319 |  | 
|  | 320 | \keyword{pass} is a null operation --- when it is executed, nothing | 
|  | 321 | happens.  It is useful as a placeholder when a statement is | 
|  | 322 | required syntactically, but no code needs to be executed, for example: | 
|  | 323 | \indexii{null}{operation} | 
|  | 324 |  | 
|  | 325 | \begin{verbatim} | 
|  | 326 | def f(arg): pass    # a function that does nothing (yet) | 
|  | 327 |  | 
|  | 328 | class C: pass       # a class with no methods (yet) | 
|  | 329 | \end{verbatim} | 
|  | 330 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 331 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 332 | \section{The \keyword{del} statement \label{del}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 333 | \stindex{del} | 
|  | 334 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 335 | \begin{productionlist} | 
|  | 336 | \production{del_stmt} | 
|  | 337 | {"del" \token{target_list}} | 
|  | 338 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 339 |  | 
|  | 340 | Deletion is recursively defined very similar to the way assignment is | 
|  | 341 | defined. Rather that spelling it out in full details, here are some | 
|  | 342 | hints. | 
|  | 343 | \indexii{deletion}{target} | 
|  | 344 | \indexiii{deletion}{target}{list} | 
|  | 345 |  | 
|  | 346 | Deletion of a target list recursively deletes each target, from left | 
|  | 347 | to right. | 
|  | 348 |  | 
|  | 349 | Deletion of a name removes the binding of that name (which must exist) | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 350 | from the local or global namespace, depending on whether the name | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 351 | occurs in a \keyword{global} statement in the same code block. | 
|  | 352 | \stindex{global} | 
|  | 353 | \indexii{unbinding}{name} | 
|  | 354 |  | 
|  | 355 | Deletion of attribute references, subscriptions and slicings | 
|  | 356 | is passed to the primary object involved; deletion of a slicing | 
|  | 357 | is in general equivalent to assignment of an empty slice of the | 
|  | 358 | right type (but even this is determined by the sliced object). | 
|  | 359 | \indexii{attribute}{deletion} | 
|  | 360 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 361 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 362 | \section{The \keyword{print} statement \label{print}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 363 | \stindex{print} | 
|  | 364 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 365 | \begin{productionlist} | 
|  | 366 | \production{print_stmt} | 
|  | 367 | {"print" ( \optional{\token{expression} ("," \token{expression})* \optional{","}} | 
|  | 368 | | ">\code{>}" \token{expression} | 
|  | 369 | \optional{("," \token{expression})+ \optional{","}})} | 
|  | 370 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 371 |  | 
| Fred Drake | d4c3352 | 1998-10-01 20:39:47 +0000 | [diff] [blame] | 372 | \keyword{print} evaluates each expression in turn and writes the | 
|  | 373 | resulting object to standard output (see below).  If an object is not | 
| Fred Drake | be9d10e | 2001-06-23 06:16:52 +0000 | [diff] [blame] | 374 | a string, it is first converted to a string using the rules for string | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 375 | conversions.  The (resulting or original) string is then written.  A | 
| Fred Drake | be9d10e | 2001-06-23 06:16:52 +0000 | [diff] [blame] | 376 | space is written before each object is (converted and) written, unless | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 377 | the output system believes it is positioned at the beginning of a | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 378 | line.  This is the case (1) when no characters have yet been written | 
|  | 379 | to standard output, (2) when the last character written to standard | 
| Fred Drake | d4c3352 | 1998-10-01 20:39:47 +0000 | [diff] [blame] | 380 | output is \character{\e n}, or (3) when the last write operation on | 
|  | 381 | standard output was not a \keyword{print} statement.  (In some cases | 
|  | 382 | it may be functional to write an empty string to standard output for | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 383 | this reason.)  \note{Objects which act like file objects but which are | 
|  | 384 | not the built-in file objects often do not properly emulate this | 
|  | 385 | aspect of the file object's behavior, so it is best not to rely on | 
|  | 386 | this.} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 387 | \index{output} | 
|  | 388 | \indexii{writing}{values} | 
|  | 389 |  | 
| Fred Drake | d4c3352 | 1998-10-01 20:39:47 +0000 | [diff] [blame] | 390 | A \character{\e n} character is written at the end, unless the | 
|  | 391 | \keyword{print} statement ends with a comma.  This is the only action | 
|  | 392 | if the statement contains just the keyword \keyword{print}. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 393 | \indexii{trailing}{comma} | 
|  | 394 | \indexii{newline}{suppression} | 
|  | 395 |  | 
| Fred Drake | dde91f0 | 1998-05-06 20:59:46 +0000 | [diff] [blame] | 396 | Standard output is defined as the file object named \code{stdout} | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 397 | in the built-in module \module{sys}.  If no such object exists, or if | 
|  | 398 | it does not have a \method{write()} method, a \exception{RuntimeError} | 
|  | 399 | exception is raised. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 400 | \indexii{standard}{output} | 
|  | 401 | \refbimodindex{sys} | 
| Fred Drake | 2b3730e | 1998-11-25 17:40:00 +0000 | [diff] [blame] | 402 | \withsubitem{(in module sys)}{\ttindex{stdout}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 403 | \exindex{RuntimeError} | 
|  | 404 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 405 | \keyword{print} also has an extended\index{extended print statement} | 
|  | 406 | form, defined by the second portion of the syntax described above. | 
|  | 407 | This form is sometimes referred to as ``\keyword{print} chevron.'' | 
| Fred Drake | 62effc1 | 2001-04-13 15:55:25 +0000 | [diff] [blame] | 408 | In this form, the first expression after the \code{>}\code{>} must | 
| Barry Warsaw | 8c0a242 | 2000-08-21 15:45:16 +0000 | [diff] [blame] | 409 | evaluate to a ``file-like'' object, specifically an object that has a | 
| Barry Warsaw | 33f785f | 2000-08-29 04:57:34 +0000 | [diff] [blame] | 410 | \method{write()} method as described above.  With this extended form, | 
|  | 411 | the subsequent expressions are printed to this file object.  If the | 
|  | 412 | first expression evaluates to \code{None}, then \code{sys.stdout} is | 
|  | 413 | used as the file for output. | 
| Barry Warsaw | 8c0a242 | 2000-08-21 15:45:16 +0000 | [diff] [blame] | 414 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 415 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 416 | \section{The \keyword{return} statement \label{return}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 417 | \stindex{return} | 
|  | 418 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 419 | \begin{productionlist} | 
|  | 420 | \production{return_stmt} | 
|  | 421 | {"return" [\token{expression_list}]} | 
|  | 422 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 423 |  | 
|  | 424 | \keyword{return} may only occur syntactically nested in a function | 
|  | 425 | definition, not within a nested class definition. | 
|  | 426 | \indexii{function}{definition} | 
|  | 427 | \indexii{class}{definition} | 
|  | 428 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 429 | If an expression list is present, it is evaluated, else \code{None} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 430 | is substituted. | 
|  | 431 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 432 | \keyword{return} leaves the current function call with the expression | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 433 | list (or \code{None}) as return value. | 
|  | 434 |  | 
|  | 435 | When \keyword{return} passes control out of a \keyword{try} statement | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 436 | with a \keyword{finally} clause, that \keyword{finally} clause is executed | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 437 | before really leaving the function. | 
|  | 438 | \kwindex{finally} | 
|  | 439 |  | 
| Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 440 | In a generator function, the \keyword{return} statement is not allowed | 
|  | 441 | to include an \grammartoken{expression_list}.  In that context, a bare | 
|  | 442 | \keyword{return} indicates that the generator is done and will cause | 
|  | 443 | \exception{StopIteration} to be raised. | 
|  | 444 |  | 
|  | 445 |  | 
|  | 446 | \section{The \keyword{yield} statement \label{yield}} | 
|  | 447 | \stindex{yield} | 
|  | 448 |  | 
|  | 449 | \begin{productionlist} | 
|  | 450 | \production{yield_stmt} | 
|  | 451 | {"yield" \token{expression_list}} | 
|  | 452 | \end{productionlist} | 
|  | 453 |  | 
|  | 454 | \index{generator!function} | 
|  | 455 | \index{generator!iterator} | 
|  | 456 | \index{function!generator} | 
|  | 457 | \exindex{StopIteration} | 
|  | 458 |  | 
|  | 459 | The \keyword{yield} statement is only used when defining a generator | 
|  | 460 | function, and is only used in the body of the generator function. | 
|  | 461 | Using a \keyword{yield} statement in a function definition is | 
|  | 462 | sufficient to cause that definition to create a generator function | 
|  | 463 | instead of a normal function. | 
|  | 464 |  | 
|  | 465 | When a generator function is called, it returns an iterator known as a | 
|  | 466 | generator iterator, or more commonly, a generator.  The body of the | 
|  | 467 | generator function is executed by calling the generator's | 
|  | 468 | \method{next()} method repeatedly until it raises an exception. | 
|  | 469 |  | 
|  | 470 | When a \keyword{yield} statement is executed, the state of the | 
|  | 471 | generator is frozen and the value of \grammartoken{expression_list} is | 
|  | 472 | returned to \method{next()}'s caller.  By ``frozen'' we mean that all | 
|  | 473 | local state is retained, including the current bindings of local | 
|  | 474 | variables, the instruction pointer, and the internal evaluation stack: | 
|  | 475 | enough information is saved so that the next time \method{next()} is | 
|  | 476 | invoked, the function can proceed exactly as if the \keyword{yield} | 
|  | 477 | statement were just another external call. | 
|  | 478 |  | 
| Fred Drake | 3a8e59e | 2001-12-11 21:58:35 +0000 | [diff] [blame] | 479 | The \keyword{yield} statement is not allowed in the \keyword{try} | 
|  | 480 | clause of a \keyword{try} ...\ \keyword{finally} construct.  The | 
|  | 481 | difficulty is that there's no guarantee the generator will ever be | 
|  | 482 | resumed, hence no guarantee that the \keyword{finally} block will ever | 
|  | 483 | get executed. | 
| Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 484 |  | 
| Fred Drake | 8d0645c | 2001-12-12 06:06:43 +0000 | [diff] [blame^] | 485 | \note{In Python 2.2, the \keyword{yield} statement is only allowed | 
|  | 486 | when the \code{generators} feature has been enabled.  It will always | 
|  | 487 | be enabled in Python 2.3.  This \code{__future__} import statment can | 
|  | 488 | be used to enable the feature:} | 
|  | 489 |  | 
|  | 490 | \begin{verbatim} | 
|  | 491 | from __future__ import generators | 
|  | 492 | \end{verbatim} | 
|  | 493 |  | 
|  | 494 |  | 
| Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 495 | \begin{seealso} | 
|  | 496 | \seepep{0255}{Simple Generators} | 
|  | 497 | {The proposal for adding generators and the \keyword{yield} | 
|  | 498 | statement to Python.} | 
|  | 499 | \end{seealso} | 
|  | 500 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 501 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 502 | \section{The \keyword{raise} statement \label{raise}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 503 | \stindex{raise} | 
|  | 504 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 505 | \begin{productionlist} | 
|  | 506 | \production{raise_stmt} | 
|  | 507 | {"raise" [\token{expression} ["," \token{expression} | 
|  | 508 | ["," \token{expression}]]]} | 
|  | 509 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 510 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 511 | If no expressions are present, \keyword{raise} re-raises the last | 
|  | 512 | expression that was raised in the current scope. | 
|  | 513 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 514 | Otherwise, \keyword{raise} evaluates its first expression, which must yield | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 515 | a string, class, or instance object.  If there is a second expression, | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 516 | this is evaluated, else \code{None} is substituted.  If the first | 
| Guido van Rossum | f5a80a4 | 1998-08-25 14:45:41 +0000 | [diff] [blame] | 517 | expression is a class object, then the second expression may be an | 
|  | 518 | instance of that class or one of its derivatives, and then that | 
|  | 519 | instance is raised.  If the second expression is not such an instance, | 
|  | 520 | the given class is instantiated.  The argument list for the | 
|  | 521 | instantiation is determined as follows: if the second expression is a | 
|  | 522 | tuple, it is used as the argument list; if it is \code{None}, the | 
|  | 523 | argument list is empty; otherwise, the argument list consists of a | 
|  | 524 | single argument which is the second expression.  If the first | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 525 | expression is an instance object, the second expression must be | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 526 | \code{None}. | 
|  | 527 | \index{exception} | 
|  | 528 | \indexii{raising}{exception} | 
|  | 529 |  | 
| Guido van Rossum | f5a80a4 | 1998-08-25 14:45:41 +0000 | [diff] [blame] | 530 | If the first object is a string, it then raises the exception | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 531 | identified by the first object, with the second one (or \code{None}) | 
| Guido van Rossum | f5a80a4 | 1998-08-25 14:45:41 +0000 | [diff] [blame] | 532 | as its parameter.  If the first object is a class or instance, | 
|  | 533 | it raises the exception identified by the class of the instance | 
|  | 534 | determined in the previous step, with the instance as | 
|  | 535 | its parameter. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 536 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 537 | If a third object is present, and it is not \code{None}, it should be | 
| Fred Drake | c2f496a | 2001-12-05 05:46:25 +0000 | [diff] [blame] | 538 | a traceback object (see section~\ref{traceback}), and it is | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 539 | substituted instead of the current location as the place where the | 
|  | 540 | exception occurred.  This is useful to re-raise an exception | 
|  | 541 | transparently in an except clause. | 
|  | 542 | \obindex{traceback} | 
|  | 543 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 544 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 545 | \section{The \keyword{break} statement \label{break}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 546 | \stindex{break} | 
|  | 547 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 548 | \begin{productionlist} | 
|  | 549 | \production{break_stmt} | 
|  | 550 | {"break"} | 
|  | 551 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 552 |  | 
|  | 553 | \keyword{break} may only occur syntactically nested in a \keyword{for} | 
|  | 554 | or \keyword{while} loop, but not nested in a function or class definition | 
|  | 555 | within that loop. | 
|  | 556 | \stindex{for} | 
|  | 557 | \stindex{while} | 
|  | 558 | \indexii{loop}{statement} | 
|  | 559 |  | 
|  | 560 | It terminates the nearest enclosing loop, skipping the optional | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 561 | \keyword{else} clause if the loop has one. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 562 | \kwindex{else} | 
|  | 563 |  | 
|  | 564 | If a \keyword{for} loop is terminated by \keyword{break}, the loop control | 
|  | 565 | target keeps its current value. | 
|  | 566 | \indexii{loop control}{target} | 
|  | 567 |  | 
|  | 568 | When \keyword{break} passes control out of a \keyword{try} statement | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 569 | with a \keyword{finally} clause, that \keyword{finally} clause is executed | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 570 | before really leaving the loop. | 
|  | 571 | \kwindex{finally} | 
|  | 572 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 573 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 574 | \section{The \keyword{continue} statement \label{continue}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 575 | \stindex{continue} | 
|  | 576 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 577 | \begin{productionlist} | 
|  | 578 | \production{continue_stmt} | 
|  | 579 | {"continue"} | 
|  | 580 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 581 |  | 
|  | 582 | \keyword{continue} may only occur syntactically nested in a \keyword{for} or | 
|  | 583 | \keyword{while} loop, but not nested in a function or class definition or | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 584 | \keyword{try} statement within that loop.\footnote{It may | 
|  | 585 | occur within an \keyword{except} or \keyword{else} clause.  The | 
| Thomas Wouters | f9b526d | 2000-07-16 19:05:38 +0000 | [diff] [blame] | 586 | restriction on occurring in the \keyword{try} clause is implementor's | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 587 | laziness and will eventually be lifted.} | 
|  | 588 | It continues with the next cycle of the nearest enclosing loop. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 589 | \stindex{for} | 
|  | 590 | \stindex{while} | 
|  | 591 | \indexii{loop}{statement} | 
|  | 592 | \kwindex{finally} | 
|  | 593 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 594 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 595 | \section{The \keyword{import} statement \label{import}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 596 | \stindex{import} | 
|  | 597 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 598 | \begin{productionlist} | 
|  | 599 | \production{import_stmt} | 
|  | 600 | {"import" \token{module} ["as" \token{name}] | 
|  | 601 | ( "," \token{module} ["as" \token{name}] )* | 
|  | 602 | | "from" \token{module} "import" \token{identifier} | 
|  | 603 | ["as" \token{name}] | 
|  | 604 | ( "," \token{identifier} ["as" \token{name}] )* | 
|  | 605 | | "from" \token{module} "import" "*"} | 
|  | 606 | \production{module} | 
|  | 607 | {(\token{identifier} ".")* \token{identifier}} | 
|  | 608 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 609 |  | 
|  | 610 | Import statements are executed in two steps: (1) find a module, and | 
|  | 611 | initialize it if necessary; (2) define a name or names in the local | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 612 | namespace (of the scope where the \keyword{import} statement occurs). | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 613 | The first form (without \keyword{from}) repeats these steps for each | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 614 | identifier in the list.  The form with \keyword{from} performs step | 
|  | 615 | (1) once, and then performs step (2) repeatedly. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 616 | \indexii{importing}{module} | 
|  | 617 | \indexii{name}{binding} | 
|  | 618 | \kwindex{from} | 
| Guido van Rossum | b1f97d6 | 1998-12-21 18:57:36 +0000 | [diff] [blame] | 619 | % XXX Need to define what ``initialize'' means here | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 620 |  | 
|  | 621 | The system maintains a table of modules that have been initialized, | 
| Fred Drake | 191a282 | 2000-07-06 00:50:42 +0000 | [diff] [blame] | 622 | indexed by module name.  This table is | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 623 | accessible as \code{sys.modules}.  When a module name is found in | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 624 | this table, step (1) is finished.  If not, a search for a module | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 625 | definition is started.  When a module is found, it is loaded.  Details | 
|  | 626 | of the module searching and loading process are implementation and | 
|  | 627 | platform specific.  It generally involves searching for a ``built-in'' | 
|  | 628 | module with the given name and then searching a list of locations | 
|  | 629 | given as \code{sys.path}. | 
| Fred Drake | 2b3730e | 1998-11-25 17:40:00 +0000 | [diff] [blame] | 630 | \withsubitem{(in module sys)}{\ttindex{modules}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 631 | \ttindex{sys.modules} | 
|  | 632 | \indexii{module}{name} | 
|  | 633 | \indexii{built-in}{module} | 
|  | 634 | \indexii{user-defined}{module} | 
|  | 635 | \refbimodindex{sys} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 636 | \indexii{filename}{extension} | 
| Fred Drake | dde91f0 | 1998-05-06 20:59:46 +0000 | [diff] [blame] | 637 | \indexiii{module}{search}{path} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 638 |  | 
|  | 639 | If a built-in module is found, its built-in initialization code is | 
|  | 640 | executed and step (1) is finished.  If no matching file is found, | 
|  | 641 | \exception{ImportError} is raised.  If a file is found, it is parsed, | 
|  | 642 | yielding an executable code block.  If a syntax error occurs, | 
|  | 643 | \exception{SyntaxError} is raised.  Otherwise, an empty module of the given | 
|  | 644 | name is created and inserted in the module table, and then the code | 
|  | 645 | block is executed in the context of this module.  Exceptions during | 
|  | 646 | this execution terminate step (1). | 
|  | 647 | \indexii{module}{initialization} | 
|  | 648 | \exindex{SyntaxError} | 
|  | 649 | \exindex{ImportError} | 
|  | 650 | \index{code block} | 
|  | 651 |  | 
|  | 652 | When step (1) finishes without raising an exception, step (2) can | 
|  | 653 | begin. | 
|  | 654 |  | 
| Fred Drake | 859eb62 | 2001-03-06 07:34:00 +0000 | [diff] [blame] | 655 | The first form of \keyword{import} statement binds the module name in | 
|  | 656 | the local namespace to the module object, and then goes on to import | 
|  | 657 | the next identifier, if any.  If the module name is followed by | 
|  | 658 | \keyword{as}, the name following \keyword{as} is used as the local | 
|  | 659 | name for the module. To avoid confusion, you cannot import modules | 
|  | 660 | with dotted names \keyword{as} a different local name. So \code{import | 
|  | 661 | module as m} is legal, but \code{import module.submod as s} is not. | 
|  | 662 | The latter should be written as \code{from module import submod as s}; | 
| Thomas Wouters | 8bad612 | 2000-08-19 20:55:02 +0000 | [diff] [blame] | 663 | see below. | 
|  | 664 |  | 
| Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 665 | The \keyword{from} form does not bind the module name: it goes through the | 
|  | 666 | list of identifiers, looks each one of them up in the module found in step | 
|  | 667 | (1), and binds the name in the local namespace to the object thus found. | 
| Fred Drake | d68442b | 2000-09-21 22:01:36 +0000 | [diff] [blame] | 668 | As with the first form of \keyword{import}, an alternate local name can be | 
| Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 669 | supplied by specifying "\keyword{as} localname".  If a name is not found, | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 670 | \exception{ImportError} is raised.  If the list of identifiers is replaced | 
| Fred Drake | 08fd515 | 2001-10-24 19:50:31 +0000 | [diff] [blame] | 671 | by a star (\character{*}), all public names defined in the module are | 
|  | 672 | bound in the local namespace of the \keyword{import} statement.. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 673 | \indexii{name}{binding} | 
|  | 674 | \exindex{ImportError} | 
|  | 675 |  | 
| Fred Drake | 08fd515 | 2001-10-24 19:50:31 +0000 | [diff] [blame] | 676 | The \emph{public names} defined by a module are determined by checking | 
|  | 677 | the module's namespace for a variable named \code{__all__}; if | 
|  | 678 | defined, it must be a sequence of strings which are names defined or | 
|  | 679 | imported by that module.  The names given in \code{__all__} are all | 
|  | 680 | considered public and are required to exist.  If \code{__all__} is not | 
|  | 681 | defined, the set of public names includes all names found in the | 
|  | 682 | module's namespace which do not begin with an underscore character | 
|  | 683 | (\character{_}). | 
|  | 684 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 685 | Names bound by \keyword{import} statements may not occur in | 
|  | 686 | \keyword{global} statements in the same scope. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 687 | \stindex{global} | 
|  | 688 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 689 | The \keyword{from} form with \samp{*} may only occur in a module scope. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 690 | \kwindex{from} | 
| Fred Drake | 2b3730e | 1998-11-25 17:40:00 +0000 | [diff] [blame] | 691 | \stindex{from} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 692 |  | 
| Fred Drake | 246837d | 1998-07-24 20:28:22 +0000 | [diff] [blame] | 693 | \strong{Hierarchical module names:}\indexiii{hierarchical}{module}{names} | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 694 | when the module names contains one or more dots, the module search | 
|  | 695 | path is carried out differently.  The sequence of identifiers up to | 
|  | 696 | the last dot is used to find a ``package''\index{packages}; the final | 
|  | 697 | identifier is then searched inside the package.  A package is | 
|  | 698 | generally a subdirectory of a directory on \code{sys.path} that has a | 
|  | 699 | file \file{__init__.py}.\ttindex{__init__.py} | 
|  | 700 | % | 
|  | 701 | [XXX Can't be bothered to spell this out right now; see the URL | 
| Fred Drake | 1a0b872 | 1998-08-07 17:40:20 +0000 | [diff] [blame] | 702 | \url{http://www.python.org/doc/essays/packages.html} for more details, also | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 703 | about how the module search works from inside a package.] | 
|  | 704 |  | 
| Fred Drake | 08fd515 | 2001-10-24 19:50:31 +0000 | [diff] [blame] | 705 | The built-in function \function{__import__()} is provided to support | 
|  | 706 | applications that determine which modules need to be loaded | 
|  | 707 | dynamically; refer to \ulink{Built-in | 
|  | 708 | Functions}{../lib/built-in-funcs.html} in the | 
|  | 709 | \citetitle[../lib/lib.html]{Python Library Reference} for additional | 
|  | 710 | information. | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 711 | \bifuncindex{__import__} | 
|  | 712 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 713 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 714 | \section{The \keyword{global} statement \label{global}} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 715 | \stindex{global} | 
|  | 716 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 717 | \begin{productionlist} | 
|  | 718 | \production{global_stmt} | 
|  | 719 | {"global" \token{identifier} ("," \token{identifier})*} | 
|  | 720 | \end{productionlist} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 721 |  | 
|  | 722 | The \keyword{global} statement is a declaration which holds for the | 
|  | 723 | entire current code block.  It means that the listed identifiers are to be | 
| Fred Drake | dde91f0 | 1998-05-06 20:59:46 +0000 | [diff] [blame] | 724 | interpreted as globals.  While \emph{using} global names is automatic | 
|  | 725 | if they are not defined in the local scope, \emph{assigning} to global | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 726 | names would be impossible without \keyword{global}. | 
|  | 727 | \indexiii{global}{name}{binding} | 
|  | 728 |  | 
|  | 729 | Names listed in a \keyword{global} statement must not be used in the same | 
| Guido van Rossum | b1f97d6 | 1998-12-21 18:57:36 +0000 | [diff] [blame] | 730 | code block textually preceding that \keyword{global} statement. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 731 |  | 
|  | 732 | Names listed in a \keyword{global} statement must not be defined as formal | 
|  | 733 | parameters or in a \keyword{for} loop control target, \keyword{class} | 
|  | 734 | definition, function definition, or \keyword{import} statement. | 
|  | 735 |  | 
|  | 736 | (The current implementation does not enforce the latter two | 
|  | 737 | restrictions, but programs should not abuse this freedom, as future | 
|  | 738 | implementations may enforce them or silently change the meaning of the | 
|  | 739 | program.) | 
|  | 740 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 741 | \strong{Programmer's note:} | 
|  | 742 | the \keyword{global} is a directive to the parser.  It | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 743 | applies only to code parsed at the same time as the \keyword{global} | 
|  | 744 | statement.  In particular, a \keyword{global} statement contained in an | 
| Fred Drake | dde91f0 | 1998-05-06 20:59:46 +0000 | [diff] [blame] | 745 | \keyword{exec} statement does not affect the code block \emph{containing} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 746 | the \keyword{exec} statement, and code contained in an \keyword{exec} | 
|  | 747 | statement is unaffected by \keyword{global} statements in the code | 
|  | 748 | containing the \keyword{exec} statement.  The same applies to the | 
|  | 749 | \function{eval()}, \function{execfile()} and \function{compile()} functions. | 
|  | 750 | \stindex{exec} | 
|  | 751 | \bifuncindex{eval} | 
|  | 752 | \bifuncindex{execfile} | 
|  | 753 | \bifuncindex{compile} | 
| Guido van Rossum | 5f574aa | 1998-07-06 13:18:39 +0000 | [diff] [blame] | 754 |  | 
| Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 755 |  | 
| Fred Drake | 011f6fc | 1999-04-14 12:52:14 +0000 | [diff] [blame] | 756 | \section{The \keyword{exec} statement \label{exec}} | 
| Guido van Rossum | 5f574aa | 1998-07-06 13:18:39 +0000 | [diff] [blame] | 757 | \stindex{exec} | 
|  | 758 |  | 
| Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 759 | \begin{productionlist} | 
|  | 760 | \production{exec_stmt} | 
|  | 761 | {"exec" \token{expression} | 
|  | 762 | ["in" \token{expression} ["," \token{expression}]]} | 
|  | 763 | \end{productionlist} | 
| Guido van Rossum | 5f574aa | 1998-07-06 13:18:39 +0000 | [diff] [blame] | 764 |  | 
|  | 765 | This statement supports dynamic execution of Python code.  The first | 
|  | 766 | expression should evaluate to either a string, an open file object, or | 
|  | 767 | a code object.  If it is a string, the string is parsed as a suite of | 
|  | 768 | Python statements which is then executed (unless a syntax error | 
| Fred Drake | 93852ef | 2001-06-23 06:06:52 +0000 | [diff] [blame] | 769 | occurs).  If it is an open file, the file is parsed until \EOF{} and | 
| Guido van Rossum | 5f574aa | 1998-07-06 13:18:39 +0000 | [diff] [blame] | 770 | executed.  If it is a code object, it is simply executed. | 
|  | 771 |  | 
|  | 772 | In all cases, if the optional parts are omitted, the code is executed | 
|  | 773 | in the current scope.  If only the first expression after \keyword{in} | 
|  | 774 | is specified, it should be a dictionary, which will be used for both | 
|  | 775 | the global and the local variables.  If two expressions are given, | 
|  | 776 | both must be dictionaries and they are used for the global and local | 
|  | 777 | variables, respectively. | 
|  | 778 |  | 
|  | 779 | As a side effect, an implementation may insert additional keys into | 
|  | 780 | the dictionaries given besides those corresponding to variable names | 
|  | 781 | set by the executed code.  For example, the current implementation | 
|  | 782 | may add a reference to the dictionary of the built-in module | 
|  | 783 | \module{__builtin__} under the key \code{__builtins__} (!). | 
|  | 784 | \ttindex{__builtins__} | 
|  | 785 | \refbimodindex{__builtin__} | 
|  | 786 |  | 
| Guido van Rossum | 56c2013 | 1998-07-24 18:25:38 +0000 | [diff] [blame] | 787 | \strong{Programmer's hints:} | 
|  | 788 | dynamic evaluation of expressions is supported by the built-in | 
| Guido van Rossum | 5f574aa | 1998-07-06 13:18:39 +0000 | [diff] [blame] | 789 | function \function{eval()}.  The built-in functions | 
|  | 790 | \function{globals()} and \function{locals()} return the current global | 
|  | 791 | and local dictionary, respectively, which may be useful to pass around | 
|  | 792 | for use by \keyword{exec}. | 
|  | 793 | \bifuncindex{eval} | 
|  | 794 | \bifuncindex{globals} | 
|  | 795 | \bifuncindex{locals} | 
| Greg Ward | 38c28e3 | 2000-04-27 18:32:02 +0000 | [diff] [blame] | 796 |  | 
|  | 797 | Also, in the current implementation, multi-line compound statements must | 
|  | 798 | end with a newline: | 
|  | 799 | \code{exec "for v in seq:\e{}n\e{}tprint v\e{}n"} works, but | 
|  | 800 | \code{exec "for v in seq:\e{}n\e{}tprint v"} fails with | 
|  | 801 | \exception{SyntaxError}. | 
|  | 802 | \exindex{SyntaxError} | 
|  | 803 |  | 
|  | 804 |  |