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