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 | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 3 | \documentstyle[11pt,myformat]{report} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 26 | Python is a simple, yet powerful, interpreted programming language |
| 27 | that bridges the gap between C and shell programming, and is thus |
| 28 | ideally suited for ``throw-away programming'' and rapid prototyping. |
| 29 | Its syntax is put together from constructs borrowed from a variety of |
| 30 | other languages; most prominent are influences from ABC, C, Modula-3 |
| 31 | and Icon. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 43 | the language. It is terse, but attempts to be exact and complete. |
| 44 | The semantics of non-essential built-in object types and of the |
| 45 | built-in functions and modules are described in the {\em Python |
| 46 | Library Reference}. For an informal introduction to the language, see |
| 47 | the {\em Python Tutorial}. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 48 | |
| 49 | \end{abstract} |
| 50 | |
| 51 | \pagebreak |
| 52 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 53 | { |
| 54 | \parskip = 0mm |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 55 | \tableofcontents |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 56 | } |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 57 | |
| 58 | \pagebreak |
| 59 | |
| 60 | \pagenumbering{arabic} |
| 61 | |
| 62 | \chapter{Introduction} |
| 63 | |
| 64 | This reference manual describes the Python programming language. |
| 65 | It is not intended as a tutorial. |
| 66 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 67 | While I am trying to be as precise as possible, I chose to use English |
| 68 | rather than formal specifications for everything except syntax and |
| 69 | lexical analysis. This should make the document better understandable |
| 70 | to the average reader, but will leave room for ambiguities. |
| 71 | 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] | 72 | Python from this document alone, you might have to guess things and in |
| 73 | fact you would be implementing quite a different language. |
| 74 | On the other hand, if you are using |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 75 | 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] | 76 | 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] | 77 | |
| 78 | It is dangerous to add too many implementation details to a language |
| 79 | reference document -- the implementation may change, and other |
| 80 | implementations of the same language may work differently. On the |
| 81 | other hand, there is currently only one Python implementation, and |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 82 | its particular quirks are sometimes worth being mentioned, especially |
| 83 | where the implementation imposes additional limitations. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 84 | |
| 85 | Every Python implementation comes with a number of built-in and |
| 86 | standard modules. These are not documented here, but in the separate |
| 87 | {\em Python Library Reference} document. A few built-in modules are |
| 88 | mentioned when they interact in a significant way with the language |
| 89 | definition. |
| 90 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 91 | \section{Warning} |
| 92 | |
| 93 | This version of the manual is incomplete. Sections that still need to |
| 94 | be written or need considerable work are marked with ``XXX''. |
| 95 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 96 | \section{Notation} |
| 97 | |
| 98 | The descriptions of lexical analysis and syntax use a modified BNF |
| 99 | grammar notation. This uses the following style of definition: |
| 100 | |
| 101 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 102 | name: lc_letter (lc_letter | "_")* |
| 103 | lc_letter: "a"..."z" |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 104 | \end{verbatim} |
| 105 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 106 | The first line says that a \verb\name\ is an \verb\lc_letter\ followed by |
| 107 | a sequence of zero or more \verb\lc_letter\s and underscores. An |
| 108 | \verb\lc_letter\ in turn is any of the single characters `a' through `z'. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 109 | (This rule is actually adhered to for the names defined in syntax and |
| 110 | grammar rules in this document.) |
| 111 | |
| 112 | Each rule begins with a name (which is the name defined by the rule) |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 113 | and a colon. A vertical bar |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 114 | (\verb\|\) is used to separate alternatives; it is the least binding |
| 115 | operator in this notation. A star (\verb\*\) means zero or more |
| 116 | repetitions of the preceding item; likewise, a plus (\verb\+\) means |
| 117 | one or more repetitions, and a question mark (\verb\?\) zero or one |
| 118 | (in other words, the preceding item is optional). These three |
| 119 | operators bind as tightly as possible; parentheses are used for |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 120 | grouping. Literal strings are enclosed in double quotes. White space |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 121 | is only meaningful to separate tokens. Rules are normally contained |
| 122 | on a single line; rules with many alternatives may be formatted |
| 123 | alternatively with each line after the first beginning with a |
| 124 | vertical bar. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 125 | |
| 126 | In lexical definitions (as the example above), two more conventions |
| 127 | are used: Two literal characters separated by three dots mean a choice |
| 128 | of any single character in the given (inclusive) range of ASCII |
| 129 | characters. A phrase between angular brackets (\verb\<...>\) gives an |
| 130 | informal description of the symbol defined; e.g., this could be used |
| 131 | to describe the notion of `control character' if needed. |
| 132 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 133 | 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] | 134 | difference between the meaning of lexical and syntactic definitions: |
| 135 | a lexical definition operates on the individual characters of the |
| 136 | input source, while a syntax definition operates on the stream of |
| 137 | tokens generated by the lexical analysis. |
| 138 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 139 | \chapter{Lexical analysis} |
| 140 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 141 | A Python program is read by a {\em parser}. Input to the parser is a |
| 142 | stream of {\em tokens}, generated by the {\em lexical analyzer}. This |
| 143 | chapter describes how the lexical analyzer breaks a file into tokens. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 144 | |
| 145 | \section{Line structure} |
| 146 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 147 | A Python program is divided in a number of logical lines. The end of |
| 148 | a logical line is represented by the token NEWLINE. Statements cannot |
| 149 | cross logical line boundaries except where NEWLINE is allowed by the |
| 150 | syntax (e.g., between statements in compound statements). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 151 | |
| 152 | \subsection{Comments} |
| 153 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 154 | 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] | 155 | a string literal, and ends at the end of the physical line. A comment |
| 156 | always signifies the end of the logical line. Comments are ignored by |
| 157 | the syntax. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 158 | |
| 159 | \subsection{Line joining} |
| 160 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 161 | 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] | 162 | backslash characters (\verb/\/), as follows: when a physical line ends |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 163 | in a backslash that is not part of a string literal or comment, it is |
| 164 | joined with the following forming a single logical line, deleting the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 165 | backslash and the following end-of-line character. For example: |
| 166 | % |
| 167 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 168 | moth_names = ['Januari', 'Februari', 'Maart', \ |
| 169 | 'April', 'Mei', 'Juni', \ |
| 170 | 'Juli', 'Augustus', 'September', \ |
| 171 | 'Oktober', 'November', 'December'] |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 172 | \end{verbatim} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 173 | |
| 174 | \subsection{Blank lines} |
| 175 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 176 | A logical line that contains only spaces, tabs, and possibly a |
| 177 | comment, is ignored (i.e., no NEWLINE token is generated), except that |
| 178 | during interactive input of statements, an entirely blank logical line |
| 179 | terminates a multi-line statement. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 180 | |
| 181 | \subsection{Indentation} |
| 182 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 183 | Leading whitespace (spaces and tabs) at the beginning of a logical |
| 184 | line is used to compute the indentation level of the line, which in |
| 185 | turn is used to determine the grouping of statements. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 187 | First, tabs are replaced (from left to right) by one to eight spaces |
| 188 | such that the total number of characters up to there is a multiple of |
| 189 | eight (this is intended to be the same rule as used by UNIX). The |
| 190 | total number of spaces preceding the first non-blank character then |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 191 | determines the line's indentation. Indentation cannot be split over |
| 192 | multiple physical lines using backslashes. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 193 | |
| 194 | The indentation levels of consecutive lines are used to generate |
| 195 | INDENT and DEDENT tokens, using a stack, as follows. |
| 196 | |
| 197 | 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] | 198 | the stack; this will never be popped off again. The numbers pushed on |
| 199 | the stack will always be strictly increasing from bottom to top. At |
| 200 | the beginning of each logical line, the line's indentation level is |
| 201 | compared to the top of the stack. If it is equal, nothing happens. |
| 202 | If it larger, it is pushed on the stack, and one INDENT token is |
| 203 | generated. If it is smaller, it {\em must} be one of the numbers |
| 204 | occurring on the stack; all numbers on the stack that are larger are |
| 205 | popped off, and for each number popped off a DEDENT token is |
| 206 | generated. At the end of the file, a DEDENT token is generated for |
| 207 | each number remaining on the stack that is larger than zero. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 209 | Here is an example of a correctly (though confusingly) indented piece |
| 210 | of Python code: |
| 211 | |
| 212 | \begin{verbatim} |
| 213 | def perm(l): |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 366 | Plain integer decimal literals must be at most $2^{31} - 1$ (i.e., the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 371 | Some examples of plain and long integer literals: |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 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 | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 389 | The allowed range of floating point literals is |
| 390 | implementation-dependent. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 391 | |
| 392 | Some examples of floating point literals: |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 393 | |
| 394 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 395 | 3.14 10. .001 1e100 3.14e-10 |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 396 | \end{verbatim} |
| 397 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 398 | Note that numeric literals do not include a sign; a phrase like |
| 399 | \verb\-1\ is actually an expression composed of the operator |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 400 | \verb\-\ and the literal \verb\1\. |
| 401 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 402 | \section{Operators} |
| 403 | |
| 404 | The following tokens are operators: |
| 405 | |
| 406 | \begin{verbatim} |
| 407 | + - * / % |
| 408 | << >> & | ^ ~ |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 409 | < == > <= <> != >= |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 410 | \end{verbatim} |
| 411 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 412 | The comparison operators \verb\<>\ and \verb\!=\ are alternate |
| 413 | spellings of the same operator. |
| 414 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 415 | \section{Delimiters} |
| 416 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 417 | The following tokens serve as delimiters or otherwise have a special |
| 418 | meaning: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 419 | |
| 420 | \begin{verbatim} |
| 421 | ( ) [ ] { } |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 422 | ; , : . ` = |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 423 | \end{verbatim} |
| 424 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 425 | The following printing ASCII characters are not used in Python (except |
| 426 | in string literals and in comments). Their occurrence is an |
| 427 | unconditional error: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 428 | |
| 429 | \begin{verbatim} |
| 430 | ! @ $ " ? |
| 431 | \end{verbatim} |
| 432 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 433 | They may be used by future versions of the language though! |
| 434 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 435 | \chapter{Execution model} |
| 436 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 437 | \section{Objects, values and types} |
| 438 | |
| 439 | I won't try to define rigorously here what an object is, but I'll give |
| 440 | some properties of objects that are important to know about. |
| 441 | |
| 442 | Every object has an identity, a type and a value. An object's {\em |
| 443 | identity} never changes once it has been created; think of it as the |
| 444 | object's (permanent) address. An object's {\em type} determines the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 445 | operations that an object supports (e.g., does it have a length?) and |
| 446 | also defines the ``meaning'' of the object's value. The type also |
| 447 | never changes. The {\em value} of some objects can change; whether |
| 448 | this is possible is a property of its type. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 449 | |
| 450 | Objects are never explicitly destroyed; however, when they become |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 451 | unreachable they may be garbage-collected. An implementation is |
| 452 | allowed to delay garbage collection or omit it altogether -- it is a |
| 453 | matter of implementation quality how garbage collection is |
| 454 | implemented, as long as no objects are collected that are still |
| 455 | reachable. (Implementation note: the current implementation uses a |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 456 | reference-counting scheme which collects most objects as soon as they |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 457 | become unreachable, but never collects garbage containing circular |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 458 | references.) |
| 459 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 460 | Note that the use of the implementation's tracing or debugging |
| 461 | facilities may keep objects alive that would normally be collectable. |
| 462 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 463 | (Some objects contain references to ``external'' resources such as |
| 464 | open files. It is understood that these resources are freed when the |
| 465 | object is garbage-collected, but since garbage collection is not |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 466 | guaranteed, such objects also provide an explicit way to release the |
| 467 | external resource (e.g., a \verb\close\ method). Programs are strongly |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 468 | recommended to use this.) |
| 469 | |
| 470 | Some objects contain references to other objects. These references |
| 471 | are part of the object's value; in most cases, when such a |
| 472 | ``container'' object is compared to another (of the same type), the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 473 | comparison applies to the {\em values} of the referenced objects (not |
| 474 | their identities). |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 475 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 476 | Types affect almost all aspects of objects. |
| 477 | Even object identity is affected in some sense: for immutable |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 478 | types, operations that compute new values may actually return a |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 479 | 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] | 480 | for mutable objects this is not allowed. E.g., after |
| 481 | |
| 482 | \begin{verbatim} |
| 483 | a = 1; b = 1; c = []; d = [] |
| 484 | \end{verbatim} |
| 485 | |
| 486 | \verb\a\ and \verb\b\ may or may not refer to the same object, but |
| 487 | \verb\c\ and \verb\d\ are guaranteed to refer to two different, unique, |
| 488 | newly created lists. |
| 489 | |
| 490 | \section{Execution frames, name spaces, and scopes} |
| 491 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 492 | XXX code blocks, scopes, name spaces, name binding, exceptions |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 494 | \chapter{The standard type hierarchy} |
| 495 | |
| 496 | The following types are built into Python. Extension modules |
| 497 | written in C can define additional types. Future versions of Python |
| 498 | may also add types to the type hierarchy (e.g., rational or complex |
| 499 | numbers, lists of efficiently stored integers, etc.). |
| 500 | |
| 501 | \begin{description} |
| 502 | |
| 503 | \item[None] |
| 504 | This type has a single value. There is a single object with this value. |
| 505 | This object is accessed through the built-in name \verb\None\. |
| 506 | It is returned from functions that don't explicitly return an object. |
| 507 | |
| 508 | \item[Numbers] |
| 509 | These are created by numeric literals and returned as results |
| 510 | by arithmetic operators and arithmetic built-in functions. |
| 511 | Numeric objects are immutable; once created their value never changes. |
| 512 | Python numbers are of course strongly related to mathematical numbers, |
| 513 | but subject to the limitations of numerical representation in computers. |
| 514 | |
| 515 | Python distinguishes between integers and floating point numbers: |
| 516 | |
| 517 | \begin{description} |
| 518 | \item[Integers] |
| 519 | These represent elements from the mathematical set of whole numbers. |
| 520 | |
| 521 | There are two types of integers: |
| 522 | |
| 523 | \begin{description} |
| 524 | |
| 525 | \item[Plain integers] |
| 526 | These represent numbers in the range $-2^{31}$ through $2^{31}-1$. |
| 527 | (The range may be larger on machines with a larger natural word |
| 528 | size, but not smaller.) |
| 529 | When the result of an operation falls outside this range, the |
| 530 | exception \verb\OverflowError\ is raised. |
| 531 | For the purpose of shift and mask operations, integers are assumed to |
| 532 | have a binary, 2's complement notation using 32 or more bits, and |
| 533 | hiding no bits from the user (i.e., all $2^{32}$ different bit |
| 534 | patterns correspond to different values). |
| 535 | |
| 536 | \item[Long integers] |
| 537 | These represent numbers in an unlimited range, subject to avaiable |
| 538 | (virtual) memory only. For the purpose of shift and mask operations, |
| 539 | a binary representation is assumed, and negative numbers are |
| 540 | represented in a variant of 2's complement which gives the illusion of |
| 541 | an infinite string of sign bits extending to the left. |
| 542 | |
| 543 | \end{description} % Integers |
| 544 | |
| 545 | The rules for integer representation are intended to give the most |
| 546 | meaningful interpretation of shift and mask operations involving |
| 547 | negative integers and the least surprises when switching between the |
| 548 | plain and long integer domains. For any operation except left shift, |
| 549 | if it yields a result in the plain integer domain without causing |
| 550 | overflow, it will yield the same result in the long integer domain or |
| 551 | when using mixed operands. |
| 552 | |
| 553 | \item[Floating point numbers] |
| 554 | These represent machine-level double precision floating point numbers. |
| 555 | You are at the mercy of the underlying machine architecture and |
| 556 | C implementation for the accepted range and handling of overflow. |
| 557 | |
| 558 | \end{description} % Numbers |
| 559 | |
| 560 | \item[Sequences] |
| 561 | These represent finite ordered sets indexed by natural numbers. |
| 562 | The built-in function \verb\len()\ returns the number of elements |
| 563 | of a sequence. When this number is $n$, the index set contains |
| 564 | the numbers $0, 1, \ldots, n-1$. Element \verb\i\ of sequence |
| 565 | \verb\a\ is selected by \verb\a[i]\. |
| 566 | |
| 567 | Sequences also support slicing: \verb\a[i:j]\ selects all elements |
| 568 | with index $k$ such that $i < k < j$. When used as an expression, |
| 569 | a slice is a sequence of the same type -- this implies that the |
| 570 | index set is renumbered so that it starts at 0 again. |
| 571 | |
| 572 | Sequences are distinguished according to their mutability: |
| 573 | |
| 574 | \begin{description} |
| 575 | % |
| 576 | \item[Immutable sequences] |
| 577 | An object of an immutable sequence type cannot change once it is |
| 578 | created. (If the object contains references to other objects, |
| 579 | these other objects may be mutable and may be changed; however |
| 580 | the collection of objects directly referenced by an immutable object |
| 581 | cannot change.) |
| 582 | |
| 583 | The following types are immutable sequences: |
| 584 | |
| 585 | \begin{description} |
| 586 | |
| 587 | \item[Strings] |
| 588 | The elements of a string are characters. There is no separate |
| 589 | character type; a character is represented by a string of one element. |
| 590 | Characters represent (at least) 8-bit bytes. The built-in |
| 591 | functions \verb\chr()\ and \verb\ord()\ convert between characters |
| 592 | and nonnegative integers representing the byte values. |
| 593 | Bytes with the values 0-127 represent the corresponding ASCII values. |
| 594 | |
| 595 | (On systems whose native character set is not ASCII, strings may use |
| 596 | EBCDIC in their internal representation, provided the functions |
| 597 | \verb\chr()\ and \verb\ord()\ implement a mapping between ASCII and |
| 598 | EBCDIC, and string comparisons preserve the ASCII order. |
| 599 | Or perhaps someone can propose a better rule?) |
| 600 | |
| 601 | \item[Tuples] |
| 602 | The elements of a tuple are arbitrary Python objects. |
| 603 | Tuples of two or more elements are formed by comma-separated lists |
| 604 | of expressions. A tuple of one element can be formed by affixing |
| 605 | a comma to an expression (an expression by itself of course does |
| 606 | not create a tuple). An empty tuple can be formed by enclosing |
| 607 | `nothing' in parentheses. |
| 608 | |
| 609 | \end{description} % Immutable sequences |
| 610 | |
| 611 | \item[Mutable sequences] |
| 612 | Mutable sequences can be changed after they are created. |
| 613 | The subscript and slice notations can be used as the target |
| 614 | of assignment and \verb\del\ (delete) statements. |
| 615 | |
| 616 | There is currently a single mutable sequence type: |
| 617 | |
| 618 | \begin{description} |
| 619 | |
| 620 | \item[Lists] |
| 621 | The elements of a list are arbitrary Python objects. |
| 622 | Lists are formed by placing a comma-separated list of expressions |
| 623 | in square brackets. (Note that there are no special cases for lists |
| 624 | of length 0 or 1.) |
| 625 | |
| 626 | \end{description} % Mutable sequences |
| 627 | |
| 628 | \end{description} % Sequences |
| 629 | |
| 630 | \item[Mapping types] |
| 631 | These represent finite sets of objects indexed by arbitrary index sets. |
| 632 | The subscript notation \verb\a[k]\ selects the element indexed |
| 633 | by \verb\k\ from the mapping \verb\a\; this can be used in |
| 634 | expressions and as the target of assignments or \verb\del\ statements. |
| 635 | The built-in function \verb\len()\ returns the number of elements |
| 636 | in a mapping. |
| 637 | |
| 638 | There is currently a single mapping type: |
| 639 | |
| 640 | \begin{description} |
| 641 | |
| 642 | \item[Dictionaries] |
| 643 | These represent finite sets of objects indexed by strings. |
| 644 | Dictionaries are created by the \verb\{...}\ notation (see section |
| 645 | \ref{dict}). (Implementation note: the strings used for indexing must |
| 646 | not contain null bytes.) |
| 647 | |
| 648 | \end{description} % Mapping types |
| 649 | |
| 650 | \item[Callable types] |
| 651 | These are the types to which the function call operation can be applied: |
| 652 | |
| 653 | \begin{description} |
| 654 | \item[User-defined functions] |
| 655 | XXX |
| 656 | \item[Built-in functions] |
| 657 | XXX |
| 658 | \item[User-defined methods] |
| 659 | XXX |
| 660 | \item[Built-in methods] |
| 661 | XXX |
| 662 | \item[User-defined classes] |
| 663 | XXX |
| 664 | \end{description} |
| 665 | |
| 666 | \item[Modules] |
| 667 | XXX |
| 668 | |
| 669 | \item[Class instances] |
| 670 | XXX |
| 671 | |
| 672 | \item[Files] |
| 673 | XXX |
| 674 | |
| 675 | \item[Internal types] |
| 676 | A few types used internally by the interpreter are exposed to the user. |
| 677 | Their definition may change with future versions of the interpreter, |
| 678 | but they are mentioned here for completeness. |
| 679 | |
| 680 | \begin{description} |
| 681 | \item[Code objects] |
| 682 | XXX |
| 683 | \item[Traceback objects] |
| 684 | XXX |
| 685 | \end{description} % Internal types |
| 686 | |
| 687 | \end{description} % Types |
| 688 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 689 | \chapter{Expressions and conditions} |
| 690 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 691 | From now on, extended BNF notation will be used to describe syntax, |
| 692 | not lexical analysis. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 693 | |
| 694 | This chapter explains the meaning of the elements of expressions and |
| 695 | conditions. Conditions are a superset of expressions, and a condition |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 696 | may be used wherever an expression is required by enclosing it in |
| 697 | parentheses. The only places where expressions are used in the syntax |
| 698 | instead of conditions is in expression statements and on the |
| 699 | right-hand side of assignments; this catches some nasty bugs like |
| 700 | accedentally writing \verb\x == 1\ instead of \verb\x = 1\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 701 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 702 | 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] | 703 | operator with a lower precedence than all others, but occasionally |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 704 | serves other purposes as well; e.g., it separates function arguments, |
| 705 | is used in list and dictionary constructors, and has special semantics |
| 706 | in \verb\print\ statements. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 707 | |
| 708 | When (one alternative of) a syntax rule has the form |
| 709 | |
| 710 | \begin{verbatim} |
| 711 | name: othername |
| 712 | \end{verbatim} |
| 713 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 714 | and no semantics are given, the semantics of this form of \verb\name\ |
| 715 | are the same as for \verb\othername\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 716 | |
| 717 | \section{Arithmetic conversions} |
| 718 | |
| 719 | When a description of an arithmetic operator below uses the phrase |
| 720 | ``the numeric arguments are converted to a common type'', |
| 721 | this both means that if either argument is not a number, a |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 722 | \verb\TypeError\ exception is raised, and that otherwise |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 723 | the following conversions are applied: |
| 724 | |
| 725 | \begin{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 726 | \item first, if either argument is a floating point number, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 727 | the other is converted to floating point; |
| 728 | \item else, if either argument is a long integer, |
| 729 | the other is converted to long integer; |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 730 | \item otherwise, both must be plain integers and no conversion |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 731 | is necessary. |
| 732 | \end{itemize} |
| 733 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 734 | \section{Atoms} |
| 735 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 736 | Atoms are the most basic elements of expressions. Forms enclosed in |
| 737 | reverse quotes or in parentheses, brackets or braces are also |
| 738 | categorized syntactically as atoms. The syntax for atoms is: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 739 | |
| 740 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 741 | atom: identifier | literal | enclosure |
| 742 | enclosure: parenth_form | list_display | dict_display | string_conversion |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 743 | \end{verbatim} |
| 744 | |
| 745 | \subsection{Identifiers (Names)} |
| 746 | |
| 747 | 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] | 748 | or built-in name binding. If a name can be assigned to anywhere in a |
| 749 | code block, and is not mentioned in a \verb\global\ statement in that |
| 750 | 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] | 751 | Otherwise, it refers to a global name if one exists, else to a |
| 752 | built-in name. |
| 753 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 754 | When the name is bound to an object, evaluation of the atom yields |
| 755 | that object. When a name is not bound, an attempt to evaluate it |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 756 | raises a \verb\NameError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 757 | |
| 758 | \subsection{Literals} |
| 759 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 760 | Python knows string and numeric literals: |
| 761 | |
| 762 | \begin{verbatim} |
| 763 | literal: stringliteral | integer | longinteger | floatnumber |
| 764 | \end{verbatim} |
| 765 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 766 | Evaluation of a literal yields an object of the given type |
| 767 | (string, integer, long integer, floating point number) |
| 768 | with the given value. |
| 769 | The value may be approximated in the case of floating point literals. |
| 770 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 771 | All literals correspond to immutable data types, and hence the |
| 772 | object's identity is less important than its value. Multiple |
| 773 | evaluations of literals with the same value (either the same |
| 774 | occurrence in the program text or a different occurrence) may obtain |
| 775 | the same object or a different object with the same value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 776 | |
| 777 | (In the original implementation, all literals in the same code block |
| 778 | with the same type and value yield the same object.) |
| 779 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 780 | \subsection{Parenthesized forms} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 782 | A parenthesized form is an optional condition list enclosed in |
| 783 | parentheses: |
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 | \begin{verbatim} |
| 786 | parenth_form: "(" [condition_list] ")" |
| 787 | \end{verbatim} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 789 | A parenthesized condition list yields whatever that condition list |
| 790 | yields. |
| 791 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 792 | An empty pair of parentheses yields an empty tuple object. Since |
| 793 | tuples are immutable, the rules for literals apply here. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 794 | |
| 795 | (Note that tuples are not formed by the parentheses, but rather by use |
| 796 | of the comma operator. The exception is the empty tuple, for which |
| 797 | parentheses {\em are} required -- allowing unparenthesized ``nothing'' |
| 798 | in expressions would causes ambiguities and allow common typos to |
| 799 | pass uncaught.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 800 | |
| 801 | \subsection{List displays} |
| 802 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 803 | A list display is a possibly empty series of conditions enclosed in |
| 804 | square brackets: |
| 805 | |
| 806 | \begin{verbatim} |
| 807 | list_display: "[" [condition_list] "]" |
| 808 | \end{verbatim} |
| 809 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 810 | A list display yields a new list object. |
| 811 | |
| 812 | If it has no condition list, the list object has no items. |
| 813 | Otherwise, the elements of the condition list are evaluated |
| 814 | from left to right and inserted in the list object in that order. |
| 815 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 816 | \subsection{Dictionary displays} \label{dict} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 817 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 818 | A dictionary display is a possibly empty series of key/datum pairs |
| 819 | enclosed in curly braces: |
| 820 | |
| 821 | \begin{verbatim} |
| 822 | dict_display: "{" [key_datum_list] "}" |
| 823 | key_datum_list: [key_datum ("," key_datum)* [","] |
| 824 | key_datum: condition ":" condition |
| 825 | \end{verbatim} |
| 826 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 827 | A dictionary display yields a new dictionary object. |
| 828 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 829 | The key/datum pairs are evaluated from left to right to define the |
| 830 | entries of the dictionary: each key object is used as a key into the |
| 831 | dictionary to store the corresponding datum. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 832 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 833 | Keys must be strings, otherwise a \verb\TypeError\ exception is raised. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 834 | Clashes between duplicate keys are not detected; the last datum |
| 835 | (textually rightmost in the display) stored for a given key value |
| 836 | prevails. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 837 | |
| 838 | \subsection{String conversions} |
| 839 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 840 | A string conversion is a condition list enclosed in reverse (or |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 841 | backward) quotes: |
| 842 | |
| 843 | \begin{verbatim} |
| 844 | string_conversion: "`" condition_list "`" |
| 845 | \end{verbatim} |
| 846 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 847 | A string conversion evaluates the contained condition list and converts the |
| 848 | resulting object into a string according to rules specific to its type. |
| 849 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 850 | 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] | 851 | dictionary containing only objects whose type is one of these, the |
| 852 | resulting string is a valid Python expression which can be passed to |
| 853 | 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] | 854 | same value (or an approximation, if floating point numbers are |
| 855 | involved). |
| 856 | |
| 857 | (In particular, converting a string adds quotes around it and converts |
| 858 | ``funny'' characters to escape sequences that are safe to print.) |
| 859 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 860 | It is illegal to attempt to convert recursive objects (e.g., lists or |
| 861 | dictionaries that contain a reference to themselves, directly or |
| 862 | indirectly.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 863 | |
| 864 | \section{Primaries} |
| 865 | |
| 866 | Primaries represent the most tightly bound operations of the language. |
| 867 | Their syntax is: |
| 868 | |
| 869 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 870 | primary: atom | attributeref | subscription | slicing | call |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 871 | \end{verbatim} |
| 872 | |
| 873 | \subsection{Attribute references} |
| 874 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 875 | An attribute reference is a primary followed by a period and a name: |
| 876 | |
| 877 | \begin{verbatim} |
| 878 | attributeref: primary "." identifier |
| 879 | \end{verbatim} |
| 880 | |
| 881 | The primary must evaluate to an object of a type that supports |
| 882 | attribute references, e.g., a module or a list. This object is then |
| 883 | asked to produce the attribute whose name is the identifier. If this |
| 884 | attribute is not available, the exception \verb\AttributeError\ is |
| 885 | raised. Otherwise, the type and value of the object produced is |
| 886 | determined by the object. Multiple evaluations of the same attribute |
| 887 | reference may yield different objects. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 888 | |
| 889 | \subsection{Subscriptions} |
| 890 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 891 | A subscription selects an item of a sequence or mapping object: |
| 892 | |
| 893 | \begin{verbatim} |
| 894 | subscription: primary "[" condition "]" |
| 895 | \end{verbatim} |
| 896 | |
| 897 | The primary must evaluate to an object of a sequence or mapping type. |
| 898 | |
| 899 | If it is a mapping, the condition must evaluate to an object whose |
| 900 | value is one of the keys of the mapping, and the subscription selects |
| 901 | the value in the mapping that corresponds to that key. |
| 902 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 903 | If it is a sequence, the condition must evaluate to a plain integer. |
| 904 | If this value is negative, the length of the sequence is added to it |
| 905 | (so that, e.g., \verb\x[-1]\ selects the last item of \verb\x\.) |
| 906 | The resulting value must be a nonnegative integer smaller than the |
| 907 | number of items in the sequence, and the subscription selects the item |
| 908 | whose index is that value (counting from zero). |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 909 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 910 | A string's items are characters. A character is not a separate data |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 911 | type but a string of exactly one character. |
| 912 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 913 | \subsection{Slicings} |
| 914 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 915 | A slicing selects a range of items in a sequence object: |
| 916 | |
| 917 | \begin{verbatim} |
| 918 | slicing: primary "[" [condition] ":" [condition] "]" |
| 919 | \end{verbatim} |
| 920 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 921 | The primary must evaluate to a sequence object. The lower and upper |
| 922 | bound expressions, if present, must evaluate to plain integers; |
| 923 | defaults are zero and the sequence's length, respectively. If either |
| 924 | bound is negative, the sequence's length is added to it. The slicing |
| 925 | now selects all items with index $k$ such that $i <= k < j$ where $i$ |
| 926 | and $j$ are the specified lower and upper bounds. This may be an |
| 927 | empty sequence. It is not an error if $i$ or $j$ lie outside the |
| 928 | range of valid indexes (such items don't exist so they aren't |
| 929 | selected). |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 930 | |
| 931 | \subsection{Calls} |
| 932 | |
| 933 | A call calls a function with a possibly empty series of arguments: |
| 934 | |
| 935 | \begin{verbatim} |
| 936 | call: primary "(" [condition_list] ")" |
| 937 | \end{verbatim} |
| 938 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 939 | The primary must evaluate to a callable object (user-defined |
| 940 | functions, built-in functions, methods of built-in objects, class |
| 941 | objects, and methods of class instances are callable). If it is a |
| 942 | class, the argument list must be empty. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 943 | |
| 944 | XXX explain what happens on function call |
| 945 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 946 | \section{Factors} |
| 947 | |
| 948 | Factors represent the unary numeric operators. |
| 949 | Their syntax is: |
| 950 | |
| 951 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 952 | factor: primary | "-" factor | "+" factor | "~" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 953 | \end{verbatim} |
| 954 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 955 | The unary \verb\"-"\ operator yields the negative of its |
| 956 | numeric argument. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 957 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 958 | The unary \verb\"+"\ operator yields its numeric argument unchanged. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 960 | The unary \verb\"~"\ operator yields the bit-wise negation of its |
| 961 | plain or long integer argument. The bit-wise negation negation of |
| 962 | \verb\x\ is defined as \verb\-(x+1)\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 963 | |
| 964 | In all three cases, if the argument does not have the proper type, |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 965 | a \verb\TypeError\ exception is raised. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 966 | |
| 967 | \section{Terms} |
| 968 | |
| 969 | Terms represent the most tightly binding binary operators: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 970 | % |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 971 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 972 | term: factor | term "*" factor | term "/" factor | term "%" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 973 | \end{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 974 | % |
| 975 | The \verb\"*"\ (multiplication) operator yields the product of its |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 976 | arguments. The arguments must either both be numbers, or one argument |
| 977 | must be a plain integer and the other must be a sequence. In the |
| 978 | former case, the numbers are converted to a common type and then |
| 979 | multiplied together. In the latter case, sequence repetition is |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 980 | performed; a negative repetition factor yields an empty sequence. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 982 | The \verb\"/"\ (division) operator yields the quotient of its |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 983 | arguments. The numeric arguments are first converted to a common |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 984 | type. Plain or long integer division yields an integer of the same |
| 985 | type; the result is that of mathematical division with the `floor' |
| 986 | function applied to the result. Division by zero raises the |
| 987 | \verb\ZeroDivisionError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 988 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 989 | The \verb\"%"\ (modulo) operator yields the remainder from the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 990 | division of the first argument by the second. The numeric arguments |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 991 | are first converted to a common type. A zero right argument raises the |
| 992 | \verb\ZeroDivisionError\ exception. The arguments may be floating point |
| 993 | numbers, e.g., \verb\3.14 % 0.7\ equals \verb\0.34\. The modulo operator |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 994 | always yields a result with the same sign as its second operand (or |
| 995 | zero); the absolute value of the result is strictly smaller than the |
| 996 | second operand. |
| 997 | |
| 998 | The integer division and modulo operators are connected by the |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 999 | following identity: \verb\x == (x/y)*y + (x%y)\. |
| 1000 | Integer division and modulo are also connected with the built-in |
| 1001 | function \verb\divmod()\: \verb\divmod(x, y) == (x/y, x%y)\. |
| 1002 | These identities don't hold for floating point numbers; there a |
| 1003 | similar identity holds where \verb\x/y\ is replaced by |
| 1004 | \verb\floor(x/y)\). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1005 | |
| 1006 | \section{Arithmetic expressions} |
| 1007 | |
| 1008 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1009 | arith_expr: term | arith_expr "+" term | arith_expr "-" term |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1010 | \end{verbatim} |
| 1011 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1012 | The \verb|"+"| operator yields the sum of its arguments. The |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1013 | arguments must either both be numbers, or both sequences of the same |
| 1014 | type. In the former case, the numbers are converted to a common type |
| 1015 | and then added together. In the latter case, the sequences are |
| 1016 | concatenated. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1017 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1018 | The \verb|"-"| operator yields the difference of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1019 | The numeric arguments are first converted to a common type. |
| 1020 | |
| 1021 | \section{Shift expressions} |
| 1022 | |
| 1023 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1024 | shift_expr: arith_expr | shift_expr ( "<<" | ">>" ) arith_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1025 | \end{verbatim} |
| 1026 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1027 | These operators accept plain or long integers as arguments. The |
| 1028 | arguments are converted to a common type. They shift the first |
| 1029 | argument to the left or right by the number of bits given by the |
| 1030 | second argument. |
| 1031 | |
| 1032 | A right shift by $n$ bits is defined as division by $2^n$. A left |
| 1033 | shift by $n$ bits is defined as multiplication with $2^n$ without |
| 1034 | overflow check; for plain integers this drops bits if the result is |
| 1035 | not less than $2^{31} - 1$ in absolute value. |
| 1036 | |
| 1037 | Negative shift counts raise a \verb\ValueError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1038 | |
| 1039 | \section{Bitwise AND expressions} |
| 1040 | |
| 1041 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1042 | and_expr: shift_expr | and_expr "&" shift_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1043 | \end{verbatim} |
| 1044 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1045 | This operator yields the bitwise AND of its arguments, which must be |
| 1046 | plain or long integers. The arguments are converted to a common type. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1047 | |
| 1048 | \section{Bitwise XOR expressions} |
| 1049 | |
| 1050 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1051 | xor_expr: and_expr | xor_expr "^" and_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1052 | \end{verbatim} |
| 1053 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1054 | This operator yields the bitwise exclusive OR of its arguments, which |
| 1055 | must be plain or long integers. The arguments are converted to a |
| 1056 | common type. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1057 | |
| 1058 | \section{Bitwise OR expressions} |
| 1059 | |
| 1060 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1061 | or_expr: xor_expr | or_expr "|" xor_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1062 | \end{verbatim} |
| 1063 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1064 | This operator yields the bitwise OR of its arguments, which must be |
| 1065 | plain or long integers. The arguments are converted to a common type. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1066 | |
| 1067 | \section{Comparisons} |
| 1068 | |
| 1069 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1070 | comparison: or_expr (comp_operator or_expr)* |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1071 | comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1072 | \end{verbatim} |
| 1073 | |
| 1074 | Comparisons yield integer value: 1 for true, 0 for false. |
| 1075 | |
| 1076 | Comparisons can be chained arbitrarily, |
| 1077 | e.g., $x < y <= z$ is equivalent to |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1078 | $x < y$ \verb\and\ $y <= z$, except that $y$ is evaluated only once |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1079 | (but in both cases $z$ is not evaluated at all when $x < y$ is |
| 1080 | found to be false). |
| 1081 | |
| 1082 | Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1083 | $e_0 op_1 e_1$ \verb\and\ $e_1 op_2 e_2$ \verb\and\ ... \verb\and\ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1084 | $e_{n-1} op_n e_n$, except that each expression is evaluated at most once. |
| 1085 | |
| 1086 | Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison |
| 1087 | between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. |
| 1088 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1089 | The forms \verb\<>\ and \verb\!=\ are equivalent; for consistency with |
| 1090 | C, \verb\!=\ is preferred; where \verb\!=\ is mentioned below |
| 1091 | \verb\<>\ is also implied. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1092 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1093 | The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1094 | the values of two objects. The objects needn't have the same type. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1095 | If both are numbers, they are coverted to a common type. Otherwise, |
| 1096 | objects of different types {\em always} compare unequal, and are |
| 1097 | ordered consistently but arbitrarily. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1098 | |
| 1099 | (This unusual |
| 1100 | definition of comparison is done to simplify the definition of |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 1101 | 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] | 1102 | |
| 1103 | Comparison of objects of the same type depends on the type: |
| 1104 | |
| 1105 | \begin{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1106 | |
| 1107 | \item |
| 1108 | Numbers are compared arithmetically. |
| 1109 | |
| 1110 | \item |
| 1111 | Strings are compared lexicographically using the numeric equivalents |
| 1112 | (the result of the built-in function \verb\ord\) of their characters. |
| 1113 | |
| 1114 | \item |
| 1115 | Tuples and lists are compared lexicographically using comparison of |
| 1116 | corresponding items. |
| 1117 | |
| 1118 | \item |
| 1119 | Mappings (dictionaries) are compared through lexicographic |
| 1120 | comparison of their sorted (key, value) lists.% |
| 1121 | \footnote{This is expensive since it requires sorting the keys first, |
| 1122 | but about the only sensible definition. It was tried to compare |
| 1123 | dictionaries using the following rules, but this gave surprises in |
| 1124 | cases like \verb|if d == {}: ...|.} |
| 1125 | |
| 1126 | \item |
| 1127 | Most other types compare unequal unless they are the same object; |
| 1128 | the choice whether one object is considered smaller or larger than |
| 1129 | another one is made arbitrarily but consistently within one |
| 1130 | execution of a program. |
| 1131 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1132 | \end{itemize} |
| 1133 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1134 | The operators \verb\in\ and \verb\not in\ test for sequence |
| 1135 | membership: if $y$ is a sequence, $x ~\verb\in\~ y$ is true if and |
| 1136 | only if there exists an index $i$ such that $x = y[i]$. |
| 1137 | $x ~\verb\not in\~ y$ yields the inverse truth value. The exception |
| 1138 | \verb\TypeError\ is raised when $y$ is not a sequence, or when $y$ is |
| 1139 | a string and $x$ is not a string of length one.% |
| 1140 | \footnote{The latter restriction is sometimes a nuisance.} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1141 | |
| 1142 | The operators \verb\is\ and \verb\is not\ compare object identity: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1143 | $x ~\verb\is\~ y$ is true if and only if $x$ and $y$ are the same |
| 1144 | object. $x ~\verb\is not\~ y$ yields the inverse truth value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1145 | |
| 1146 | \section{Boolean operators} |
| 1147 | |
| 1148 | \begin{verbatim} |
| 1149 | condition: or_test |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1150 | or_test: and_test | or_test "or" and_test |
| 1151 | and_test: not_test | and_test "and" not_test |
| 1152 | not_test: comparison | "not" not_test |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1153 | \end{verbatim} |
| 1154 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1155 | In the context of Boolean operators, and also when conditions are used |
| 1156 | by control flow statements, the following values are interpreted as |
| 1157 | false: \verb\None\, numeric zero of all types, empty sequences |
| 1158 | (strings, tuples and lists), and empty mappings (dictionaries). All |
| 1159 | other values are interpreted as true. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1160 | |
| 1161 | The operator \verb\not\ yields 1 if its argument is false, 0 otherwise. |
| 1162 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1163 | The condition $x ~\verb\and\~ y$ first evaluates $x$; if $x$ is false, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1164 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 1165 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1166 | The condition $x ~\verb\or\~ y$ first evaluates $x$; if $x$ is true, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1167 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 1168 | |
| 1169 | (Note that \verb\and\ and \verb\or\ do not restrict the value and type |
| 1170 | they return to 0 and 1, but rather return the last evaluated argument. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1171 | This is sometimes useful, e.g., if \verb\s\ is a string, which should be |
| 1172 | replaced by a default value if it is empty, \verb\s or 'foo'\ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1173 | returns the desired value. Because \verb\not\ has to invent a value |
| 1174 | anyway, it does not bother to return a value of the same type as its |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1175 | argument, so \verb\not 'foo'\ yields \verb\0\, not \verb\''\.) |
| 1176 | |
| 1177 | \section{Expression lists and condition lists} |
| 1178 | |
| 1179 | \begin{verbatim} |
| 1180 | expr_list: or_expr ("," or_expr)* [","] |
| 1181 | cond_list: condition ("," condition)* [","] |
| 1182 | \end{verbatim} |
| 1183 | |
| 1184 | The only difference between expression lists and condition lists is |
| 1185 | the lowest priority of operators that can be used in them without |
| 1186 | being enclosed in parentheses; condition lists allow all operators, |
| 1187 | while expression lists don't allow comparisons and Boolean operators |
| 1188 | (they do allow bitwise and shift operators though). |
| 1189 | |
| 1190 | Expression lists are used in expression statements and assignments; |
| 1191 | condition lists are used everywhere else. |
| 1192 | |
| 1193 | An expression (condition) list containing at least one comma yields a |
| 1194 | tuple. The length of the tuple is the number of expressions |
| 1195 | (conditions) in the list. The expressions (conditions) are evaluated |
| 1196 | from left to right. |
| 1197 | |
| 1198 | The trailing comma is required only to create a single tuple (a.k.a. a |
| 1199 | {\em singleton}); it is optional in all other cases. A single |
| 1200 | expression (condition) without a trailing comma doesn't create a |
| 1201 | tuple, but rather yields the value of that expression (condition). |
| 1202 | |
| 1203 | 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] | 1204 | |
| 1205 | \chapter{Simple statements} |
| 1206 | |
| 1207 | Simple statements are comprised within a single logical line. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1208 | Several simple statements may occur on a single line separated |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1209 | by semicolons. The syntax for simple statements is: |
| 1210 | |
| 1211 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1212 | stmt_list: simple_stmt (";" simple_stmt)* [";"] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1213 | simple_stmt: expression_stmt |
| 1214 | | assignment |
| 1215 | | pass_stmt |
| 1216 | | del_stmt |
| 1217 | | print_stmt |
| 1218 | | return_stmt |
| 1219 | | raise_stmt |
| 1220 | | break_stmt |
| 1221 | | continue_stmt |
| 1222 | | import_stmt |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1223 | | global_stmt |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1224 | \end{verbatim} |
| 1225 | |
| 1226 | \section{Expression statements} |
| 1227 | |
| 1228 | \begin{verbatim} |
| 1229 | expression_stmt: expression_list |
| 1230 | \end{verbatim} |
| 1231 | |
| 1232 | An expression statement evaluates the expression list (which may |
| 1233 | be a single expression). |
| 1234 | If the value is not \verb\None\, it is converted to a string |
| 1235 | using the rules for string conversions, and the resulting string |
| 1236 | is written to standard output on a line by itself. |
| 1237 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1238 | (The exception for \verb\None\ is made so that procedure calls, which |
| 1239 | are syntactically equivalent to expressions, do not cause any output. |
| 1240 | A tuple with only \verb\None\ items is written normally.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1241 | |
| 1242 | \section{Assignments} |
| 1243 | |
| 1244 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1245 | assignment: (target_list "=")+ expression_list |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1246 | target_list: target ("," target)* [","] |
| 1247 | target: identifier | "(" target_list ")" | "[" target_list "]" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1248 | | attributeref | subscription | slicing |
| 1249 | \end{verbatim} |
| 1250 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1251 | (See the section on primaries for the syntax definition of the last |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1252 | three symbols.) |
| 1253 | |
| 1254 | An assignment evaluates the expression list (remember that this can |
| 1255 | be a single expression or a comma-separated list, |
| 1256 | the latter yielding a tuple) |
| 1257 | and assigns the single resulting object to each of the target lists, |
| 1258 | from left to right. |
| 1259 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1260 | Assignment is defined recursively depending on the form of the target. |
| 1261 | When a target is part of a mutable object (an attribute reference, |
| 1262 | subscription or slicing), the mutable object must ultimately perform |
| 1263 | the assignment and decide about its validity, and may raise an |
| 1264 | exception if the assignment is unacceptable. The rules observed by |
| 1265 | various types and the exceptions raised are given with the definition |
| 1266 | of the object types (some of which are defined in the library |
| 1267 | reference). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1268 | |
| 1269 | Assignment of an object to a target list is recursively |
| 1270 | defined as follows. |
| 1271 | |
| 1272 | \begin{itemize} |
| 1273 | \item |
| 1274 | If the target list contains no commas (except in nested constructs): |
| 1275 | the object is assigned to the single target contained in the list. |
| 1276 | |
| 1277 | \item |
| 1278 | If the target list contains commas (that are not in nested constructs): |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1279 | the object must be a tuple with the same number of items |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1280 | as the list contains targets, and the items are assigned, from left |
| 1281 | to right, to the corresponding targets. |
| 1282 | |
| 1283 | \end{itemize} |
| 1284 | |
| 1285 | Assignment of an object to a (non-list) |
| 1286 | target is recursively defined as follows. |
| 1287 | |
| 1288 | \begin{itemize} |
| 1289 | |
| 1290 | \item |
| 1291 | If the target is an identifier (name): |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1292 | \begin{itemize} |
| 1293 | \item |
| 1294 | If the name does not occur in a \verb\global\ statement in the current |
| 1295 | code block: the object is bound to that name in the current local |
| 1296 | name space. |
| 1297 | \item |
| 1298 | Otherwise: the object is bound to that name in the current global name |
| 1299 | space. |
| 1300 | \end{itemize} |
| 1301 | A previous binding of the same name in the same name space is undone. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1302 | |
| 1303 | \item |
| 1304 | If the target is a target list enclosed in parentheses: |
| 1305 | the object is assigned to that target list. |
| 1306 | |
| 1307 | \item |
| 1308 | If the target is a target list enclosed in square brackets: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1309 | the object must be a list with the same number of items |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1310 | as the target list contains targets, |
| 1311 | and the list's items are assigned, from left to right, |
| 1312 | to the corresponding targets. |
| 1313 | |
| 1314 | \item |
| 1315 | If the target is an attribute reference: |
| 1316 | The primary expression in the reference is evaluated. |
| 1317 | It should yield an object with assignable attributes; |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1318 | if this is not the case, \verb\TypeError\ is raised. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1319 | That object is then asked to assign the assigned object |
| 1320 | to the given attribute; if it cannot perform the assignment, |
| 1321 | it raises an exception. |
| 1322 | |
| 1323 | \item |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1324 | If the target is a subscription: The primary expression in the |
| 1325 | reference is evaluated. It should yield either a mutable sequence |
| 1326 | (list) object or a mapping (dictionary) object. Next, the subscript |
| 1327 | expression is evaluated. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1329 | If the primary is a sequence object, the subscript must yield a plain |
| 1330 | integer. If it is negative, the sequence's length is added to it. |
| 1331 | The resulting value must be a nonnegative integer less than the |
| 1332 | sequence's length, and the sequence is asked to assign the assigned |
| 1333 | object to its item with that index. If the index is out of range, |
| 1334 | \verb\IndexError\ is raised (assignment to a subscripted sequence |
| 1335 | cannot add new items to a list). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1336 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1337 | If the primary is a mapping object, the subscript must have a type |
| 1338 | compatible with the mapping's key type, and the mapping is then asked |
| 1339 | to to create a key/datum pair which maps the subscript to the assigned |
| 1340 | object. This can either replace an existing key/value pair with the |
| 1341 | same key value, or insert a new key/value pair (if no key with the |
| 1342 | same value existed). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1343 | |
| 1344 | \item |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1345 | If the target is a slicing: The primary expression in the reference is |
| 1346 | evaluated. It should yield a mutable sequence (list) object. The |
| 1347 | assigned object should be a sequence object of the same type. Next, |
| 1348 | the lower and upper bound expressions are evaluated, insofar they are |
| 1349 | present; defaults are zero and the sequence's length. The bounds |
| 1350 | should evaluate to (small) integers. If either bound is negative, the |
| 1351 | sequence's length is added to it. The resulting bounds are clipped to |
| 1352 | lie between zero and the sequence's length, inclusive. Finally, the |
| 1353 | sequence object is asked to replace the items indicated by the slice |
| 1354 | with the items of the assigned sequence. This may change the |
| 1355 | sequence's length, if it allows it. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1356 | |
| 1357 | \end{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1358 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1359 | (In the original implementation, the syntax for targets is taken |
| 1360 | to be the same as for expressions, and invalid syntax is rejected |
| 1361 | during the code generation phase, causing less detailed error |
| 1362 | messages.) |
| 1363 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1364 | \section{The \verb\pass\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1365 | |
| 1366 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1367 | pass_stmt: "pass" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1368 | \end{verbatim} |
| 1369 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1370 | \verb\pass\ is a null operation -- when it is executed, nothing |
| 1371 | happens. It is useful as a placeholder when a statement is |
| 1372 | required syntactically, but no code needs to be executed, for example: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1373 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1374 | \begin{verbatim} |
| 1375 | def f(arg): pass # a no-op function |
| 1376 | |
| 1377 | class C: pass # an empty class |
| 1378 | \end{verbatim} |
| 1379 | |
| 1380 | \section{The \verb\del\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1381 | |
| 1382 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1383 | del_stmt: "del" target_list |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1384 | \end{verbatim} |
| 1385 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1386 | Deletion is recursively defined very similar to the way assignment is |
| 1387 | defined. Rather that spelling it out in full details, here are some |
| 1388 | hints. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1389 | |
| 1390 | Deletion of a target list recursively deletes each target, |
| 1391 | from left to right. |
| 1392 | |
| 1393 | Deletion of a name removes the binding of that name (which must exist) |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1394 | from the local or global name space, depending on whether the name |
| 1395 | occurs in a \verb\global\ statement in the same code block. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1396 | |
| 1397 | Deletion of attribute references, subscriptions and slicings |
| 1398 | is passed to the primary object involved; deletion of a slicing |
| 1399 | is in general equivalent to assignment of an empty slice of the |
| 1400 | right type (but even this is determined by the sliced object). |
| 1401 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1402 | \section{The \verb\print\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1403 | |
| 1404 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1405 | print_stmt: "print" [ condition ("," condition)* [","] ] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1406 | \end{verbatim} |
| 1407 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1408 | \verb\print\ evaluates each condition in turn and writes the resulting |
| 1409 | object to standard output (see below). If an object is not a string, |
| 1410 | it is first converted to a string using the rules for string |
| 1411 | conversions. The (resulting or original) string is then written. A |
| 1412 | space is written before each object is (converted and) written, unless |
| 1413 | the output system believes it is positioned at the beginning of a |
| 1414 | line. This is the case: (1) when no characters have yet been written |
| 1415 | to standard output; or (2) when the last character written to standard |
| 1416 | output is \verb/\n/; or (3) when the last write operation on standard |
| 1417 | output was not a \verb\print\ statement. (In some cases it may be |
| 1418 | functional to write an empty string to standard output for this |
| 1419 | reason.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1420 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1421 | A \verb/"\n"/ character is written at the end, unless the \verb\print\ |
| 1422 | statement ends with a comma. This is the only action if the statement |
| 1423 | contains just the keyword \verb\print\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1424 | |
| 1425 | Standard output is defined as the file object named \verb\stdout\ |
| 1426 | in the built-in module \verb\sys\. If no such object exists, |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1427 | or if it is not a writable file, a \verb\RuntimeError\ exception is raised. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1428 | (The original implementation attempts to write to the system's original |
| 1429 | standard output instead, but this is not safe, and should be fixed.) |
| 1430 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1431 | \section{The \verb\return\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1432 | |
| 1433 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1434 | return_stmt: "return" [condition_list] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1435 | \end{verbatim} |
| 1436 | |
| 1437 | \verb\return\ may only occur syntactically nested in a function |
| 1438 | definition, not within a nested class definition. |
| 1439 | |
| 1440 | If a condition list is present, it is evaluated, else \verb\None\ |
| 1441 | is substituted. |
| 1442 | |
| 1443 | \verb\return\ leaves the current function call with the condition |
| 1444 | list (or \verb\None\) as return value. |
| 1445 | |
| 1446 | When \verb\return\ passes control out of a \verb\try\ statement |
| 1447 | with a \verb\finally\ clause, that finally clause is executed |
| 1448 | before really leaving the function. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1449 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1450 | \section{The \verb\raise\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1451 | |
| 1452 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1453 | raise_stmt: "raise" condition ["," condition] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1454 | \end{verbatim} |
| 1455 | |
| 1456 | \verb\raise\ evaluates its first condition, which must yield |
| 1457 | a string object. If there is a second condition, this is evaluated, |
| 1458 | else \verb\None\ is substituted. |
| 1459 | |
| 1460 | It then raises the exception identified by the first object, |
| 1461 | with the second one (or \verb\None\) as its parameter. |
| 1462 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1463 | \section{The \verb\break\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1464 | |
| 1465 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1466 | break_stmt: "break" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1467 | \end{verbatim} |
| 1468 | |
| 1469 | \verb\break\ may only occur syntactically nested in a \verb\for\ |
| 1470 | or \verb\while\ loop, not nested in a function or class definition. |
| 1471 | |
| 1472 | It terminates the neares enclosing loop, skipping the optional |
| 1473 | \verb\else\ clause if the loop has one. |
| 1474 | |
| 1475 | If a \verb\for\ loop is terminated by \verb\break\, the loop control |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1476 | target keeps its current value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1477 | |
| 1478 | When \verb\break\ passes control out of a \verb\try\ statement |
| 1479 | with a \verb\finally\ clause, that finally clause is executed |
| 1480 | before really leaving the loop. |
| 1481 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1482 | \section{The \verb\continue\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1483 | |
| 1484 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1485 | continue_stmt: "continue" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1486 | \end{verbatim} |
| 1487 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1488 | \verb\continue\ may only occur syntactically nested in a \verb\for\ or |
| 1489 | \verb\while\ loop, not nested in a function or class definition, and |
| 1490 | not nested in the \verb\try\ clause of a \verb\try\ statement with a |
| 1491 | \verb\finally\ clause (it may occur nested in a \verb\except\ or |
| 1492 | \verb\finally\ clause of a \verb\try\ statement though). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1493 | |
| 1494 | It continues with the next cycle of the nearest enclosing loop. |
| 1495 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1496 | \section{The \verb\import\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1497 | |
| 1498 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1499 | import_stmt: "import" identifier ("," identifier)* |
| 1500 | | "from" identifier "import" identifier ("," identifier)* |
| 1501 | | "from" identifier "import" "*" |
| 1502 | \end{verbatim} |
| 1503 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1504 | Import statements are executed in two steps: (1) find a module, and |
| 1505 | initialize it if necessary; (2) define a name or names in the local |
| 1506 | name space. The first form (without \verb\from\) repeats these steps |
| 1507 | for each identifier in the list. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1508 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1509 | The system maintains a table of modules that have been initialized, |
| 1510 | indexed by module name. (The current implementation makes this table |
| 1511 | accessible as \verb\sys.modules\.) When a module name is found in |
| 1512 | this table, step (1) is finished. If not, a search for a module |
| 1513 | definition is started. This first looks for a built-in module |
| 1514 | definition, and if no built-in module if the given name is found, it |
| 1515 | searches a user-specified list of directories for a file whose name is |
| 1516 | the module name with extension \verb\".py"\. (The current |
| 1517 | implementation uses the list of strings \verb\sys.path\ as the search |
| 1518 | path; it is initialized from the shell environment variable |
| 1519 | \verb\$PYTHONPATH\, with an installation-dependent default.) |
| 1520 | |
| 1521 | If a built-in module is found, its built-in initialization code is |
| 1522 | executed and step (1) is finished. If no matching file is found, |
| 1523 | \ImportError\ is raised (and step (2) is never started). If a file is |
| 1524 | found, it is parsed. If a syntax error occurs, HIRO |
| 1525 | |
| 1526 | \section{The \verb\global\ statement} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1527 | |
| 1528 | \begin{verbatim} |
| 1529 | global_stmt: "global" identifier ("," identifier)* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1530 | \end{verbatim} |
| 1531 | |
| 1532 | (XXX To be done.) |
| 1533 | |
| 1534 | \chapter{Compound statements} |
| 1535 | |
| 1536 | (XXX The semantic definitions of this chapter are still to be done.) |
| 1537 | |
| 1538 | \begin{verbatim} |
| 1539 | statement: stmt_list NEWLINE | compound_stmt |
| 1540 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 1541 | suite: statement | NEWLINE INDENT statement+ DEDENT |
| 1542 | \end{verbatim} |
| 1543 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1544 | \section{The \verb\if\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1545 | |
| 1546 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1547 | if_stmt: "if" condition ":" suite |
| 1548 | ("elif" condition ":" suite)* |
| 1549 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1550 | \end{verbatim} |
| 1551 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1552 | \section{The \verb\while\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1553 | |
| 1554 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1555 | while_stmt: "while" condition ":" suite ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1556 | \end{verbatim} |
| 1557 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1558 | \section{The \verb\for\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1559 | |
| 1560 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1561 | for_stmt: "for" target_list "in" condition_list ":" suite |
| 1562 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1563 | \end{verbatim} |
| 1564 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame^] | 1565 | \section{The \verb\try\ statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1566 | |
| 1567 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1568 | try_stmt: "try" ":" suite |
| 1569 | ("except" condition ["," condition] ":" suite)* |
| 1570 | ["finally" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1571 | \end{verbatim} |
| 1572 | |
| 1573 | \section{Function definitions} |
| 1574 | |
| 1575 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1576 | funcdef: "def" identifier "(" [parameter_list] ")" ":" suite |
| 1577 | parameter_list: parameter ("," parameter)* |
| 1578 | parameter: identifier | "(" parameter_list ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1579 | \end{verbatim} |
| 1580 | |
| 1581 | \section{Class definitions} |
| 1582 | |
| 1583 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1584 | classdef: "class" identifier [inheritance] ":" suite |
| 1585 | inheritance: "(" expression ("," expression)* ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1586 | \end{verbatim} |
| 1587 | |
| 1588 | XXX Syntax for scripts, modules |
| 1589 | XXX Syntax for interactive input, eval, exec, input |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1590 | XXX New definition of expressions (as conditions) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1591 | |
| 1592 | \end{document} |