Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1 | % Format this file with latex. |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 63 | While I am trying to be as precise as possible, I chose to use English |
| 64 | rather than formal specifications for everything except syntax and |
| 65 | lexical analysis. This should make the document better understandable |
| 66 | to the average reader, but will leave room for ambiguities. |
| 67 | Consequently, if you were coming from Mars and tried to re-implement |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 68 | Python from this document alone, you might have to guess things and in |
| 69 | fact you would be implementing quite a different language. |
| 70 | On the other hand, if you are using |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 71 | Python and wonder what the precise rules about a particular area of |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 72 | the language are, you should definitely be able to find it here. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 73 | |
| 74 | It is dangerous to add too many implementation details to a language |
| 75 | reference document -- the implementation may change, and other |
| 76 | implementations of the same language may work differently. On the |
| 77 | other hand, there is currently only one Python implementation, and |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 78 | its particular quirks are sometimes worth being mentioned, especially |
| 79 | where the implementation imposes additional limitations. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 80 | |
| 81 | Every Python implementation comes with a number of built-in and |
| 82 | standard modules. These are not documented here, but in the separate |
| 83 | {\em Python Library Reference} document. A few built-in modules are |
| 84 | mentioned when they interact in a significant way with the language |
| 85 | definition. |
| 86 | |
| 87 | \section{Notation} |
| 88 | |
| 89 | The descriptions of lexical analysis and syntax use a modified BNF |
| 90 | grammar notation. This uses the following style of definition: |
| 91 | |
| 92 | \begin{verbatim} |
| 93 | name: lcletter (lcletter | "_")* |
| 94 | lcletter: "a"..."z" |
| 95 | \end{verbatim} |
| 96 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 97 | The first line says that a \verb\name\ is an \verb\lcletter\ followed by |
| 98 | a sequence of zero or more \verb\lcletter\s and underscores. An |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 99 | \verb\lcletter\ in turn is any of the single characters `a' through `z'. |
| 100 | (This rule is actually adhered to for the names defined in syntax and |
| 101 | grammar rules in this document.) |
| 102 | |
| 103 | Each rule begins with a name (which is the name defined by the rule) |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 104 | and a colon, and is wholly contained on one line. A vertical bar |
| 105 | (\verb\|\) is used to separate alternatives; it is the least binding |
| 106 | operator in this notation. A star (\verb\*\) means zero or more |
| 107 | repetitions of the preceding item; likewise, a plus (\verb\+\) means |
| 108 | one or more repetitions, and a question mark (\verb\?\) zero or one |
| 109 | (in other words, the preceding item is optional). These three |
| 110 | operators bind as tightly as possible; parentheses are used for |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 111 | grouping. Literal strings are enclosed in double quotes. White space |
| 112 | is only meaningful to separate tokens. |
| 113 | |
| 114 | In lexical definitions (as the example above), two more conventions |
| 115 | are used: Two literal characters separated by three dots mean a choice |
| 116 | of any single character in the given (inclusive) range of ASCII |
| 117 | characters. A phrase between angular brackets (\verb\<...>\) gives an |
| 118 | informal description of the symbol defined; e.g., this could be used |
| 119 | to describe the notion of `control character' if needed. |
| 120 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 121 | Even though the notation used is almost the same, there is a big |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 122 | difference between the meaning of lexical and syntactic definitions: |
| 123 | a lexical definition operates on the individual characters of the |
| 124 | input source, while a syntax definition operates on the stream of |
| 125 | tokens generated by the lexical analysis. |
| 126 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 127 | \chapter{Lexical analysis} |
| 128 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 129 | A Python program is read by a {\em parser}. Input to the parser is a |
| 130 | stream of {\em tokens}, generated by the {\em lexical analyzer}. This |
| 131 | chapter describes how the lexical analyzer breaks a file into tokens. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 132 | |
| 133 | \section{Line structure} |
| 134 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 135 | A Python program is divided in a number of logical lines. The end of |
| 136 | a logical line is represented by the token NEWLINE. Statements cannot |
| 137 | cross logical line boundaries except where NEWLINE is allowed by the |
| 138 | syntax (e.g., between statements in compound statements). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 139 | |
| 140 | \subsection{Comments} |
| 141 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 142 | A comment starts with a hash character (\verb\#\) that is not part of |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 143 | a string literal, and ends at the end of the physical line. A comment |
| 144 | always signifies the end of the logical line. Comments are ignored by |
| 145 | the syntax. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 146 | |
| 147 | \subsection{Line joining} |
| 148 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 149 | Two or more physical lines may be joined into logical lines using |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 150 | backslash characters (\verb/\/), as follows: when a physical line ends |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 151 | in a backslash that is not part of a string literal or comment, it is |
| 152 | joined with the following forming a single logical line, deleting the |
| 153 | backslash and the following end-of-line character. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 154 | |
| 155 | \subsection{Blank lines} |
| 156 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 157 | A logical line that contains only spaces, tabs, and possibly a |
| 158 | comment, is ignored (i.e., no NEWLINE token is generated), except that |
| 159 | during interactive input of statements, an entirely blank logical line |
| 160 | terminates a multi-line statement. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 161 | |
| 162 | \subsection{Indentation} |
| 163 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 164 | Leading whitespace (spaces and tabs) at the beginning of a logical |
| 165 | line is used to compute the indentation level of the line, which in |
| 166 | turn is used to determine the grouping of statements. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 168 | First, tabs are replaced (from left to right) by one to eight spaces |
| 169 | such that the total number of characters up to there is a multiple of |
| 170 | eight (this is intended to be the same rule as used by UNIX). The |
| 171 | total number of spaces preceding the first non-blank character then |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 172 | determines the line's indentation. Indentation cannot be split over |
| 173 | multiple physical lines using backslashes. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 174 | |
| 175 | The indentation levels of consecutive lines are used to generate |
| 176 | INDENT and DEDENT tokens, using a stack, as follows. |
| 177 | |
| 178 | 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] | 179 | the stack; this will never be popped off again. The numbers pushed on |
| 180 | the stack will always be strictly increasing from bottom to top. At |
| 181 | the beginning of each logical line, the line's indentation level is |
| 182 | compared to the top of the stack. If it is equal, nothing happens. |
| 183 | If it larger, it is pushed on the stack, and one INDENT token is |
| 184 | generated. If it is smaller, it {\em must} be one of the numbers |
| 185 | occurring on the stack; all numbers on the stack that are larger are |
| 186 | popped off, and for each number popped off a DEDENT token is |
| 187 | generated. At the end of the file, a DEDENT token is generated for |
| 188 | each number remaining on the stack that is larger than zero. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 190 | Here is an example of a correctly (though confusingly) indented piece |
| 191 | of Python code: |
| 192 | |
| 193 | \begin{verbatim} |
| 194 | def perm(l): |
| 195 | if len(l) <= 1: |
| 196 | return [l] |
| 197 | r = [] |
| 198 | for i in range(len(l)): |
| 199 | s = l[:i] + l[i+1:] |
| 200 | p = perm(s) |
| 201 | for x in p: |
| 202 | r.append(l[i:i+1] + x) |
| 203 | return r |
| 204 | \end{verbatim} |
| 205 | |
| 206 | The following example shows various indentation errors: |
| 207 | |
| 208 | \begin{verbatim} |
| 209 | def perm(l): # error: first line indented |
| 210 | for i in range(len(l)): # error: not indented |
| 211 | s = l[:i] + l[i+1:] |
| 212 | p = perm(l[:i] + l[i+1:]) # error: unexpected indent |
| 213 | for x in p: |
| 214 | r.append(l[i:i+1] + x) |
| 215 | return r # error: inconsistent indent |
| 216 | \end{verbatim} |
| 217 | |
| 218 | (Actually, the first three errors are detected by the parser; only the |
| 219 | last error is found by the lexical analyzer -- the indentation of |
| 220 | \verb\return r\ does not match a level popped off the stack.) |
| 221 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 222 | \section{Other tokens} |
| 223 | |
| 224 | Besides NEWLINE, INDENT and DEDENT, the following categories of tokens |
| 225 | exist: identifiers, keywords, literals, operators, and delimiters. |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 226 | Spaces and tabs are not tokens, but serve to delimit tokens. Where |
| 227 | ambiguity exists, a token comprises the longest possible string that |
| 228 | forms a legal token, when read from left to right. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 230 | \section{Identifiers} |
| 231 | |
| 232 | Identifiers are described by the following regular expressions: |
| 233 | |
| 234 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 235 | identifier: (letter|"_") (letter|digit|"_")* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 236 | letter: lowercase | uppercase |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 237 | lowercase: "a"..."z" |
| 238 | uppercase: "A"..."Z" |
| 239 | digit: "0"..."9" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 240 | \end{verbatim} |
| 241 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 242 | Identifiers are unlimited in length. Case is significant. Keywords |
| 243 | are not identifiers. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 244 | |
| 245 | \section{Keywords} |
| 246 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 247 | The following identifiers are used as reserved words, or {\em |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 248 | keywords} of the language, and cannot be used as ordinary |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 249 | identifiers. They must be spelled exactly as written here: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 251 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 252 | and del for in print |
| 253 | break elif from is raise |
| 254 | class else global not return |
| 255 | continue except if or try |
| 256 | def finally import pass while |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 257 | \end{verbatim} |
| 258 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 259 | % # This Python program sorts and formats the above table |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 260 | % import string |
| 261 | % l = [] |
| 262 | % try: |
| 263 | % while 1: |
| 264 | % l = l + string.split(raw_input()) |
| 265 | % except EOFError: |
| 266 | % pass |
| 267 | % l.sort() |
| 268 | % for i in range((len(l)+4)/5): |
| 269 | % for j in range(i, len(l), 5): |
| 270 | % print string.ljust(l[j], 10), |
| 271 | % print |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 272 | |
| 273 | \section{Literals} |
| 274 | |
| 275 | \subsection{String literals} |
| 276 | |
| 277 | String literals are described by the following regular expressions: |
| 278 | |
| 279 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 280 | stringliteral: "'" stringitem* "'" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 281 | stringitem: stringchar | escapeseq |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 282 | stringchar: <any ASCII character except newline or "\" or "'"> |
| 283 | escapeseq: "'" <any ASCII character except newline> |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 284 | \end{verbatim} |
| 285 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 286 | String literals cannot span physical line boundaries. Escape |
| 287 | sequences in strings are actually interpreted according to rules |
| 288 | simular to those used by Standard C. The recognized escape sequences |
| 289 | are: |
| 290 | |
| 291 | \begin{center} |
| 292 | \begin{tabular}{|l|l|} |
| 293 | \hline |
| 294 | \verb/\\/ & Backslash (\verb/\/) \\ |
| 295 | \verb/\'/ & Single quote (\verb/'/) \\ |
| 296 | \verb/\a/ & ASCII Bell (BEL) \\ |
| 297 | \verb/\b/ & ASCII Backspace (BS) \\ |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 298 | %\verb/\E/ & ASCII Escape (ESC) \\ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 299 | \verb/\f/ & ASCII Formfeed (FF) \\ |
| 300 | \verb/\n/ & ASCII Linefeed (LF) \\ |
| 301 | \verb/\r/ & ASCII Carriage Return (CR) \\ |
| 302 | \verb/\t/ & ASCII Horizontal Tab (TAB) \\ |
| 303 | \verb/\v/ & ASCII Vertical Tab (VT) \\ |
| 304 | \verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\ |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 305 | \verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 306 | \hline |
| 307 | \end{tabular} |
| 308 | \end{center} |
| 309 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 310 | In strict compatibility with in Standard C, up to three octal digits are |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 311 | accepted, but an unlimited number of hex digits is taken to be part of |
| 312 | the hex escape (and then the lower 8 bits of the resulting hex number |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 313 | are used in all current implementations...). |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 315 | All unrecognized escape sequences are left in the string unchanged, |
| 316 | i.e., {\em the backslash is left in the string.} (This rule is |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 317 | useful when debugging: if an escape sequence is mistyped, the |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 318 | resulting output is more easily recognized as broken. It also helps a |
| 319 | great deal for string literals used as regular expressions or |
| 320 | otherwise passed to other modules that do their own escape handling -- |
| 321 | but you may end up quadrupling backslashes that must appear literally.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 322 | |
| 323 | \subsection{Numeric literals} |
| 324 | |
| 325 | There are three types of numeric literals: integers, long integers, |
| 326 | and floating point numbers. |
| 327 | |
| 328 | Integers and long integers are described by the following regular expressions: |
| 329 | |
| 330 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 331 | longinteger: integer ("l"|"L") |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 332 | integer: decimalinteger | octinteger | hexinteger |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 333 | decimalinteger: nonzerodigit digit* | "0" |
| 334 | octinteger: "0" octdigit+ |
| 335 | hexinteger: "0" ("x"|"X") hexdigit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 337 | nonzerodigit: "1"..."9" |
| 338 | octdigit: "0"..."7" |
| 339 | hexdigit: digit|"a"..."f"|"A"..."F" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 340 | \end{verbatim} |
| 341 | |
| 342 | Floating point numbers are described by the following regular expressions: |
| 343 | |
| 344 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 345 | floatnumber: [intpart] fraction [exponent] | intpart ["."] exponent |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 346 | intpart: digit+ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 347 | fraction: "." digit+ |
| 348 | exponent: ("e"|"E") ["+"|"-"] digit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 349 | \end{verbatim} |
| 350 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 351 | Some examples of numeric literals: |
| 352 | |
| 353 | \begin{verbatim} |
| 354 | 1 1234567890 0177777 0x80000 |
| 355 | |
| 356 | |
| 357 | \end{verbatim} |
| 358 | |
| 359 | Note that the definitions for literals do not include a sign; a phrase |
| 360 | like \verb\-1\ is actually an expression composed of the operator |
| 361 | \verb\-\ and the literal \verb\1\. |
| 362 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 363 | \section{Operators} |
| 364 | |
| 365 | The following tokens are operators: |
| 366 | |
| 367 | \begin{verbatim} |
| 368 | + - * / % |
| 369 | << >> & | ^ ~ |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 370 | < == > <= <> != >= |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 371 | \end{verbatim} |
| 372 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 373 | The comparison operators \verb\<>\ and \verb\!=\ are alternate |
| 374 | spellings of the same operator. |
| 375 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 376 | \section{Delimiters} |
| 377 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 378 | The following tokens serve as delimiters or otherwise have a special |
| 379 | meaning: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 380 | |
| 381 | \begin{verbatim} |
| 382 | ( ) [ ] { } |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 383 | ; , : . ` = |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 384 | \end{verbatim} |
| 385 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 386 | The following printing ASCII characters are not used in Python (except |
| 387 | in string literals and in comments). Their occurrence is an |
| 388 | unconditional error: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 389 | |
| 390 | \begin{verbatim} |
| 391 | ! @ $ " ? |
| 392 | \end{verbatim} |
| 393 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 394 | They may be used by future versions of the language though! |
| 395 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 396 | \chapter{Execution model} |
| 397 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 398 | (XXX This chapter should explain the general model of the execution of |
| 399 | Python code and the evaluation of expressions. It should introduce |
| 400 | objects, values, code blocks, scopes, name spaces, name binding, |
| 401 | types, sequences, numbers, mappings, exceptions, and other technical |
| 402 | terms needed to make the following chapters concise and exact.) |
| 403 | |
| 404 | \section{Objects, values and types} |
| 405 | |
| 406 | I won't try to define rigorously here what an object is, but I'll give |
| 407 | some properties of objects that are important to know about. |
| 408 | |
| 409 | Every object has an identity, a type and a value. An object's {\em |
| 410 | identity} never changes once it has been created; think of it as the |
| 411 | object's (permanent) address. An object's {\em type} determines the |
| 412 | operations that an object supports (e.g., can its length be taken?) |
| 413 | and also defines the ``meaning'' of the object's value; it also never |
| 414 | changes. The {\em value} of some objects can change; whether an |
| 415 | object's value can change is a property of its type. |
| 416 | |
| 417 | Objects are never explicitly destroyed; however, when they become |
| 418 | unreachable they may be garbage-collected. An implementation, |
| 419 | however, is allowed to delay garbage collection or omit it altogether |
| 420 | -- it is a matter of implementation quality how garbage collection is |
| 421 | implemented. (Implementation note: the current implementation uses a |
| 422 | reference-counting scheme which collects most objects as soon as they |
| 423 | become onreachable, but does not detect garbage containing circular |
| 424 | references.) |
| 425 | |
| 426 | (Some objects contain references to ``external'' resources such as |
| 427 | open files. It is understood that these resources are freed when the |
| 428 | object is garbage-collected, but since garbage collection is not |
| 429 | guaranteed such objects also provide an explicit way to release the |
| 430 | external resource (e.g., a \verb\close\ method) and programs are |
| 431 | recommended to use this.) |
| 432 | |
| 433 | Some objects contain references to other objects. These references |
| 434 | are part of the object's value; in most cases, when such a |
| 435 | ``container'' object is compared to another (of the same type), the |
| 436 | comparison takes the {\em values} of the referenced objects into |
| 437 | account (not their identities). |
| 438 | |
| 439 | Except for their identity, types affect almost any aspect of objects. |
| 440 | Even object identities are affected in some sense: for immutable |
| 441 | types, operations that compute new values may actually return a |
| 442 | reference to an existing object with the same type and value, while |
| 443 | for mutable objects this is not allowed. E.g., after |
| 444 | |
| 445 | \begin{verbatim} |
| 446 | a = 1; b = 1; c = []; d = [] |
| 447 | \end{verbatim} |
| 448 | |
| 449 | \verb\a\ and \verb\b\ may or may not refer to the same object, but |
| 450 | \verb\c\ and \verb\d\ are guaranteed to refer to two different, unique, |
| 451 | newly created lists. |
| 452 | |
| 453 | \section{Execution frames, name spaces, and scopes} |
| 454 | |
| 455 | XXX |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 456 | |
| 457 | \chapter{Expressions and conditions} |
| 458 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 459 | From now on, extended BNF notation will be used to describe syntax, |
| 460 | not lexical analysis. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 461 | |
| 462 | This chapter explains the meaning of the elements of expressions and |
| 463 | conditions. Conditions are a superset of expressions, and a condition |
| 464 | may be used where an expression is required by enclosing it in |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 465 | parentheses. The only place where an unparenthesized condition is not |
| 466 | allowed is on the right-hand side of the assignment operator, because |
| 467 | this operator is the same token (\verb\=\) as used for compasisons. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 469 | The comma plays a somewhat special role in Python's syntax. It is an |
| 470 | operator with a lower precedence than all others, but occasionally |
| 471 | serves other purposes as well (e.g., it has special semantics in print |
| 472 | statements). When a comma is accepted by the syntax, one of the |
| 473 | syntactic categories \verb\expression_list\ or \verb\condition_list\ |
| 474 | is always used. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 475 | |
| 476 | When (one alternative of) a syntax rule has the form |
| 477 | |
| 478 | \begin{verbatim} |
| 479 | name: othername |
| 480 | \end{verbatim} |
| 481 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 482 | and no semantics are given, the semantics of this form of \verb\name\ |
| 483 | are the same as for \verb\othername\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 484 | |
| 485 | \section{Arithmetic conversions} |
| 486 | |
| 487 | When a description of an arithmetic operator below uses the phrase |
| 488 | ``the numeric arguments are converted to a common type'', |
| 489 | this both means that if either argument is not a number, a |
| 490 | {\tt TypeError} exception is raised, and that otherwise |
| 491 | the following conversions are applied: |
| 492 | |
| 493 | \begin{itemize} |
| 494 | \item First, if either argument is a floating point number, |
| 495 | the other is converted to floating point; |
| 496 | \item else, if either argument is a long integer, |
| 497 | the other is converted to long integer; |
| 498 | \item otherwise, both must be short integers and no conversion |
| 499 | is necessary. |
| 500 | \end{itemize} |
| 501 | |
| 502 | (Note: ``short integers'' in Python are at least 32 bits in size; |
| 503 | ``long integers'' are arbitrary precision integers.) |
| 504 | |
| 505 | \section{Atoms} |
| 506 | |
| 507 | Atoms are the most basic elements of expressions. |
| 508 | Forms enclosed in reverse quotes or various types of parentheses |
| 509 | or braces are also categorized syntactically as atoms. |
| 510 | Syntax rules for atoms: |
| 511 | |
| 512 | \begin{verbatim} |
| 513 | atom: identifier | literal | parenth_form | string_conversion |
| 514 | literal: stringliteral | integer | longinteger | floatnumber |
| 515 | parenth_form: enclosure | list_display | dict_display |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 516 | enclosure: "(" [condition_list] ")" |
| 517 | list_display: "[" [condition_list] "]" |
| 518 | dict_display: "{" [key_datum ("," key_datum)* [","] "}" |
| 519 | key_datum: condition ":" condition |
| 520 | string_conversion:"`" condition_list "`" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 521 | \end{verbatim} |
| 522 | |
| 523 | \subsection{Identifiers (Names)} |
| 524 | |
| 525 | An identifier occurring as an atom is a reference to a local, global |
| 526 | or built-in name binding. If a name can be assigned to anywhere in a code |
| 527 | block, it refers to a local name throughout that code block. |
| 528 | Otherwise, it refers to a global name if one exists, else to a |
| 529 | built-in name. |
| 530 | |
| 531 | When the name is bound to an object, evaluation of the atom |
| 532 | yields that object. |
| 533 | When it is not bound, a {\tt NameError} exception |
| 534 | is raised, with the identifier as string parameter. |
| 535 | |
| 536 | \subsection{Literals} |
| 537 | |
| 538 | Evaluation of a literal yields an object of the given type |
| 539 | (string, integer, long integer, floating point number) |
| 540 | with the given value. |
| 541 | The value may be approximated in the case of floating point literals. |
| 542 | |
| 543 | All literals correspond to immutable data types, and hence the object's |
| 544 | identity is less important than its value. |
| 545 | Multiple evaluations of the same literal (either the same occurrence |
| 546 | in the program text or a different occurrence) may |
| 547 | obtain the same object or a different object with the same value. |
| 548 | |
| 549 | (In the original implementation, all literals in the same code block |
| 550 | with the same type and value yield the same object.) |
| 551 | |
| 552 | \subsection{Enclosures} |
| 553 | |
| 554 | An empty enclosure yields an empty tuple object. |
| 555 | |
| 556 | An enclosed condition list yields whatever that condition list yields. |
| 557 | |
| 558 | (Note that, except for empty tuples, tuples are not formed by |
| 559 | enclosure in parentheses, but rather by use of the comma operator.) |
| 560 | |
| 561 | \subsection{List displays} |
| 562 | |
| 563 | A list display yields a new list object. |
| 564 | |
| 565 | If it has no condition list, the list object has no items. |
| 566 | Otherwise, the elements of the condition list are evaluated |
| 567 | from left to right and inserted in the list object in that order. |
| 568 | |
| 569 | \subsection{Dictionary displays} |
| 570 | |
| 571 | A dictionary display yields a new dictionary object. |
| 572 | |
| 573 | The key/datum pairs are evaluated from left to right to |
| 574 | define the entries of the dictionary: |
| 575 | each key object is used as a key into the dictionary to store |
| 576 | the corresponding datum pair. |
| 577 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 578 | Keys must be strings, otherwise a {\tt TypeError} exception is raised. |
| 579 | Clashes between keys are not detected; the last datum (textually |
| 580 | rightmost in the display) stored for a given key value prevails. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 581 | |
| 582 | \subsection{String conversions} |
| 583 | |
| 584 | A string conversion evaluates the contained condition list and converts the |
| 585 | resulting object into a string according to rules specific to its type. |
| 586 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 587 | 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] | 588 | dictionary containing only objects whose type is in this list, |
| 589 | the resulting |
| 590 | 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] | 591 | built-in function \verb\eval()\ to yield an expression with the |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 592 | same value (or an approximation, if floating point numbers are |
| 593 | involved). |
| 594 | |
| 595 | (In particular, converting a string adds quotes around it and converts |
| 596 | ``funny'' characters to escape sequences that are safe to print.) |
| 597 | |
| 598 | It is illegal to attempt to convert recursive objects (e.g., |
| 599 | lists or dictionaries that -- directly or indirectly -- contain a reference |
| 600 | to themselves.) |
| 601 | |
| 602 | \section{Primaries} |
| 603 | |
| 604 | Primaries represent the most tightly bound operations of the language. |
| 605 | Their syntax is: |
| 606 | |
| 607 | \begin{verbatim} |
| 608 | primary: atom | attributeref | call | subscription | slicing |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 609 | attributeref: primary "." identifier |
| 610 | call: primary "(" [condition_list] ")" |
| 611 | subscription: primary "[" condition "]" |
| 612 | slicing: primary "[" [condition] ":" [condition] "]" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 613 | \end{verbatim} |
| 614 | |
| 615 | \subsection{Attribute references} |
| 616 | |
| 617 | \subsection{Calls} |
| 618 | |
| 619 | \subsection{Subscriptions} |
| 620 | |
| 621 | \subsection{Slicings} |
| 622 | |
| 623 | \section{Factors} |
| 624 | |
| 625 | Factors represent the unary numeric operators. |
| 626 | Their syntax is: |
| 627 | |
| 628 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 629 | factor: primary | "-" factor | "+" factor | "~" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 630 | \end{verbatim} |
| 631 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 632 | The unary \verb\-\ operator yields the negative of its numeric argument. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 634 | The unary \verb\+\ operator yields its numeric argument unchanged. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 636 | The unary \verb\~\ operator yields the bit-wise negation of its |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 637 | integral numerical argument. |
| 638 | |
| 639 | In all three cases, if the argument does not have the proper type, |
| 640 | a {\tt TypeError} exception is raised. |
| 641 | |
| 642 | \section{Terms} |
| 643 | |
| 644 | Terms represent the most tightly binding binary operators: |
| 645 | |
| 646 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 647 | term: factor | term "*" factor | term "/" factor | term "%" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 648 | \end{verbatim} |
| 649 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 650 | The \verb\*\ operator yields the product of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 651 | The arguments must either both be numbers, or one argument must be |
| 652 | a (short) integer and the other must be a string. |
| 653 | In the former case, the numbers are converted to a common type |
| 654 | and then multiplied together. |
| 655 | In the latter case, string repetition is performed; a negative |
| 656 | repetition factor yields the empty string. |
| 657 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 658 | The \verb|"/"| operator yields the quotient of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 659 | The numeric arguments are first converted to a common type. |
| 660 | (Short or long) integer division yields an integer of the same type, |
| 661 | truncating towards zero. |
| 662 | Division by zero raises a {\tt RuntimeError} exception. |
| 663 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 664 | The \verb|"%"| operator yields the remainder from the division |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 665 | of the first argument by the second. |
| 666 | The numeric arguments are first converted to a common type. |
Guido van Rossum | 47f2333 | 1991-12-06 17:21:05 +0000 | [diff] [blame] | 667 | The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 668 | A zero right argument raises a {\tt RuntimeError} exception. |
| 669 | The arguments may be floating point numbers, e.g., |
Guido van Rossum | 47f2333 | 1991-12-06 17:21:05 +0000 | [diff] [blame] | 670 | $3.14 \% 0.7$ equals $0.34$. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 671 | |
| 672 | \section{Arithmetic expressions} |
| 673 | |
| 674 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 675 | arith_expr: term | arith_expr "+" term | arith_expr "-" term |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 676 | \end{verbatim} |
| 677 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 678 | The \verb|"+"| operator yields the sum of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 679 | The arguments must either both be numbers, or both strings. |
| 680 | In the former case, the numbers are converted to a common type |
| 681 | and then added together. |
| 682 | In the latter case, the strings are concatenated directly, |
| 683 | without inserting a space. |
| 684 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 685 | The \verb|"-"| operator yields the difference of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 686 | The numeric arguments are first converted to a common type. |
| 687 | |
| 688 | \section{Shift expressions} |
| 689 | |
| 690 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 691 | shift_expr: arith_expr | shift_expr "<<" arith_expr | shift_expr ">>" arith_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 692 | \end{verbatim} |
| 693 | |
| 694 | These operators accept short integers as arguments only. |
| 695 | They shift their left argument to the left or right by the number of bits |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 696 | given by the right argument. Shifts are ``logical"", e.g., bits shifted |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 697 | out on one end are lost, and bits shifted in are zero; |
| 698 | negative numbers are shifted as if they were unsigned in C. |
| 699 | Negative shift counts and shift counts greater than {\em or equal to} |
| 700 | the word size yield undefined results. |
| 701 | |
| 702 | \section{Bitwise AND expressions} |
| 703 | |
| 704 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 705 | and_expr: shift_expr | and_expr "&" shift_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 706 | \end{verbatim} |
| 707 | |
| 708 | This operator yields the bitwise AND of its arguments, |
| 709 | which must be short integers. |
| 710 | |
| 711 | \section{Bitwise XOR expressions} |
| 712 | |
| 713 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 714 | xor_expr: and_expr | xor_expr "^" and_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 715 | \end{verbatim} |
| 716 | |
| 717 | This operator yields the bitwise exclusive OR of its arguments, |
| 718 | which must be short integers. |
| 719 | |
| 720 | \section{Bitwise OR expressions} |
| 721 | |
| 722 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 723 | or_expr: xor_expr | or_expr "|" xor_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 724 | \end{verbatim} |
| 725 | |
| 726 | This operator yields the bitwise OR of its arguments, |
| 727 | which must be short integers. |
| 728 | |
| 729 | \section{Expressions and expression lists} |
| 730 | |
| 731 | \begin{verbatim} |
| 732 | expression: or_expression |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 733 | expr_list: expression ("," expression)* [","] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 734 | \end{verbatim} |
| 735 | |
| 736 | An expression list containing at least one comma yields a new tuple. |
| 737 | The length of the tuple is the number of expressions in the list. |
| 738 | The expressions are evaluated from left to right. |
| 739 | |
| 740 | The trailing comma is required only to create a single tuple; |
| 741 | it is optional in all other cases (a single expression without |
| 742 | a trailing comma doesn't create a tuple, but rather yields the |
| 743 | value of that expression). |
| 744 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 745 | 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] | 746 | |
| 747 | \section{Comparisons} |
| 748 | |
| 749 | \begin{verbatim} |
| 750 | comparison: expression (comp_operator expression)* |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 751 | comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 752 | \end{verbatim} |
| 753 | |
| 754 | Comparisons yield integer value: 1 for true, 0 for false. |
| 755 | |
| 756 | Comparisons can be chained arbitrarily, |
| 757 | e.g., $x < y <= z$ is equivalent to |
| 758 | $x < y$ {\tt and} $y <= z$, except that $y$ is evaluated only once |
| 759 | (but in both cases $z$ is not evaluated at all when $x < y$ is |
| 760 | found to be false). |
| 761 | |
| 762 | Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to |
| 763 | $e_0 op_1 e_1$ {\tt and} $e_1 op_2 e_2$ {\tt and} ... {\tt and} |
| 764 | $e_{n-1} op_n e_n$, except that each expression is evaluated at most once. |
| 765 | |
| 766 | Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison |
| 767 | between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. |
| 768 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 769 | The forms \verb\<>\ and \verb\!=\ are equivalent. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 770 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 771 | The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "<>"} compare |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 772 | the values of two objects. The objects needn't have the same type. |
| 773 | If both are numbers, they are compared to a common type. |
| 774 | Otherwise, objects of different types {\em always} compare unequal, |
| 775 | and are ordered consistently but arbitrarily, except that |
| 776 | the value \verb\None\ compares smaller than the values of any other type. |
| 777 | |
| 778 | (This unusual |
| 779 | definition of comparison is done to simplify the definition of |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 780 | 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] | 781 | |
| 782 | Comparison of objects of the same type depends on the type: |
| 783 | |
| 784 | \begin{itemize} |
| 785 | \item Numbers are compared arithmetically. |
| 786 | \item Strings are compared lexicographically using the numeric |
| 787 | equivalents (the result of the built-in function ord()) |
| 788 | of their characters. |
| 789 | \item Tuples and lists are compared lexicographically |
| 790 | using comparison of corresponding items. |
| 791 | \item Dictionaries compare unequal unless they are the same object; |
| 792 | the choice whether one dictionary object is considered smaller |
| 793 | or larger than another one is made arbitrarily but |
| 794 | consistently within one execution of a program. |
| 795 | \item The latter rule is also used for most other built-in types. |
| 796 | \end{itemize} |
| 797 | |
| 798 | The operators \verb\in\ and \verb\not in\ test for sequence membership: |
| 799 | if $y$ is a sequence, $x {\tt in} y$ is true if and only if there exists |
| 800 | an index $i$ such that $x = y_i$. |
| 801 | $x {\tt not in} y$ yields the inverse truth value. |
| 802 | The exception {\tt TypeError} is raised when $y$ is not a sequence, |
| 803 | or when $y$ is a string and $x$ is not a string of length one. |
| 804 | |
| 805 | The operators \verb\is\ and \verb\is not\ compare object identity: |
| 806 | $x {\tt is} y$ is true if and only if $x$ and $y$ are the same object. |
| 807 | $x {\tt is not} y$ yields the inverse truth value. |
| 808 | |
| 809 | \section{Boolean operators} |
| 810 | |
| 811 | \begin{verbatim} |
| 812 | condition: or_test |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 813 | or_test: and_test | or_test "or" and_test |
| 814 | and_test: not_test | and_test "and" not_test |
| 815 | not_test: comparison | "not" not_test |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 816 | \end{verbatim} |
| 817 | |
| 818 | In the context of Boolean operators, and also when conditions are |
| 819 | used by control flow statements, the following values are interpreted |
| 820 | as false: None, numeric zero of all types, empty sequences (strings, |
| 821 | tuples and lists), and empty mappings (dictionaries). |
| 822 | All other values are interpreted as true. |
| 823 | |
| 824 | The operator \verb\not\ yields 1 if its argument is false, 0 otherwise. |
| 825 | |
| 826 | The condition $x {\tt and} y$ first evaluates $x$; if $x$ is false, |
| 827 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 828 | |
| 829 | The condition $x {\tt or} y$ first evaluates $x$; if $x$ is true, |
| 830 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 831 | |
| 832 | (Note that \verb\and\ and \verb\or\ do not restrict the value and type |
| 833 | they return to 0 and 1, but rather return the last evaluated argument. |
| 834 | This is sometimes useful, e.g., if $s$ is a string, which should be |
| 835 | replaced by a default value if it is empty, $s {\tt or} 'foo'$ |
| 836 | returns the desired value. Because \verb\not\ has to invent a value |
| 837 | anyway, it does not bother to return a value of the same type as its |
| 838 | argument, so \verb\not 'foo'\ yields $0$, not $''$.) |
| 839 | |
| 840 | \chapter{Simple statements} |
| 841 | |
| 842 | Simple statements are comprised within a single logical line. |
| 843 | Several simple statements may occor on a single line separated |
| 844 | by semicolons. The syntax for simple statements is: |
| 845 | |
| 846 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 847 | stmt_list: simple_stmt (";" simple_stmt)* [";"] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 848 | simple_stmt: expression_stmt |
| 849 | | assignment |
| 850 | | pass_stmt |
| 851 | | del_stmt |
| 852 | | print_stmt |
| 853 | | return_stmt |
| 854 | | raise_stmt |
| 855 | | break_stmt |
| 856 | | continue_stmt |
| 857 | | import_stmt |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 858 | | global_stmt |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 859 | \end{verbatim} |
| 860 | |
| 861 | \section{Expression statements} |
| 862 | |
| 863 | \begin{verbatim} |
| 864 | expression_stmt: expression_list |
| 865 | \end{verbatim} |
| 866 | |
| 867 | An expression statement evaluates the expression list (which may |
| 868 | be a single expression). |
| 869 | If the value is not \verb\None\, it is converted to a string |
| 870 | using the rules for string conversions, and the resulting string |
| 871 | is written to standard output on a line by itself. |
| 872 | |
| 873 | (The exception for \verb\None\ is made so that procedure calls, |
| 874 | which are syntactically equivalent to expressions, |
| 875 | do not cause any output.) |
| 876 | |
| 877 | \section{Assignments} |
| 878 | |
| 879 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 880 | assignment: target_list ("=" target_list)* "=" expression_list |
| 881 | target_list: target ("," target)* [","] |
| 882 | target: identifier | "(" target_list ")" | "[" target_list "]" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 883 | | attributeref | subscription | slicing |
| 884 | \end{verbatim} |
| 885 | |
| 886 | (See the section on primaries for the definition of the last |
| 887 | three symbols.) |
| 888 | |
| 889 | An assignment evaluates the expression list (remember that this can |
| 890 | be a single expression or a comma-separated list, |
| 891 | the latter yielding a tuple) |
| 892 | and assigns the single resulting object to each of the target lists, |
| 893 | from left to right. |
| 894 | |
| 895 | Assignment is defined recursively depending on the type of the |
| 896 | target. Where assignment is to part of a mutable object |
| 897 | (through an attribute reference, subscription or slicing), |
| 898 | the mutable object must ultimately perform the |
| 899 | assignment and decide about its validity, raising an exception |
| 900 | if the assignment is unacceptable. The rules observed by |
| 901 | various types and the exceptions raised are given with the |
| 902 | definition of the object types (some of which are defined |
| 903 | in the library reference). |
| 904 | |
| 905 | Assignment of an object to a target list is recursively |
| 906 | defined as follows. |
| 907 | |
| 908 | \begin{itemize} |
| 909 | \item |
| 910 | If the target list contains no commas (except in nested constructs): |
| 911 | the object is assigned to the single target contained in the list. |
| 912 | |
| 913 | \item |
| 914 | If the target list contains commas (that are not in nested constructs): |
| 915 | the object must be a tuple with as many items |
| 916 | as the list contains targets, and the items are assigned, from left |
| 917 | to right, to the corresponding targets. |
| 918 | |
| 919 | \end{itemize} |
| 920 | |
| 921 | Assignment of an object to a (non-list) |
| 922 | target is recursively defined as follows. |
| 923 | |
| 924 | \begin{itemize} |
| 925 | |
| 926 | \item |
| 927 | If the target is an identifier (name): |
| 928 | the object is bound to that name |
| 929 | in the current local scope. Any previous binding of the same name |
| 930 | is undone. |
| 931 | |
| 932 | \item |
| 933 | If the target is a target list enclosed in parentheses: |
| 934 | the object is assigned to that target list. |
| 935 | |
| 936 | \item |
| 937 | If the target is a target list enclosed in square brackets: |
| 938 | the object must be a list with as many items |
| 939 | as the target list contains targets, |
| 940 | and the list's items are assigned, from left to right, |
| 941 | to the corresponding targets. |
| 942 | |
| 943 | \item |
| 944 | If the target is an attribute reference: |
| 945 | The primary expression in the reference is evaluated. |
| 946 | It should yield an object with assignable attributes; |
| 947 | if this is not the case, a {\tt TypeError} exception is raised. |
| 948 | That object is then asked to assign the assigned object |
| 949 | to the given attribute; if it cannot perform the assignment, |
| 950 | it raises an exception. |
| 951 | |
| 952 | \item |
| 953 | If the target is a subscription: |
| 954 | The primary expression in the reference is evaluated. |
| 955 | It should yield either a mutable sequence object or a mapping |
| 956 | (dictionary) object. |
| 957 | Next, the subscript expression is evaluated. |
| 958 | |
| 959 | If the primary is a sequence object, the subscript must yield a |
| 960 | nonnegative integer smaller than the sequence's length, |
| 961 | and the sequence is asked to assign the assigned object |
| 962 | to its item with that index. |
| 963 | |
| 964 | If the primary is a mapping object, the subscript must have a |
| 965 | type compatible with the mapping's key type, |
| 966 | and the mapping is then asked to to create a key/datum pair |
| 967 | which maps the subscript to the assigned object. |
| 968 | |
| 969 | Various exceptions can be raised. |
| 970 | |
| 971 | \item |
| 972 | If the target is a slicing: |
| 973 | The primary expression in the reference is evaluated. |
| 974 | It should yield a mutable sequence object (currently only lists). |
| 975 | The assigned object should be a sequence object of the same type. |
| 976 | Next, the lower and upper bound expressions are evaluated, |
| 977 | insofar they are present; defaults are zero and the sequence's length. |
| 978 | The bounds should evaluate to (small) integers. |
| 979 | If either bound is negative, the sequence's length is added to it (once). |
| 980 | The resulting bounds are clipped to lie between zero |
| 981 | and the sequence's length, inclusive. |
| 982 | (XXX Shouldn't this description be with expressions?) |
| 983 | Finally, the sequence object is asked to replace the items |
| 984 | indicated by the slice with the items of the assigned sequence. |
| 985 | This may change the sequence's length, if it allows it. |
| 986 | |
| 987 | \end{itemize} |
| 988 | |
| 989 | (In the original implementation, the syntax for targets is taken |
| 990 | to be the same as for expressions, and invalid syntax is rejected |
| 991 | during the code generation phase, causing less detailed error |
| 992 | messages.) |
| 993 | |
| 994 | \section{The {\tt pass} statement} |
| 995 | |
| 996 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 997 | pass_stmt: "pass" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 998 | \end{verbatim} |
| 999 | |
| 1000 | {\tt pass} is a null operation -- when it is executed, |
| 1001 | nothing happens. |
| 1002 | |
| 1003 | \section{The {\tt del} statement} |
| 1004 | |
| 1005 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1006 | del_stmt: "del" target_list |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1007 | \end{verbatim} |
| 1008 | |
| 1009 | Deletion is recursively defined similar to assignment. |
| 1010 | |
| 1011 | (XXX Rather that spelling it out in full details, |
| 1012 | here are some hints.) |
| 1013 | |
| 1014 | Deletion of a target list recursively deletes each target, |
| 1015 | from left to right. |
| 1016 | |
| 1017 | Deletion of a name removes the binding of that name (which must exist) |
| 1018 | from the local scope. |
| 1019 | |
| 1020 | Deletion of attribute references, subscriptions and slicings |
| 1021 | is passed to the primary object involved; deletion of a slicing |
| 1022 | is in general equivalent to assignment of an empty slice of the |
| 1023 | right type (but even this is determined by the sliced object). |
| 1024 | |
| 1025 | \section{The {\tt print} statement} |
| 1026 | |
| 1027 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1028 | print_stmt: "print" [ condition ("," condition)* [","] ] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1029 | \end{verbatim} |
| 1030 | |
| 1031 | {\tt print} evaluates each condition in turn and writes the resulting |
| 1032 | object to standard output (see below). |
| 1033 | If an object is not a string, it is first converted to |
| 1034 | a string using the rules for string conversions. |
| 1035 | The (resulting or original) string is then written. |
| 1036 | A space is written before each object is (converted and) written, |
| 1037 | unless the output system believes it is positioned at the beginning |
| 1038 | of a line. This is the case: (1) when no characters have been written |
| 1039 | to standard output; or (2) when the last character written to |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 1040 | standard output is \verb/\n/; |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1041 | or (3) when the last I/O operation |
| 1042 | on standard output was not a \verb\print\ statement. |
| 1043 | |
| 1044 | Finally, |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 1045 | a \verb/\n/ character is written at the end, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1046 | unless the \verb\print\ statement ends with a comma. |
| 1047 | This is the only action if the statement contains just the keyword |
| 1048 | \verb\print\. |
| 1049 | |
| 1050 | Standard output is defined as the file object named \verb\stdout\ |
| 1051 | in the built-in module \verb\sys\. If no such object exists, |
| 1052 | or if it is not a writable file, a {\tt RuntimeError} exception is raised. |
| 1053 | (The original implementation attempts to write to the system's original |
| 1054 | standard output instead, but this is not safe, and should be fixed.) |
| 1055 | |
| 1056 | \section{The {\tt return} statement} |
| 1057 | |
| 1058 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1059 | return_stmt: "return" [condition_list] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1060 | \end{verbatim} |
| 1061 | |
| 1062 | \verb\return\ may only occur syntactically nested in a function |
| 1063 | definition, not within a nested class definition. |
| 1064 | |
| 1065 | If a condition list is present, it is evaluated, else \verb\None\ |
| 1066 | is substituted. |
| 1067 | |
| 1068 | \verb\return\ leaves the current function call with the condition |
| 1069 | list (or \verb\None\) as return value. |
| 1070 | |
| 1071 | When \verb\return\ passes control out of a \verb\try\ statement |
| 1072 | with a \verb\finally\ clause, that finally clause is executed |
| 1073 | before really leaving the function. |
| 1074 | (XXX This should be made more exact, a la Modula-3.) |
| 1075 | |
| 1076 | \section{The {\tt raise} statement} |
| 1077 | |
| 1078 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1079 | raise_stmt: "raise" condition ["," condition] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1080 | \end{verbatim} |
| 1081 | |
| 1082 | \verb\raise\ evaluates its first condition, which must yield |
| 1083 | a string object. If there is a second condition, this is evaluated, |
| 1084 | else \verb\None\ is substituted. |
| 1085 | |
| 1086 | It then raises the exception identified by the first object, |
| 1087 | with the second one (or \verb\None\) as its parameter. |
| 1088 | |
| 1089 | \section{The {\tt break} statement} |
| 1090 | |
| 1091 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1092 | break_stmt: "break" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1093 | \end{verbatim} |
| 1094 | |
| 1095 | \verb\break\ may only occur syntactically nested in a \verb\for\ |
| 1096 | or \verb\while\ loop, not nested in a function or class definition. |
| 1097 | |
| 1098 | It terminates the neares enclosing loop, skipping the optional |
| 1099 | \verb\else\ clause if the loop has one. |
| 1100 | |
| 1101 | If a \verb\for\ loop is terminated by \verb\break\, the loop control |
| 1102 | target (list) keeps its current value. |
| 1103 | |
| 1104 | When \verb\break\ passes control out of a \verb\try\ statement |
| 1105 | with a \verb\finally\ clause, that finally clause is executed |
| 1106 | before really leaving the loop. |
| 1107 | |
| 1108 | \section{The {\tt continue} statement} |
| 1109 | |
| 1110 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1111 | continue_stmt: "continue" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1112 | \end{verbatim} |
| 1113 | |
| 1114 | \verb\continue\ may only occur syntactically nested in a \verb\for\ |
| 1115 | or \verb\while\ loop, not nested in a function or class definition, |
| 1116 | and {\em not nested in a \verb\try\ statement with a \verb\finally\ |
| 1117 | clause}. |
| 1118 | |
| 1119 | It continues with the next cycle of the nearest enclosing loop. |
| 1120 | |
| 1121 | \section{The {\tt import} statement} |
| 1122 | |
| 1123 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1124 | import_stmt: "import" identifier ("," identifier)* |
| 1125 | | "from" identifier "import" identifier ("," identifier)* |
| 1126 | | "from" identifier "import" "*" |
| 1127 | \end{verbatim} |
| 1128 | |
| 1129 | (XXX To be done.) |
| 1130 | |
| 1131 | \section{The {\tt global} statement} |
| 1132 | |
| 1133 | \begin{verbatim} |
| 1134 | global_stmt: "global" identifier ("," identifier)* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1135 | \end{verbatim} |
| 1136 | |
| 1137 | (XXX To be done.) |
| 1138 | |
| 1139 | \chapter{Compound statements} |
| 1140 | |
| 1141 | (XXX The semantic definitions of this chapter are still to be done.) |
| 1142 | |
| 1143 | \begin{verbatim} |
| 1144 | statement: stmt_list NEWLINE | compound_stmt |
| 1145 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 1146 | suite: statement | NEWLINE INDENT statement+ DEDENT |
| 1147 | \end{verbatim} |
| 1148 | |
| 1149 | \section{The {\tt if} statement} |
| 1150 | |
| 1151 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1152 | if_stmt: "if" condition ":" suite |
| 1153 | ("elif" condition ":" suite)* |
| 1154 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1155 | \end{verbatim} |
| 1156 | |
| 1157 | \section{The {\tt while} statement} |
| 1158 | |
| 1159 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1160 | while_stmt: "while" condition ":" suite ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1161 | \end{verbatim} |
| 1162 | |
| 1163 | \section{The {\tt for} statement} |
| 1164 | |
| 1165 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1166 | for_stmt: "for" target_list "in" condition_list ":" suite |
| 1167 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1168 | \end{verbatim} |
| 1169 | |
| 1170 | \section{The {\tt try} statement} |
| 1171 | |
| 1172 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1173 | try_stmt: "try" ":" suite |
| 1174 | ("except" condition ["," condition] ":" suite)* |
| 1175 | ["finally" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1176 | \end{verbatim} |
| 1177 | |
| 1178 | \section{Function definitions} |
| 1179 | |
| 1180 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1181 | funcdef: "def" identifier "(" [parameter_list] ")" ":" suite |
| 1182 | parameter_list: parameter ("," parameter)* |
| 1183 | parameter: identifier | "(" parameter_list ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1184 | \end{verbatim} |
| 1185 | |
| 1186 | \section{Class definitions} |
| 1187 | |
| 1188 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1189 | classdef: "class" identifier [inheritance] ":" suite |
| 1190 | inheritance: "(" expression ("," expression)* ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1191 | \end{verbatim} |
| 1192 | |
| 1193 | XXX Syntax for scripts, modules |
| 1194 | XXX Syntax for interactive input, eval, exec, input |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1195 | XXX New definition of expressions (as conditions) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1196 | |
| 1197 | \end{document} |