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