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