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