Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1 | % Format this file with latex. |
| 2 | |
| 3 | \documentstyle[myformat]{report} |
| 4 | |
| 5 | \title{\bf |
| 6 | Python Reference Manual \\ |
| 7 | {\em Incomplete Draft} |
| 8 | } |
| 9 | |
| 10 | \author{ |
| 11 | Guido van Rossum \\ |
| 12 | Dept. CST, CWI, Kruislaan 413 \\ |
| 13 | 1098 SJ Amsterdam, The Netherlands \\ |
| 14 | E-mail: {\tt guido@cwi.nl} |
| 15 | } |
| 16 | |
| 17 | \begin{document} |
| 18 | |
| 19 | \pagenumbering{roman} |
| 20 | |
| 21 | \maketitle |
| 22 | |
| 23 | \begin{abstract} |
| 24 | |
| 25 | \noindent |
| 26 | Python is a simple, yet powerful programming language that bridges the |
| 27 | gap between C and shell programming, and is thus ideally suited for |
| 28 | ``throw-away programming'' |
| 29 | and rapid prototyping. Its syntax is put |
| 30 | together from constructs borrowed from a variety of other languages; |
| 31 | most prominent are influences from ABC, C, Modula-3 and Icon. |
| 32 | |
| 33 | The Python interpreter is easily extended with new functions and data |
| 34 | types implemented in C. Python is also suitable as an extension |
| 35 | language for highly customizable C applications such as editors or |
| 36 | window managers. |
| 37 | |
| 38 | Python is available for various operating systems, amongst which |
| 39 | several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S., |
| 40 | and MS-DOS. |
| 41 | |
| 42 | This reference manual describes the syntax and ``core semantics'' of |
| 43 | the language. It is terse, but exact and complete. The semantics of |
| 44 | non-essential built-in object types and of the built-in functions and |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 45 | modules are described in the {\em Python Library Reference}. For an |
| 46 | informal introduction to the language, see the {\em Python Tutorial}. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 47 | |
| 48 | \end{abstract} |
| 49 | |
| 50 | \pagebreak |
| 51 | |
| 52 | \tableofcontents |
| 53 | |
| 54 | \pagebreak |
| 55 | |
| 56 | \pagenumbering{arabic} |
| 57 | |
| 58 | \chapter{Introduction} |
| 59 | |
| 60 | This reference manual describes the Python programming language. |
| 61 | It is not intended as a tutorial. |
| 62 | |
| 63 | \chapter{Lexical analysis} |
| 64 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 65 | A Python program is read by a {\em parser}. Input to the parser is a |
| 66 | stream of {\em tokens}, generated by the {\em lexical analyzer}. This |
| 67 | chapter describes how the lexical analyzer breaks a file into tokens. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 68 | |
| 69 | \section{Line structure} |
| 70 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 71 | A Python program is divided in a number of logical lines. Statements |
| 72 | do not straddle logical line boundaries except where explicitly |
| 73 | indicated by the syntax (i.e., for compound statements). To this |
| 74 | purpose, the end of a logical line is represented by the token |
| 75 | NEWLINE. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 76 | |
| 77 | \subsection{Comments} |
| 78 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 79 | A comment starts with a hash character (\verb\#\) that is not part of |
| 80 | a string literal, and ends at the end of the physical line. Comments |
| 81 | are ignored by the syntax. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 82 | |
| 83 | \subsection{Line joining} |
| 84 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 85 | Two or more physical lines may be joined into logical lines using |
| 86 | backslash characters (\verb/\/), as follows: When physical line ends |
| 87 | in a backslash that is not part of a string literal or comment, it is |
| 88 | joined with the following forming a single logical line, deleting the |
| 89 | backslash and the following end-of-line character. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 90 | |
| 91 | \subsection{Blank lines} |
| 92 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 93 | A logical line that contains only spaces, tabs, and possibly a |
| 94 | comment, is ignored (i.e., no NEWLINE token is generated), except that |
| 95 | during interactive input of statements, an entirely blank logical line |
| 96 | terminates a multi-line statement. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 97 | |
| 98 | \subsection{Indentation} |
| 99 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 100 | Spaces and tabs at the beginning of a logical line are used to compute |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 101 | the indentation level of the line, which in turn is used to determine |
| 102 | the grouping of statements. |
| 103 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 104 | First, each tab is replaced by one to eight spaces such that the total |
| 105 | number of spaces up to that point is a multiple of eight. The total |
| 106 | number of spaces preceding the first non-blank character then |
| 107 | determines the line's indentation. Indentation cannot be split over |
| 108 | multiple physical lines using backslashes. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 109 | |
| 110 | The indentation levels of consecutive lines are used to generate |
| 111 | INDENT and DEDENT tokens, using a stack, as follows. |
| 112 | |
| 113 | Before the first line of the file is read, a single zero is pushed on |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 114 | the stack; this will never be popped off again. The numbers pushed on |
| 115 | the stack will always be strictly increasing from bottom to top. At |
| 116 | the beginning of each logical line, the line's indentation level is |
| 117 | compared to the top of the stack. If it is equal, nothing happens. |
| 118 | If it larger, it is pushed on the stack, and one INDENT token is |
| 119 | generated. If it is smaller, it {\em must} be one of the numbers |
| 120 | occurring on the stack; all numbers on the stack that are larger are |
| 121 | popped off, and for each number popped off a DEDENT token is |
| 122 | generated. At the end of the file, a DEDENT token is generated for |
| 123 | each number remaining on the stack that is larger than zero. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 124 | |
| 125 | \section{Other tokens} |
| 126 | |
| 127 | Besides NEWLINE, INDENT and DEDENT, the following categories of tokens |
| 128 | exist: identifiers, keywords, literals, operators, and delimiters. |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 129 | Spaces and tabs are not tokens, but serve to delimit tokens. Where |
| 130 | ambiguity exists, a token comprises the longest possible string that |
| 131 | forms a legal token, when read from left to right. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 132 | |
| 133 | Tokens are described using an extended regular expression notation. |
| 134 | This is similar to the extended BNF notation used later, except that |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 135 | the notation \verb\<...>\ is used to give an informal description of a |
| 136 | character, and that spaces and tabs are not to be ignored. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 137 | |
| 138 | \section{Identifiers} |
| 139 | |
| 140 | Identifiers are described by the following regular expressions: |
| 141 | |
| 142 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 143 | identifier: (letter|"_") (letter|digit|"_")* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 144 | letter: lowercase | uppercase |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 145 | lowercase: "a"|"b"|...|"z" |
| 146 | uppercase: "A"|"B"|...|"Z" |
| 147 | digit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 148 | \end{verbatim} |
| 149 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 150 | Identifiers are unlimited in length. Case is significant. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 151 | |
| 152 | \section{Keywords} |
| 153 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 154 | The following identifiers are used as reserved words, or {\em |
| 155 | keywords} of the language, and may not be used as ordinary |
| 156 | identifiers. They must be spelled exactly as written here: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 158 | \begin{verbatim} |
| 159 | and del for is raise |
| 160 | break elif from not return |
| 161 | class else if or try |
| 162 | continue except import pass while |
| 163 | def finally in print |
| 164 | \end{verbatim} |
| 165 | |
| 166 | % import string |
| 167 | % l = [] |
| 168 | % try: |
| 169 | % while 1: |
| 170 | % l = l + string.split(raw_input()) |
| 171 | % except EOFError: |
| 172 | % pass |
| 173 | % l.sort() |
| 174 | % for i in range((len(l)+4)/5): |
| 175 | % for j in range(i, len(l), 5): |
| 176 | % print string.ljust(l[j], 10), |
| 177 | % print |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 178 | |
| 179 | \section{Literals} |
| 180 | |
| 181 | \subsection{String literals} |
| 182 | |
| 183 | String literals are described by the following regular expressions: |
| 184 | |
| 185 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 186 | stringliteral: "'" stringitem* "'" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 187 | stringitem: stringchar | escapeseq |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 188 | stringchar: <any character except newline or "\" or "'"> |
| 189 | escapeseq: "'" <any character except newline> |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 190 | \end{verbatim} |
| 191 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 192 | String literals cannot span physical line boundaries. Escape |
| 193 | sequences in strings are actually interpreted according to rules |
| 194 | simular to those used by Standard C. The recognized escape sequences |
| 195 | are: |
| 196 | |
| 197 | \begin{center} |
| 198 | \begin{tabular}{|l|l|} |
| 199 | \hline |
| 200 | \verb/\\/ & Backslash (\verb/\/) \\ |
| 201 | \verb/\'/ & Single quote (\verb/'/) \\ |
| 202 | \verb/\a/ & ASCII Bell (BEL) \\ |
| 203 | \verb/\b/ & ASCII Backspace (BS) \\ |
| 204 | \verb/\E/ & ASCII Escape (ESC) \\ |
| 205 | \verb/\f/ & ASCII Formfeed (FF) \\ |
| 206 | \verb/\n/ & ASCII Linefeed (LF) \\ |
| 207 | \verb/\r/ & ASCII Carriage Return (CR) \\ |
| 208 | \verb/\t/ & ASCII Horizontal Tab (TAB) \\ |
| 209 | \verb/\v/ & ASCII Vertical Tab (VT) \\ |
| 210 | \verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\ |
| 211 | \verb/\x/{em xx...} & ASCII character with hex value {\em xx} \\ |
| 212 | \hline |
| 213 | \end{tabular} |
| 214 | \end{center} |
| 215 | |
| 216 | For compatibility with in Standard C, up to three octal digits are |
| 217 | accepted, but an unlimited number of hex digits is taken to be part of |
| 218 | the hex escape (and then the lower 8 bits of the resulting hex number |
| 219 | are used...). |
| 220 | |
| 221 | All unrecognized escape sequences are left in the string {\em |
| 222 | unchanged}, i.e., the backslash is left in the string. (This rule is |
| 223 | useful when debugging: if an escape sequence is mistyped, the |
| 224 | resulting output is more easily recognized as broken. It also helps |
| 225 | somewhat for string literals used as regular expressions or otherwise |
| 226 | passed to other modules that do their own escape handling.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 227 | |
| 228 | \subsection{Numeric literals} |
| 229 | |
| 230 | There are three types of numeric literals: integers, long integers, |
| 231 | and floating point numbers. |
| 232 | |
| 233 | Integers and long integers are described by the following regular expressions: |
| 234 | |
| 235 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 236 | longinteger: integer ("l"|"L") |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 237 | integer: decimalinteger | octinteger | hexinteger |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 238 | decimalinteger: nonzerodigit digit* | "0" |
| 239 | octinteger: "0" octdigit+ |
| 240 | hexinteger: "0" ("x"|"X") hexdigit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 242 | nonzerodigit: "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" |
| 243 | octdigit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7" |
| 244 | hexdigit: digit|"a"|"b"|"c"|"d"|"e"|"f"|"A"|"B"|"C"|"D"|"E"|"F" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 245 | \end{verbatim} |
| 246 | |
| 247 | Floating point numbers are described by the following regular expressions: |
| 248 | |
| 249 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 250 | floatnumber: [intpart] fraction [exponent] | intpart ["."] exponent |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 251 | intpart: digit+ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 252 | fraction: "." digit+ |
| 253 | exponent: ("e"|"E") ["+"|"-"] digit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 254 | \end{verbatim} |
| 255 | |
| 256 | \section{Operators} |
| 257 | |
| 258 | The following tokens are operators: |
| 259 | |
| 260 | \begin{verbatim} |
| 261 | + - * / % |
| 262 | << >> & | ^ ~ |
| 263 | < = == > <= <> != >= |
| 264 | \end{verbatim} |
| 265 | |
| 266 | \section{Delimiters} |
| 267 | |
| 268 | The following tokens are delimiters: |
| 269 | |
| 270 | \begin{verbatim} |
| 271 | ( ) [ ] { } |
| 272 | ; , : . ` |
| 273 | \end{verbatim} |
| 274 | |
| 275 | The following printing ASCII characters are currently not used; |
| 276 | their occurrence is an unconditional error: |
| 277 | |
| 278 | \begin{verbatim} |
| 279 | ! @ $ " ? |
| 280 | \end{verbatim} |
| 281 | |
| 282 | \chapter{Execution model} |
| 283 | |
| 284 | (XXX This chapter should explain the general model |
| 285 | of the execution of Python code and |
| 286 | the evaluation of expressions. |
| 287 | It should introduce objects, values, code blocks, scopes, name spaces, |
| 288 | name binding, |
| 289 | types, sequences, numbers, mappings, |
| 290 | exceptions, and other technical terms needed to make the following |
| 291 | chapters concise and exact.) |
| 292 | |
| 293 | \chapter{Expressions and conditions} |
| 294 | |
| 295 | (From now on, extended BNF notation will be used to describe |
| 296 | syntax, not lexical analysis.) |
| 297 | (XXX Explain the notation.) |
| 298 | |
| 299 | This chapter explains the meaning of the elements of expressions and |
| 300 | conditions. Conditions are a superset of expressions, and a condition |
| 301 | may be used where an expression is required by enclosing it in |
| 302 | parentheses. The only place where an unparenthesized condition |
| 303 | is not allowed is on the right-hand side of the assignment operator, |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 304 | because this operator is the same token (\verb\=\) as used for |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 305 | compasisons. |
| 306 | |
| 307 | The comma plays a somewhat special role in Python's syntax. |
| 308 | It is an operator with a lower precedence than all others, but |
| 309 | occasionally serves other purposes as well (e.g., it has special |
| 310 | semantics in print statements). When a comma is accepted by the |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 311 | syntax, one of the syntactic categories \verb\expression_list\ |
| 312 | or \verb\condition_list\ is always used. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 313 | |
| 314 | When (one alternative of) a syntax rule has the form |
| 315 | |
| 316 | \begin{verbatim} |
| 317 | name: othername |
| 318 | \end{verbatim} |
| 319 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 320 | and no semantics are given, the semantics of this form of \verb\name\ |
| 321 | are the same as for \verb\othername\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 322 | |
| 323 | \section{Arithmetic conversions} |
| 324 | |
| 325 | When a description of an arithmetic operator below uses the phrase |
| 326 | ``the numeric arguments are converted to a common type'', |
| 327 | this both means that if either argument is not a number, a |
| 328 | {\tt TypeError} exception is raised, and that otherwise |
| 329 | the following conversions are applied: |
| 330 | |
| 331 | \begin{itemize} |
| 332 | \item First, if either argument is a floating point number, |
| 333 | the other is converted to floating point; |
| 334 | \item else, if either argument is a long integer, |
| 335 | the other is converted to long integer; |
| 336 | \item otherwise, both must be short integers and no conversion |
| 337 | is necessary. |
| 338 | \end{itemize} |
| 339 | |
| 340 | (Note: ``short integers'' in Python are at least 32 bits in size; |
| 341 | ``long integers'' are arbitrary precision integers.) |
| 342 | |
| 343 | \section{Atoms} |
| 344 | |
| 345 | Atoms are the most basic elements of expressions. |
| 346 | Forms enclosed in reverse quotes or various types of parentheses |
| 347 | or braces are also categorized syntactically as atoms. |
| 348 | Syntax rules for atoms: |
| 349 | |
| 350 | \begin{verbatim} |
| 351 | atom: identifier | literal | parenth_form | string_conversion |
| 352 | literal: stringliteral | integer | longinteger | floatnumber |
| 353 | parenth_form: enclosure | list_display | dict_display |
| 354 | enclosure: '(' [condition_list] ')' |
| 355 | list_display: '[' [condition_list] ']' |
| 356 | dict_display: '{' [key_datum (',' key_datum)* [','] '}' |
| 357 | key_datum: condition ':' condition |
| 358 | string_conversion:'`' condition_list '`' |
| 359 | \end{verbatim} |
| 360 | |
| 361 | \subsection{Identifiers (Names)} |
| 362 | |
| 363 | An identifier occurring as an atom is a reference to a local, global |
| 364 | or built-in name binding. If a name can be assigned to anywhere in a code |
| 365 | block, it refers to a local name throughout that code block. |
| 366 | Otherwise, it refers to a global name if one exists, else to a |
| 367 | built-in name. |
| 368 | |
| 369 | When the name is bound to an object, evaluation of the atom |
| 370 | yields that object. |
| 371 | When it is not bound, a {\tt NameError} exception |
| 372 | is raised, with the identifier as string parameter. |
| 373 | |
| 374 | \subsection{Literals} |
| 375 | |
| 376 | Evaluation of a literal yields an object of the given type |
| 377 | (string, integer, long integer, floating point number) |
| 378 | with the given value. |
| 379 | The value may be approximated in the case of floating point literals. |
| 380 | |
| 381 | All literals correspond to immutable data types, and hence the object's |
| 382 | identity is less important than its value. |
| 383 | Multiple evaluations of the same literal (either the same occurrence |
| 384 | in the program text or a different occurrence) may |
| 385 | obtain the same object or a different object with the same value. |
| 386 | |
| 387 | (In the original implementation, all literals in the same code block |
| 388 | with the same type and value yield the same object.) |
| 389 | |
| 390 | \subsection{Enclosures} |
| 391 | |
| 392 | An empty enclosure yields an empty tuple object. |
| 393 | |
| 394 | An enclosed condition list yields whatever that condition list yields. |
| 395 | |
| 396 | (Note that, except for empty tuples, tuples are not formed by |
| 397 | enclosure in parentheses, but rather by use of the comma operator.) |
| 398 | |
| 399 | \subsection{List displays} |
| 400 | |
| 401 | A list display yields a new list object. |
| 402 | |
| 403 | If it has no condition list, the list object has no items. |
| 404 | Otherwise, the elements of the condition list are evaluated |
| 405 | from left to right and inserted in the list object in that order. |
| 406 | |
| 407 | \subsection{Dictionary displays} |
| 408 | |
| 409 | A dictionary display yields a new dictionary object. |
| 410 | |
| 411 | The key/datum pairs are evaluated from left to right to |
| 412 | define the entries of the dictionary: |
| 413 | each key object is used as a key into the dictionary to store |
| 414 | the corresponding datum pair. |
| 415 | |
| 416 | Key objects must be strings, otherwise a {\tt TypeError} |
| 417 | exception is raised. |
| 418 | Clashes between keys are not detected; the last datum stored for a given |
| 419 | key value prevails. |
| 420 | |
| 421 | \subsection{String conversions} |
| 422 | |
| 423 | A string conversion evaluates the contained condition list and converts the |
| 424 | resulting object into a string according to rules specific to its type. |
| 425 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 426 | If the object is a string, a number, \verb\None\, or a tuple, list or |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 427 | dictionary containing only objects whose type is in this list, |
| 428 | the resulting |
| 429 | string is a valid Python expression which can be passed to the |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 430 | built-in function \verb\eval()\ to yield an expression with the |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 431 | same value (or an approximation, if floating point numbers are |
| 432 | involved). |
| 433 | |
| 434 | (In particular, converting a string adds quotes around it and converts |
| 435 | ``funny'' characters to escape sequences that are safe to print.) |
| 436 | |
| 437 | It is illegal to attempt to convert recursive objects (e.g., |
| 438 | lists or dictionaries that -- directly or indirectly -- contain a reference |
| 439 | to themselves.) |
| 440 | |
| 441 | \section{Primaries} |
| 442 | |
| 443 | Primaries represent the most tightly bound operations of the language. |
| 444 | Their syntax is: |
| 445 | |
| 446 | \begin{verbatim} |
| 447 | primary: atom | attributeref | call | subscription | slicing |
| 448 | attributeref: primary '.' identifier |
| 449 | call: primary '(' [condition_list] ')' |
| 450 | subscription: primary '[' condition ']' |
| 451 | slicing: primary '[' [condition] ':' [condition] ']' |
| 452 | \end{verbatim} |
| 453 | |
| 454 | \subsection{Attribute references} |
| 455 | |
| 456 | \subsection{Calls} |
| 457 | |
| 458 | \subsection{Subscriptions} |
| 459 | |
| 460 | \subsection{Slicings} |
| 461 | |
| 462 | \section{Factors} |
| 463 | |
| 464 | Factors represent the unary numeric operators. |
| 465 | Their syntax is: |
| 466 | |
| 467 | \begin{verbatim} |
| 468 | factor: primary | '-' factor | '+' factor | '~' factor |
| 469 | \end{verbatim} |
| 470 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 471 | The unary \verb\-\ operator yields the negative of its numeric argument. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 473 | The unary \verb\+\ operator yields its numeric argument unchanged. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 475 | The unary \verb\~\ operator yields the bit-wise negation of its |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 476 | integral numerical argument. |
| 477 | |
| 478 | In all three cases, if the argument does not have the proper type, |
| 479 | a {\tt TypeError} exception is raised. |
| 480 | |
| 481 | \section{Terms} |
| 482 | |
| 483 | Terms represent the most tightly binding binary operators: |
| 484 | |
| 485 | \begin{verbatim} |
| 486 | term: factor | term '*' factor | term '/' factor | term '%' factor |
| 487 | \end{verbatim} |
| 488 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 489 | The \verb\*\ operator yields the product of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 490 | The arguments must either both be numbers, or one argument must be |
| 491 | a (short) integer and the other must be a string. |
| 492 | In the former case, the numbers are converted to a common type |
| 493 | and then multiplied together. |
| 494 | In the latter case, string repetition is performed; a negative |
| 495 | repetition factor yields the empty string. |
| 496 | |
| 497 | The \verb|'/'| operator yields the quotient of its arguments. |
| 498 | The numeric arguments are first converted to a common type. |
| 499 | (Short or long) integer division yields an integer of the same type, |
| 500 | truncating towards zero. |
| 501 | Division by zero raises a {\tt RuntimeError} exception. |
| 502 | |
| 503 | The \verb|'%'| operator yields the remainder from the division |
| 504 | of the first argument by the second. |
| 505 | The numeric arguments are first converted to a common type. |
| 506 | The outcome of $x % y$ is defined as $x - y*trunc(x/y)$. |
| 507 | A zero right argument raises a {\tt RuntimeError} exception. |
| 508 | The arguments may be floating point numbers, e.g., |
| 509 | $3.14 % 0.7$ equals $0.34$. |
| 510 | |
| 511 | \section{Arithmetic expressions} |
| 512 | |
| 513 | \begin{verbatim} |
| 514 | arith_expr: term | arith_expr '+' term | arith_expr '-' term |
| 515 | \end{verbatim} |
| 516 | |
| 517 | The \verb|'+'| operator yields the sum of its arguments. |
| 518 | The arguments must either both be numbers, or both strings. |
| 519 | In the former case, the numbers are converted to a common type |
| 520 | and then added together. |
| 521 | In the latter case, the strings are concatenated directly, |
| 522 | without inserting a space. |
| 523 | |
| 524 | The \verb|'-'| operator yields the difference of its arguments. |
| 525 | The numeric arguments are first converted to a common type. |
| 526 | |
| 527 | \section{Shift expressions} |
| 528 | |
| 529 | \begin{verbatim} |
| 530 | shift_expr: arith_expr | shift_expr '<<' arith_expr | shift_expr '>>' arith_expr |
| 531 | \end{verbatim} |
| 532 | |
| 533 | These operators accept short integers as arguments only. |
| 534 | They shift their left argument to the left or right by the number of bits |
| 535 | given by the right argument. Shifts are ``logical'', e.g., bits shifted |
| 536 | out on one end are lost, and bits shifted in are zero; |
| 537 | negative numbers are shifted as if they were unsigned in C. |
| 538 | Negative shift counts and shift counts greater than {\em or equal to} |
| 539 | the word size yield undefined results. |
| 540 | |
| 541 | \section{Bitwise AND expressions} |
| 542 | |
| 543 | \begin{verbatim} |
| 544 | and_expr: shift_expr | and_expr '&' shift_expr |
| 545 | \end{verbatim} |
| 546 | |
| 547 | This operator yields the bitwise AND of its arguments, |
| 548 | which must be short integers. |
| 549 | |
| 550 | \section{Bitwise XOR expressions} |
| 551 | |
| 552 | \begin{verbatim} |
| 553 | xor_expr: and_expr | xor_expr '^' and_expr |
| 554 | \end{verbatim} |
| 555 | |
| 556 | This operator yields the bitwise exclusive OR of its arguments, |
| 557 | which must be short integers. |
| 558 | |
| 559 | \section{Bitwise OR expressions} |
| 560 | |
| 561 | \begin{verbatim} |
| 562 | or_expr: xor_expr | or_expr '|' xor_expr |
| 563 | \end{verbatim} |
| 564 | |
| 565 | This operator yields the bitwise OR of its arguments, |
| 566 | which must be short integers. |
| 567 | |
| 568 | \section{Expressions and expression lists} |
| 569 | |
| 570 | \begin{verbatim} |
| 571 | expression: or_expression |
| 572 | expr_list: expression (',' expression)* [','] |
| 573 | \end{verbatim} |
| 574 | |
| 575 | An expression list containing at least one comma yields a new tuple. |
| 576 | The length of the tuple is the number of expressions in the list. |
| 577 | The expressions are evaluated from left to right. |
| 578 | |
| 579 | The trailing comma is required only to create a single tuple; |
| 580 | it is optional in all other cases (a single expression without |
| 581 | a trailing comma doesn't create a tuple, but rather yields the |
| 582 | value of that expression). |
| 583 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 584 | To create an empty tuple, use an empty pair of parentheses: \verb\()\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 585 | |
| 586 | \section{Comparisons} |
| 587 | |
| 588 | \begin{verbatim} |
| 589 | comparison: expression (comp_operator expression)* |
| 590 | comp_operator: '<'|'>'|'='|'=='|'>='|'<='|'<>'|'!='|['not'] 'in'|is' ['not'] |
| 591 | \end{verbatim} |
| 592 | |
| 593 | Comparisons yield integer value: 1 for true, 0 for false. |
| 594 | |
| 595 | Comparisons can be chained arbitrarily, |
| 596 | e.g., $x < y <= z$ is equivalent to |
| 597 | $x < y$ {\tt and} $y <= z$, except that $y$ is evaluated only once |
| 598 | (but in both cases $z$ is not evaluated at all when $x < y$ is |
| 599 | found to be false). |
| 600 | |
| 601 | Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to |
| 602 | $e_0 op_1 e_1$ {\tt and} $e_1 op_2 e_2$ {\tt and} ... {\tt and} |
| 603 | $e_{n-1} op_n e_n$, except that each expression is evaluated at most once. |
| 604 | |
| 605 | Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison |
| 606 | between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. |
| 607 | |
| 608 | For the benefit of C programmers, |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 609 | the comparison operators \verb\=\ and \verb\==\ are equivalent, |
| 610 | and so are \verb\<>\ and \verb\!=\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 611 | Use of the C variants is discouraged. |
| 612 | |
| 613 | The operators {\tt '<', '>', '=', '>=', '<='}, and {\tt '<>'} compare |
| 614 | the values of two objects. The objects needn't have the same type. |
| 615 | If both are numbers, they are compared to a common type. |
| 616 | Otherwise, objects of different types {\em always} compare unequal, |
| 617 | and are ordered consistently but arbitrarily, except that |
| 618 | the value \verb\None\ compares smaller than the values of any other type. |
| 619 | |
| 620 | (This unusual |
| 621 | definition of comparison is done to simplify the definition of |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 622 | operations like sorting and the \verb\in\ and \verb\not in\ operators.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 623 | |
| 624 | Comparison of objects of the same type depends on the type: |
| 625 | |
| 626 | \begin{itemize} |
| 627 | \item Numbers are compared arithmetically. |
| 628 | \item Strings are compared lexicographically using the numeric |
| 629 | equivalents (the result of the built-in function ord()) |
| 630 | of their characters. |
| 631 | \item Tuples and lists are compared lexicographically |
| 632 | using comparison of corresponding items. |
| 633 | \item Dictionaries compare unequal unless they are the same object; |
| 634 | the choice whether one dictionary object is considered smaller |
| 635 | or larger than another one is made arbitrarily but |
| 636 | consistently within one execution of a program. |
| 637 | \item The latter rule is also used for most other built-in types. |
| 638 | \end{itemize} |
| 639 | |
| 640 | The operators \verb\in\ and \verb\not in\ test for sequence membership: |
| 641 | if $y$ is a sequence, $x {\tt in} y$ is true if and only if there exists |
| 642 | an index $i$ such that $x = y_i$. |
| 643 | $x {\tt not in} y$ yields the inverse truth value. |
| 644 | The exception {\tt TypeError} is raised when $y$ is not a sequence, |
| 645 | or when $y$ is a string and $x$ is not a string of length one. |
| 646 | |
| 647 | The operators \verb\is\ and \verb\is not\ compare object identity: |
| 648 | $x {\tt is} y$ is true if and only if $x$ and $y$ are the same object. |
| 649 | $x {\tt is not} y$ yields the inverse truth value. |
| 650 | |
| 651 | \section{Boolean operators} |
| 652 | |
| 653 | \begin{verbatim} |
| 654 | condition: or_test |
| 655 | or_test: and_test | or_test 'or' and_test |
| 656 | and_test: not_test | and_test 'and' not_test |
| 657 | not_test: comparison | 'not' not_test |
| 658 | \end{verbatim} |
| 659 | |
| 660 | In the context of Boolean operators, and also when conditions are |
| 661 | used by control flow statements, the following values are interpreted |
| 662 | as false: None, numeric zero of all types, empty sequences (strings, |
| 663 | tuples and lists), and empty mappings (dictionaries). |
| 664 | All other values are interpreted as true. |
| 665 | |
| 666 | The operator \verb\not\ yields 1 if its argument is false, 0 otherwise. |
| 667 | |
| 668 | The condition $x {\tt and} y$ first evaluates $x$; if $x$ is false, |
| 669 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 670 | |
| 671 | The condition $x {\tt or} y$ first evaluates $x$; if $x$ is true, |
| 672 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 673 | |
| 674 | (Note that \verb\and\ and \verb\or\ do not restrict the value and type |
| 675 | they return to 0 and 1, but rather return the last evaluated argument. |
| 676 | This is sometimes useful, e.g., if $s$ is a string, which should be |
| 677 | replaced by a default value if it is empty, $s {\tt or} 'foo'$ |
| 678 | returns the desired value. Because \verb\not\ has to invent a value |
| 679 | anyway, it does not bother to return a value of the same type as its |
| 680 | argument, so \verb\not 'foo'\ yields $0$, not $''$.) |
| 681 | |
| 682 | \chapter{Simple statements} |
| 683 | |
| 684 | Simple statements are comprised within a single logical line. |
| 685 | Several simple statements may occor on a single line separated |
| 686 | by semicolons. The syntax for simple statements is: |
| 687 | |
| 688 | \begin{verbatim} |
| 689 | stmt_list: simple_stmt (';' simple_stmt)* [';'] |
| 690 | simple_stmt: expression_stmt |
| 691 | | assignment |
| 692 | | pass_stmt |
| 693 | | del_stmt |
| 694 | | print_stmt |
| 695 | | return_stmt |
| 696 | | raise_stmt |
| 697 | | break_stmt |
| 698 | | continue_stmt |
| 699 | | import_stmt |
| 700 | \end{verbatim} |
| 701 | |
| 702 | \section{Expression statements} |
| 703 | |
| 704 | \begin{verbatim} |
| 705 | expression_stmt: expression_list |
| 706 | \end{verbatim} |
| 707 | |
| 708 | An expression statement evaluates the expression list (which may |
| 709 | be a single expression). |
| 710 | If the value is not \verb\None\, it is converted to a string |
| 711 | using the rules for string conversions, and the resulting string |
| 712 | is written to standard output on a line by itself. |
| 713 | |
| 714 | (The exception for \verb\None\ is made so that procedure calls, |
| 715 | which are syntactically equivalent to expressions, |
| 716 | do not cause any output.) |
| 717 | |
| 718 | \section{Assignments} |
| 719 | |
| 720 | \begin{verbatim} |
| 721 | assignment: target_list ('=' target_list)* '=' expression_list |
| 722 | target_list: target (',' target)* [','] |
| 723 | target: identifier | '(' target_list ')' | '[' target_list ']' |
| 724 | | attributeref | subscription | slicing |
| 725 | \end{verbatim} |
| 726 | |
| 727 | (See the section on primaries for the definition of the last |
| 728 | three symbols.) |
| 729 | |
| 730 | An assignment evaluates the expression list (remember that this can |
| 731 | be a single expression or a comma-separated list, |
| 732 | the latter yielding a tuple) |
| 733 | and assigns the single resulting object to each of the target lists, |
| 734 | from left to right. |
| 735 | |
| 736 | Assignment is defined recursively depending on the type of the |
| 737 | target. Where assignment is to part of a mutable object |
| 738 | (through an attribute reference, subscription or slicing), |
| 739 | the mutable object must ultimately perform the |
| 740 | assignment and decide about its validity, raising an exception |
| 741 | if the assignment is unacceptable. The rules observed by |
| 742 | various types and the exceptions raised are given with the |
| 743 | definition of the object types (some of which are defined |
| 744 | in the library reference). |
| 745 | |
| 746 | Assignment of an object to a target list is recursively |
| 747 | defined as follows. |
| 748 | |
| 749 | \begin{itemize} |
| 750 | \item |
| 751 | If the target list contains no commas (except in nested constructs): |
| 752 | the object is assigned to the single target contained in the list. |
| 753 | |
| 754 | \item |
| 755 | If the target list contains commas (that are not in nested constructs): |
| 756 | the object must be a tuple with as many items |
| 757 | as the list contains targets, and the items are assigned, from left |
| 758 | to right, to the corresponding targets. |
| 759 | |
| 760 | \end{itemize} |
| 761 | |
| 762 | Assignment of an object to a (non-list) |
| 763 | target is recursively defined as follows. |
| 764 | |
| 765 | \begin{itemize} |
| 766 | |
| 767 | \item |
| 768 | If the target is an identifier (name): |
| 769 | the object is bound to that name |
| 770 | in the current local scope. Any previous binding of the same name |
| 771 | is undone. |
| 772 | |
| 773 | \item |
| 774 | If the target is a target list enclosed in parentheses: |
| 775 | the object is assigned to that target list. |
| 776 | |
| 777 | \item |
| 778 | If the target is a target list enclosed in square brackets: |
| 779 | the object must be a list with as many items |
| 780 | as the target list contains targets, |
| 781 | and the list's items are assigned, from left to right, |
| 782 | to the corresponding targets. |
| 783 | |
| 784 | \item |
| 785 | If the target is an attribute reference: |
| 786 | The primary expression in the reference is evaluated. |
| 787 | It should yield an object with assignable attributes; |
| 788 | if this is not the case, a {\tt TypeError} exception is raised. |
| 789 | That object is then asked to assign the assigned object |
| 790 | to the given attribute; if it cannot perform the assignment, |
| 791 | it raises an exception. |
| 792 | |
| 793 | \item |
| 794 | If the target is a subscription: |
| 795 | The primary expression in the reference is evaluated. |
| 796 | It should yield either a mutable sequence object or a mapping |
| 797 | (dictionary) object. |
| 798 | Next, the subscript expression is evaluated. |
| 799 | |
| 800 | If the primary is a sequence object, the subscript must yield a |
| 801 | nonnegative integer smaller than the sequence's length, |
| 802 | and the sequence is asked to assign the assigned object |
| 803 | to its item with that index. |
| 804 | |
| 805 | If the primary is a mapping object, the subscript must have a |
| 806 | type compatible with the mapping's key type, |
| 807 | and the mapping is then asked to to create a key/datum pair |
| 808 | which maps the subscript to the assigned object. |
| 809 | |
| 810 | Various exceptions can be raised. |
| 811 | |
| 812 | \item |
| 813 | If the target is a slicing: |
| 814 | The primary expression in the reference is evaluated. |
| 815 | It should yield a mutable sequence object (currently only lists). |
| 816 | The assigned object should be a sequence object of the same type. |
| 817 | Next, the lower and upper bound expressions are evaluated, |
| 818 | insofar they are present; defaults are zero and the sequence's length. |
| 819 | The bounds should evaluate to (small) integers. |
| 820 | If either bound is negative, the sequence's length is added to it (once). |
| 821 | The resulting bounds are clipped to lie between zero |
| 822 | and the sequence's length, inclusive. |
| 823 | (XXX Shouldn't this description be with expressions?) |
| 824 | Finally, the sequence object is asked to replace the items |
| 825 | indicated by the slice with the items of the assigned sequence. |
| 826 | This may change the sequence's length, if it allows it. |
| 827 | |
| 828 | \end{itemize} |
| 829 | |
| 830 | (In the original implementation, the syntax for targets is taken |
| 831 | to be the same as for expressions, and invalid syntax is rejected |
| 832 | during the code generation phase, causing less detailed error |
| 833 | messages.) |
| 834 | |
| 835 | \section{The {\tt pass} statement} |
| 836 | |
| 837 | \begin{verbatim} |
| 838 | pass_stmt: 'pass' |
| 839 | \end{verbatim} |
| 840 | |
| 841 | {\tt pass} is a null operation -- when it is executed, |
| 842 | nothing happens. |
| 843 | |
| 844 | \section{The {\tt del} statement} |
| 845 | |
| 846 | \begin{verbatim} |
| 847 | del_stmt: 'del' target_list |
| 848 | \end{verbatim} |
| 849 | |
| 850 | Deletion is recursively defined similar to assignment. |
| 851 | |
| 852 | (XXX Rather that spelling it out in full details, |
| 853 | here are some hints.) |
| 854 | |
| 855 | Deletion of a target list recursively deletes each target, |
| 856 | from left to right. |
| 857 | |
| 858 | Deletion of a name removes the binding of that name (which must exist) |
| 859 | from the local scope. |
| 860 | |
| 861 | Deletion of attribute references, subscriptions and slicings |
| 862 | is passed to the primary object involved; deletion of a slicing |
| 863 | is in general equivalent to assignment of an empty slice of the |
| 864 | right type (but even this is determined by the sliced object). |
| 865 | |
| 866 | \section{The {\tt print} statement} |
| 867 | |
| 868 | \begin{verbatim} |
| 869 | print_stmt: 'print' [ condition (',' condition)* [','] ] |
| 870 | \end{verbatim} |
| 871 | |
| 872 | {\tt print} evaluates each condition in turn and writes the resulting |
| 873 | object to standard output (see below). |
| 874 | If an object is not a string, it is first converted to |
| 875 | a string using the rules for string conversions. |
| 876 | The (resulting or original) string is then written. |
| 877 | A space is written before each object is (converted and) written, |
| 878 | unless the output system believes it is positioned at the beginning |
| 879 | of a line. This is the case: (1) when no characters have been written |
| 880 | to standard output; or (2) when the last character written to |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 881 | standard output is \verb/\n/; |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 882 | or (3) when the last I/O operation |
| 883 | on standard output was not a \verb\print\ statement. |
| 884 | |
| 885 | Finally, |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 886 | a \verb/\n/ character is written at the end, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 887 | unless the \verb\print\ statement ends with a comma. |
| 888 | This is the only action if the statement contains just the keyword |
| 889 | \verb\print\. |
| 890 | |
| 891 | Standard output is defined as the file object named \verb\stdout\ |
| 892 | in the built-in module \verb\sys\. If no such object exists, |
| 893 | or if it is not a writable file, a {\tt RuntimeError} exception is raised. |
| 894 | (The original implementation attempts to write to the system's original |
| 895 | standard output instead, but this is not safe, and should be fixed.) |
| 896 | |
| 897 | \section{The {\tt return} statement} |
| 898 | |
| 899 | \begin{verbatim} |
| 900 | return_stmt: 'return' [condition_list] |
| 901 | \end{verbatim} |
| 902 | |
| 903 | \verb\return\ may only occur syntactically nested in a function |
| 904 | definition, not within a nested class definition. |
| 905 | |
| 906 | If a condition list is present, it is evaluated, else \verb\None\ |
| 907 | is substituted. |
| 908 | |
| 909 | \verb\return\ leaves the current function call with the condition |
| 910 | list (or \verb\None\) as return value. |
| 911 | |
| 912 | When \verb\return\ passes control out of a \verb\try\ statement |
| 913 | with a \verb\finally\ clause, that finally clause is executed |
| 914 | before really leaving the function. |
| 915 | (XXX This should be made more exact, a la Modula-3.) |
| 916 | |
| 917 | \section{The {\tt raise} statement} |
| 918 | |
| 919 | \begin{verbatim} |
| 920 | raise_stmt: 'raise' condition [',' condition] |
| 921 | \end{verbatim} |
| 922 | |
| 923 | \verb\raise\ evaluates its first condition, which must yield |
| 924 | a string object. If there is a second condition, this is evaluated, |
| 925 | else \verb\None\ is substituted. |
| 926 | |
| 927 | It then raises the exception identified by the first object, |
| 928 | with the second one (or \verb\None\) as its parameter. |
| 929 | |
| 930 | \section{The {\tt break} statement} |
| 931 | |
| 932 | \begin{verbatim} |
| 933 | break_stmt: 'break' |
| 934 | \end{verbatim} |
| 935 | |
| 936 | \verb\break\ may only occur syntactically nested in a \verb\for\ |
| 937 | or \verb\while\ loop, not nested in a function or class definition. |
| 938 | |
| 939 | It terminates the neares enclosing loop, skipping the optional |
| 940 | \verb\else\ clause if the loop has one. |
| 941 | |
| 942 | If a \verb\for\ loop is terminated by \verb\break\, the loop control |
| 943 | target (list) keeps its current value. |
| 944 | |
| 945 | When \verb\break\ passes control out of a \verb\try\ statement |
| 946 | with a \verb\finally\ clause, that finally clause is executed |
| 947 | before really leaving the loop. |
| 948 | |
| 949 | \section{The {\tt continue} statement} |
| 950 | |
| 951 | \begin{verbatim} |
| 952 | continue_stmt: 'continue' |
| 953 | \end{verbatim} |
| 954 | |
| 955 | \verb\continue\ may only occur syntactically nested in a \verb\for\ |
| 956 | or \verb\while\ loop, not nested in a function or class definition, |
| 957 | and {\em not nested in a \verb\try\ statement with a \verb\finally\ |
| 958 | clause}. |
| 959 | |
| 960 | It continues with the next cycle of the nearest enclosing loop. |
| 961 | |
| 962 | \section{The {\tt import} statement} |
| 963 | |
| 964 | \begin{verbatim} |
| 965 | import_stmt: 'import' identifier (',' identifier)* |
| 966 | | 'from' identifier 'import' identifier (',' identifier)* |
| 967 | | 'from' identifier 'import' '*' |
| 968 | \end{verbatim} |
| 969 | |
| 970 | (XXX To be done.) |
| 971 | |
| 972 | \chapter{Compound statements} |
| 973 | |
| 974 | (XXX The semantic definitions of this chapter are still to be done.) |
| 975 | |
| 976 | \begin{verbatim} |
| 977 | statement: stmt_list NEWLINE | compound_stmt |
| 978 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 979 | suite: statement | NEWLINE INDENT statement+ DEDENT |
| 980 | \end{verbatim} |
| 981 | |
| 982 | \section{The {\tt if} statement} |
| 983 | |
| 984 | \begin{verbatim} |
| 985 | if_stmt: 'if' condition ':' suite |
| 986 | ('elif' condition ':' suite)* |
| 987 | ['else' ':' suite] |
| 988 | \end{verbatim} |
| 989 | |
| 990 | \section{The {\tt while} statement} |
| 991 | |
| 992 | \begin{verbatim} |
| 993 | while_stmt: 'while' condition ':' suite ['else' ':' suite] |
| 994 | \end{verbatim} |
| 995 | |
| 996 | \section{The {\tt for} statement} |
| 997 | |
| 998 | \begin{verbatim} |
| 999 | for_stmt: 'for' target_list 'in' condition_list ':' suite |
| 1000 | ['else' ':' suite] |
| 1001 | \end{verbatim} |
| 1002 | |
| 1003 | \section{The {\tt try} statement} |
| 1004 | |
| 1005 | \begin{verbatim} |
| 1006 | try_stmt: 'try' ':' suite |
| 1007 | ('except' condition [',' condition] ':' suite)* |
| 1008 | ['finally' ':' suite] |
| 1009 | \end{verbatim} |
| 1010 | |
| 1011 | \section{Function definitions} |
| 1012 | |
| 1013 | \begin{verbatim} |
| 1014 | funcdef: 'def' identifier '(' [parameter_list] ')' ':' suite |
| 1015 | parameter_list: parameter (',' parameter)* |
| 1016 | parameter: identifier | '(' parameter_list ')' |
| 1017 | \end{verbatim} |
| 1018 | |
| 1019 | \section{Class definitions} |
| 1020 | |
| 1021 | \begin{verbatim} |
| 1022 | classdef: 'class' identifier '(' ')' [inheritance] ':' suite |
| 1023 | inheritance: '=' identifier '(' ')' (',' identifier '(' ')')* |
| 1024 | \end{verbatim} |
| 1025 | |
| 1026 | XXX Syntax for scripts, modules |
| 1027 | XXX Syntax for interactive input, eval, exec, input |
| 1028 | |
| 1029 | \end{document} |