Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1 | \documentstyle[11pt,myformat]{report} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 3 | \title{\bf Python Reference Manual} |
| 4 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 5 | \author{ |
| 6 | Guido van Rossum \\ |
| 7 | Dept. CST, CWI, Kruislaan 413 \\ |
| 8 | 1098 SJ Amsterdam, The Netherlands \\ |
| 9 | E-mail: {\tt guido@cwi.nl} |
| 10 | } |
| 11 | |
| 12 | \begin{document} |
| 13 | |
| 14 | \pagenumbering{roman} |
| 15 | |
| 16 | \maketitle |
| 17 | |
| 18 | \begin{abstract} |
| 19 | |
| 20 | \noindent |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 21 | Python is a simple, yet powerful, interpreted programming language |
| 22 | that bridges the gap between C and shell programming, and is thus |
| 23 | ideally suited for ``throw-away programming'' and rapid prototyping. |
| 24 | Its syntax is put together from constructs borrowed from a variety of |
| 25 | other languages; most prominent are influences from ABC, C, Modula-3 |
| 26 | and Icon. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 27 | |
| 28 | The Python interpreter is easily extended with new functions and data |
| 29 | types implemented in C. Python is also suitable as an extension |
| 30 | language for highly customizable C applications such as editors or |
| 31 | window managers. |
| 32 | |
| 33 | Python is available for various operating systems, amongst which |
| 34 | several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S., |
| 35 | and MS-DOS. |
| 36 | |
| 37 | This reference manual describes the syntax and ``core semantics'' of |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 38 | the language. It is terse, but attempts to be exact and complete. |
| 39 | The semantics of non-essential built-in object types and of the |
| 40 | built-in functions and modules are described in the {\em Python |
| 41 | Library Reference}. For an informal introduction to the language, see |
| 42 | the {\em Python Tutorial}. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 43 | |
| 44 | \end{abstract} |
| 45 | |
| 46 | \pagebreak |
| 47 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 48 | { |
| 49 | \parskip = 0mm |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 50 | \tableofcontents |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 51 | } |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 52 | |
| 53 | \pagebreak |
| 54 | |
| 55 | \pagenumbering{arabic} |
| 56 | |
| 57 | \chapter{Introduction} |
| 58 | |
| 59 | This reference manual describes the Python programming language. |
| 60 | It is not intended as a tutorial. |
| 61 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 62 | While I am trying to be as precise as possible, I chose to use English |
| 63 | rather than formal specifications for everything except syntax and |
| 64 | lexical analysis. This should make the document better understandable |
| 65 | to the average reader, but will leave room for ambiguities. |
| 66 | 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] | 67 | Python from this document alone, you might have to guess things and in |
| 68 | fact you would be implementing quite a different language. |
| 69 | On the other hand, if you are using |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 70 | 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] | 71 | 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] | 72 | |
| 73 | It is dangerous to add too many implementation details to a language |
| 74 | reference document -- the implementation may change, and other |
| 75 | implementations of the same language may work differently. On the |
| 76 | other hand, there is currently only one Python implementation, and |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 77 | its particular quirks are sometimes worth being mentioned, especially |
| 78 | where the implementation imposes additional limitations. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 79 | |
| 80 | Every Python implementation comes with a number of built-in and |
| 81 | standard modules. These are not documented here, but in the separate |
| 82 | {\em Python Library Reference} document. A few built-in modules are |
| 83 | mentioned when they interact in a significant way with the language |
| 84 | definition. |
| 85 | |
| 86 | \section{Notation} |
| 87 | |
| 88 | The descriptions of lexical analysis and syntax use a modified BNF |
| 89 | grammar notation. This uses the following style of definition: |
| 90 | |
| 91 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 92 | name: lc_letter (lc_letter | "_")* |
| 93 | lc_letter: "a"..."z" |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 94 | \end{verbatim} |
| 95 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 96 | The first line says that a \verb\name\ is an \verb\lc_letter\ followed by |
| 97 | a sequence of zero or more \verb\lc_letter\s and underscores. An |
| 98 | \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] | 99 | (This rule is actually adhered to for the names defined in syntax and |
| 100 | grammar rules in this document.) |
| 101 | |
| 102 | Each rule begins with a name (which is the name defined by the rule) |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 103 | and a colon. A vertical bar |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 104 | (\verb\|\) is used to separate alternatives; it is the least binding |
| 105 | operator in this notation. A star (\verb\*\) means zero or more |
| 106 | repetitions of the preceding item; likewise, a plus (\verb\+\) means |
| 107 | one or more repetitions, and a question mark (\verb\?\) zero or one |
| 108 | (in other words, the preceding item is optional). These three |
| 109 | operators bind as tightly as possible; parentheses are used for |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 110 | grouping. Literal strings are enclosed in double quotes. White space |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 111 | is only meaningful to separate tokens. Rules are normally contained |
| 112 | on a single line; rules with many alternatives may be formatted |
| 113 | alternatively with each line after the first beginning with a |
| 114 | vertical bar. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 115 | |
| 116 | In lexical definitions (as the example above), two more conventions |
| 117 | are used: Two literal characters separated by three dots mean a choice |
| 118 | of any single character in the given (inclusive) range of ASCII |
| 119 | characters. A phrase between angular brackets (\verb\<...>\) gives an |
| 120 | informal description of the symbol defined; e.g., this could be used |
| 121 | to describe the notion of `control character' if needed. |
| 122 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 123 | 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] | 124 | difference between the meaning of lexical and syntactic definitions: |
| 125 | a lexical definition operates on the individual characters of the |
| 126 | input source, while a syntax definition operates on the stream of |
| 127 | tokens generated by the lexical analysis. |
| 128 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 129 | \chapter{Lexical analysis} |
| 130 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 131 | A Python program is read by a {\em parser}. Input to the parser is a |
| 132 | stream of {\em tokens}, generated by the {\em lexical analyzer}. This |
| 133 | chapter describes how the lexical analyzer breaks a file into tokens. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 134 | |
| 135 | \section{Line structure} |
| 136 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 137 | A Python program is divided in a number of logical lines. The end of |
| 138 | a logical line is represented by the token NEWLINE. Statements cannot |
| 139 | cross logical line boundaries except where NEWLINE is allowed by the |
| 140 | syntax (e.g., between statements in compound statements). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 141 | |
| 142 | \subsection{Comments} |
| 143 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 144 | 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] | 145 | a string literal, and ends at the end of the physical line. A comment |
| 146 | always signifies the end of the logical line. Comments are ignored by |
| 147 | the syntax. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 148 | |
| 149 | \subsection{Line joining} |
| 150 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 151 | 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] | 152 | backslash characters (\verb/\/), as follows: when a physical line ends |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 153 | in a backslash that is not part of a string literal or comment, it is |
| 154 | joined with the following forming a single logical line, deleting the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 155 | backslash and the following end-of-line character. For example: |
| 156 | % |
| 157 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 158 | moth_names = ['Januari', 'Februari', 'Maart', \ |
| 159 | 'April', 'Mei', 'Juni', \ |
| 160 | 'Juli', 'Augustus', 'September', \ |
| 161 | 'Oktober', 'November', 'December'] |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 162 | \end{verbatim} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 163 | |
| 164 | \subsection{Blank lines} |
| 165 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 166 | A logical line that contains only spaces, tabs, and possibly a |
| 167 | comment, is ignored (i.e., no NEWLINE token is generated), except that |
| 168 | during interactive input of statements, an entirely blank logical line |
| 169 | terminates a multi-line statement. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 170 | |
| 171 | \subsection{Indentation} |
| 172 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 173 | Leading whitespace (spaces and tabs) at the beginning of a logical |
| 174 | line is used to compute the indentation level of the line, which in |
| 175 | turn is used to determine the grouping of statements. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 177 | First, tabs are replaced (from left to right) by one to eight spaces |
| 178 | such that the total number of characters up to there is a multiple of |
| 179 | eight (this is intended to be the same rule as used by UNIX). The |
| 180 | total number of spaces preceding the first non-blank character then |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 181 | determines the line's indentation. Indentation cannot be split over |
| 182 | multiple physical lines using backslashes. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 183 | |
| 184 | The indentation levels of consecutive lines are used to generate |
| 185 | INDENT and DEDENT tokens, using a stack, as follows. |
| 186 | |
| 187 | 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] | 188 | the stack; this will never be popped off again. The numbers pushed on |
| 189 | the stack will always be strictly increasing from bottom to top. At |
| 190 | the beginning of each logical line, the line's indentation level is |
| 191 | compared to the top of the stack. If it is equal, nothing happens. |
| 192 | If it larger, it is pushed on the stack, and one INDENT token is |
| 193 | generated. If it is smaller, it {\em must} be one of the numbers |
| 194 | occurring on the stack; all numbers on the stack that are larger are |
| 195 | popped off, and for each number popped off a DEDENT token is |
| 196 | generated. At the end of the file, a DEDENT token is generated for |
| 197 | each number remaining on the stack that is larger than zero. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 199 | Here is an example of a correctly (though confusingly) indented piece |
| 200 | of Python code: |
| 201 | |
| 202 | \begin{verbatim} |
| 203 | def perm(l): |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 204 | # Compute the list of all permutations of l |
| 205 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 206 | if len(l) <= 1: |
| 207 | return [l] |
| 208 | r = [] |
| 209 | for i in range(len(l)): |
| 210 | s = l[:i] + l[i+1:] |
| 211 | p = perm(s) |
| 212 | for x in p: |
| 213 | r.append(l[i:i+1] + x) |
| 214 | return r |
| 215 | \end{verbatim} |
| 216 | |
| 217 | The following example shows various indentation errors: |
| 218 | |
| 219 | \begin{verbatim} |
| 220 | def perm(l): # error: first line indented |
| 221 | for i in range(len(l)): # error: not indented |
| 222 | s = l[:i] + l[i+1:] |
| 223 | p = perm(l[:i] + l[i+1:]) # error: unexpected indent |
| 224 | for x in p: |
| 225 | r.append(l[i:i+1] + x) |
| 226 | return r # error: inconsistent indent |
| 227 | \end{verbatim} |
| 228 | |
| 229 | (Actually, the first three errors are detected by the parser; only the |
| 230 | last error is found by the lexical analyzer -- the indentation of |
| 231 | \verb\return r\ does not match a level popped off the stack.) |
| 232 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 233 | \section{Other tokens} |
| 234 | |
| 235 | Besides NEWLINE, INDENT and DEDENT, the following categories of tokens |
| 236 | exist: identifiers, keywords, literals, operators, and delimiters. |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 237 | Spaces and tabs are not tokens, but serve to delimit tokens. Where |
| 238 | ambiguity exists, a token comprises the longest possible string that |
| 239 | forms a legal token, when read from left to right. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 241 | \section{Identifiers} |
| 242 | |
| 243 | Identifiers are described by the following regular expressions: |
| 244 | |
| 245 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 246 | identifier: (letter|"_") (letter|digit|"_")* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 247 | letter: lowercase | uppercase |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 248 | lowercase: "a"..."z" |
| 249 | uppercase: "A"..."Z" |
| 250 | digit: "0"..."9" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 251 | \end{verbatim} |
| 252 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 253 | Identifiers are unlimited in length. Case is significant. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 255 | \subsection{Keywords} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 257 | The following identifiers are used as reserved words, or {\em |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 258 | keywords} of the language, and cannot be used as ordinary |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 259 | identifiers. They must be spelled exactly as written here: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 261 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 262 | and del for in print |
| 263 | break elif from is raise |
| 264 | class else global not return |
| 265 | continue except if or try |
| 266 | def finally import pass while |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 267 | \end{verbatim} |
| 268 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 269 | % # This Python program sorts and formats the above table |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 270 | % import string |
| 271 | % l = [] |
| 272 | % try: |
| 273 | % while 1: |
| 274 | % l = l + string.split(raw_input()) |
| 275 | % except EOFError: |
| 276 | % pass |
| 277 | % l.sort() |
| 278 | % for i in range((len(l)+4)/5): |
| 279 | % for j in range(i, len(l), 5): |
| 280 | % print string.ljust(l[j], 10), |
| 281 | % print |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 282 | |
| 283 | \section{Literals} |
| 284 | |
| 285 | \subsection{String literals} |
| 286 | |
| 287 | String literals are described by the following regular expressions: |
| 288 | |
| 289 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 290 | stringliteral: "'" stringitem* "'" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 291 | stringitem: stringchar | escapeseq |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 292 | stringchar: <any ASCII character except newline or "\" or "'"> |
| 293 | escapeseq: "'" <any ASCII character except newline> |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 294 | \end{verbatim} |
| 295 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 296 | String literals cannot span physical line boundaries. Escape |
| 297 | sequences in strings are actually interpreted according to rules |
| 298 | simular to those used by Standard C. The recognized escape sequences |
| 299 | are: |
| 300 | |
| 301 | \begin{center} |
| 302 | \begin{tabular}{|l|l|} |
| 303 | \hline |
| 304 | \verb/\\/ & Backslash (\verb/\/) \\ |
| 305 | \verb/\'/ & Single quote (\verb/'/) \\ |
| 306 | \verb/\a/ & ASCII Bell (BEL) \\ |
| 307 | \verb/\b/ & ASCII Backspace (BS) \\ |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 308 | %\verb/\E/ & ASCII Escape (ESC) \\ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 309 | \verb/\f/ & ASCII Formfeed (FF) \\ |
| 310 | \verb/\n/ & ASCII Linefeed (LF) \\ |
| 311 | \verb/\r/ & ASCII Carriage Return (CR) \\ |
| 312 | \verb/\t/ & ASCII Horizontal Tab (TAB) \\ |
| 313 | \verb/\v/ & ASCII Vertical Tab (VT) \\ |
| 314 | \verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\ |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 315 | \verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 316 | \hline |
| 317 | \end{tabular} |
| 318 | \end{center} |
| 319 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 320 | 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] | 321 | accepted, but an unlimited number of hex digits is taken to be part of |
| 322 | 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] | 323 | are used in all current implementations...). |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 325 | All unrecognized escape sequences are left in the string unchanged, |
| 326 | 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] | 327 | useful when debugging: if an escape sequence is mistyped, the |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 328 | resulting output is more easily recognized as broken. It also helps a |
| 329 | great deal for string literals used as regular expressions or |
| 330 | otherwise passed to other modules that do their own escape handling -- |
| 331 | but you may end up quadrupling backslashes that must appear literally.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 332 | |
| 333 | \subsection{Numeric literals} |
| 334 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 335 | There are three types of numeric literals: plain integers, long |
| 336 | integers, and floating point numbers. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 337 | |
| 338 | Integers and long integers are described by the following regular expressions: |
| 339 | |
| 340 | \begin{verbatim} |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 341 | longinteger: integer ("l"|"L") |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 342 | integer: decimalinteger | octinteger | hexinteger |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 343 | decimalinteger: nonzerodigit digit* | "0" |
| 344 | octinteger: "0" octdigit+ |
| 345 | hexinteger: "0" ("x"|"X") hexdigit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 347 | nonzerodigit: "1"..."9" |
| 348 | octdigit: "0"..."7" |
| 349 | hexdigit: digit|"a"..."f"|"A"..."F" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 350 | \end{verbatim} |
| 351 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 352 | Although both lower case `l'and upper case `L' are allowed as suffix |
| 353 | for long integers, it is strongly recommended to always use `L', since |
| 354 | the letter `l' looks too much like the digit `1'. |
| 355 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 356 | 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] | 357 | largest positive integer, assuming 32-bit arithmetic); octal and |
| 358 | hexadecimal literals may be as large as $2^{32} - 1$. There is no limit |
| 359 | for long integer literals. |
| 360 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 361 | Some examples of plain and long integer literals: |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 362 | |
| 363 | \begin{verbatim} |
| 364 | 7 2147483647 0177 0x80000000 |
| 365 | 3L 79228162514264337593543950336L 0377L 0100000000L |
| 366 | \end{verbatim} |
| 367 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 368 | Floating point numbers are described by the following regular expressions: |
| 369 | |
| 370 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 371 | floatnumber: pointfloat | exponentfloat |
| 372 | pointfloat: [intpart] fraction | intpart "." |
| 373 | exponentfloat: (intpart | pointfloat) exponent |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 374 | intpart: digit+ |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 375 | fraction: "." digit+ |
| 376 | exponent: ("e"|"E") ["+"|"-"] digit+ |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 377 | \end{verbatim} |
| 378 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 379 | The allowed range of floating point literals is |
| 380 | implementation-dependent. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 381 | |
| 382 | Some examples of floating point literals: |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 383 | |
| 384 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 385 | 3.14 10. .001 1e100 3.14e-10 |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 386 | \end{verbatim} |
| 387 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 388 | Note that numeric literals do not include a sign; a phrase like |
| 389 | \verb\-1\ is actually an expression composed of the operator |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 390 | \verb\-\ and the literal \verb\1\. |
| 391 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 392 | \section{Operators} |
| 393 | |
| 394 | The following tokens are operators: |
| 395 | |
| 396 | \begin{verbatim} |
| 397 | + - * / % |
| 398 | << >> & | ^ ~ |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 399 | < == > <= <> != >= |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 400 | \end{verbatim} |
| 401 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 402 | The comparison operators \verb\<>\ and \verb\!=\ are alternate |
| 403 | spellings of the same operator. |
| 404 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 405 | \section{Delimiters} |
| 406 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 407 | The following tokens serve as delimiters or otherwise have a special |
| 408 | meaning: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 409 | |
| 410 | \begin{verbatim} |
| 411 | ( ) [ ] { } |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 412 | ; , : . ` = |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 413 | \end{verbatim} |
| 414 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 415 | The following printing ASCII characters are not used in Python (except |
| 416 | in string literals and in comments). Their occurrence is an |
| 417 | unconditional error: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 418 | |
| 419 | \begin{verbatim} |
| 420 | ! @ $ " ? |
| 421 | \end{verbatim} |
| 422 | |
Guido van Rossum | 7b632a6 | 1992-01-16 17:49:21 +0000 | [diff] [blame] | 423 | They may be used by future versions of the language though! |
| 424 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 425 | \chapter{Execution model} |
| 426 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 427 | \section{Objects, values and types} |
| 428 | |
| 429 | I won't try to define rigorously here what an object is, but I'll give |
| 430 | some properties of objects that are important to know about. |
| 431 | |
| 432 | Every object has an identity, a type and a value. An object's {\em |
| 433 | identity} never changes once it has been created; think of it as the |
| 434 | object's (permanent) address. An object's {\em type} determines the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 435 | operations that an object supports (e.g., does it have a length?) and |
| 436 | also defines the ``meaning'' of the object's value. The type also |
| 437 | never changes. The {\em value} of some objects can change; whether |
| 438 | this is possible is a property of its type. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 439 | |
| 440 | Objects are never explicitly destroyed; however, when they become |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 441 | unreachable they may be garbage-collected. An implementation is |
| 442 | allowed to delay garbage collection or omit it altogether -- it is a |
| 443 | matter of implementation quality how garbage collection is |
| 444 | implemented, as long as no objects are collected that are still |
| 445 | reachable. (Implementation note: the current implementation uses a |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 446 | reference-counting scheme which collects most objects as soon as they |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 447 | become unreachable, but never collects garbage containing circular |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 448 | references.) |
| 449 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 450 | Note that the use of the implementation's tracing or debugging |
| 451 | facilities may keep objects alive that would normally be collectable. |
| 452 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 453 | (Some objects contain references to ``external'' resources such as |
| 454 | open files. It is understood that these resources are freed when the |
| 455 | object is garbage-collected, but since garbage collection is not |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 456 | guaranteed, such objects also provide an explicit way to release the |
| 457 | external resource (e.g., a \verb\close\ method). Programs are strongly |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 458 | recommended to use this.) |
| 459 | |
| 460 | Some objects contain references to other objects. These references |
| 461 | are part of the object's value; in most cases, when such a |
| 462 | ``container'' object is compared to another (of the same type), the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 463 | comparison applies to the {\em values} of the referenced objects (not |
| 464 | their identities). |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 466 | Types affect almost all aspects of objects. |
| 467 | Even object identity is affected in some sense: for immutable |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 468 | types, operations that compute new values may actually return a |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 469 | 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] | 470 | for mutable objects this is not allowed. E.g., after |
| 471 | |
| 472 | \begin{verbatim} |
| 473 | a = 1; b = 1; c = []; d = [] |
| 474 | \end{verbatim} |
| 475 | |
| 476 | \verb\a\ and \verb\b\ may or may not refer to the same object, but |
| 477 | \verb\c\ and \verb\d\ are guaranteed to refer to two different, unique, |
| 478 | newly created lists. |
| 479 | |
| 480 | \section{Execution frames, name spaces, and scopes} |
| 481 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 482 | XXX code blocks, scopes, name spaces, name binding, exceptions |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 484 | \chapter{The standard type hierarchy} |
| 485 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 486 | Below is a list of the types that are built into Python. Extension |
| 487 | modules written in C can define additional types. Future versions of |
| 488 | Python may add types to the type hierarchy (e.g., rational or complex |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 489 | numbers, lists of efficiently stored integers, etc.). |
| 490 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 491 | Some type descriptions contain a paragraph listing `special |
| 492 | attributes'. These are attributes that provide access to the |
| 493 | implementation and are not intended for general use. Their definition |
| 494 | may change in the future. There are also some `generic' special |
| 495 | attributes, not listed with the individual objects: \verb\__methods__\ |
| 496 | is a list of the method names of a built-in object, if it has any; |
| 497 | \verb\__members__\ is a list of the data attribute names of a built-in |
| 498 | object, if it has any. |
| 499 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 500 | \begin{description} |
| 501 | |
| 502 | \item[None] |
| 503 | This type has a single value. There is a single object with this value. |
| 504 | This object is accessed through the built-in name \verb\None\. |
| 505 | It is returned from functions that don't explicitly return an object. |
| 506 | |
| 507 | \item[Numbers] |
| 508 | These are created by numeric literals and returned as results |
| 509 | by arithmetic operators and arithmetic built-in functions. |
| 510 | Numeric objects are immutable; once created their value never changes. |
| 511 | Python numbers are of course strongly related to mathematical numbers, |
| 512 | but subject to the limitations of numerical representation in computers. |
| 513 | |
| 514 | Python distinguishes between integers and floating point numbers: |
| 515 | |
| 516 | \begin{description} |
| 517 | \item[Integers] |
| 518 | These represent elements from the mathematical set of whole numbers. |
| 519 | |
| 520 | There are two types of integers: |
| 521 | |
| 522 | \begin{description} |
| 523 | |
| 524 | \item[Plain integers] |
| 525 | These represent numbers in the range $-2^{31}$ through $2^{31}-1$. |
| 526 | (The range may be larger on machines with a larger natural word |
| 527 | size, but not smaller.) |
| 528 | When the result of an operation falls outside this range, the |
| 529 | exception \verb\OverflowError\ is raised. |
| 530 | For the purpose of shift and mask operations, integers are assumed to |
| 531 | have a binary, 2's complement notation using 32 or more bits, and |
| 532 | hiding no bits from the user (i.e., all $2^{32}$ different bit |
| 533 | patterns correspond to different values). |
| 534 | |
| 535 | \item[Long integers] |
| 536 | These represent numbers in an unlimited range, subject to avaiable |
| 537 | (virtual) memory only. For the purpose of shift and mask operations, |
| 538 | a binary representation is assumed, and negative numbers are |
| 539 | represented in a variant of 2's complement which gives the illusion of |
| 540 | an infinite string of sign bits extending to the left. |
| 541 | |
| 542 | \end{description} % Integers |
| 543 | |
| 544 | The rules for integer representation are intended to give the most |
| 545 | meaningful interpretation of shift and mask operations involving |
| 546 | negative integers and the least surprises when switching between the |
| 547 | plain and long integer domains. For any operation except left shift, |
| 548 | if it yields a result in the plain integer domain without causing |
| 549 | overflow, it will yield the same result in the long integer domain or |
| 550 | when using mixed operands. |
| 551 | |
| 552 | \item[Floating point numbers] |
| 553 | These represent machine-level double precision floating point numbers. |
| 554 | You are at the mercy of the underlying machine architecture and |
| 555 | C implementation for the accepted range and handling of overflow. |
| 556 | |
| 557 | \end{description} % Numbers |
| 558 | |
| 559 | \item[Sequences] |
| 560 | These represent finite ordered sets indexed by natural numbers. |
| 561 | The built-in function \verb\len()\ returns the number of elements |
| 562 | of a sequence. When this number is $n$, the index set contains |
| 563 | the numbers $0, 1, \ldots, n-1$. Element \verb\i\ of sequence |
| 564 | \verb\a\ is selected by \verb\a[i]\. |
| 565 | |
| 566 | Sequences also support slicing: \verb\a[i:j]\ selects all elements |
| 567 | with index $k$ such that $i < k < j$. When used as an expression, |
| 568 | a slice is a sequence of the same type -- this implies that the |
| 569 | index set is renumbered so that it starts at 0 again. |
| 570 | |
| 571 | Sequences are distinguished according to their mutability: |
| 572 | |
| 573 | \begin{description} |
| 574 | % |
| 575 | \item[Immutable sequences] |
| 576 | An object of an immutable sequence type cannot change once it is |
| 577 | created. (If the object contains references to other objects, |
| 578 | these other objects may be mutable and may be changed; however |
| 579 | the collection of objects directly referenced by an immutable object |
| 580 | cannot change.) |
| 581 | |
| 582 | The following types are immutable sequences: |
| 583 | |
| 584 | \begin{description} |
| 585 | |
| 586 | \item[Strings] |
| 587 | The elements of a string are characters. There is no separate |
| 588 | character type; a character is represented by a string of one element. |
| 589 | Characters represent (at least) 8-bit bytes. The built-in |
| 590 | functions \verb\chr()\ and \verb\ord()\ convert between characters |
| 591 | and nonnegative integers representing the byte values. |
| 592 | Bytes with the values 0-127 represent the corresponding ASCII values. |
| 593 | |
| 594 | (On systems whose native character set is not ASCII, strings may use |
| 595 | EBCDIC in their internal representation, provided the functions |
| 596 | \verb\chr()\ and \verb\ord()\ implement a mapping between ASCII and |
| 597 | EBCDIC, and string comparisons preserve the ASCII order. |
| 598 | Or perhaps someone can propose a better rule?) |
| 599 | |
| 600 | \item[Tuples] |
| 601 | The elements of a tuple are arbitrary Python objects. |
| 602 | Tuples of two or more elements are formed by comma-separated lists |
| 603 | of expressions. A tuple of one element can be formed by affixing |
| 604 | a comma to an expression (an expression by itself of course does |
| 605 | not create a tuple). An empty tuple can be formed by enclosing |
| 606 | `nothing' in parentheses. |
| 607 | |
| 608 | \end{description} % Immutable sequences |
| 609 | |
| 610 | \item[Mutable sequences] |
| 611 | Mutable sequences can be changed after they are created. |
| 612 | The subscript and slice notations can be used as the target |
| 613 | of assignment and \verb\del\ (delete) statements. |
| 614 | |
| 615 | There is currently a single mutable sequence type: |
| 616 | |
| 617 | \begin{description} |
| 618 | |
| 619 | \item[Lists] |
| 620 | The elements of a list are arbitrary Python objects. |
| 621 | Lists are formed by placing a comma-separated list of expressions |
| 622 | in square brackets. (Note that there are no special cases for lists |
| 623 | of length 0 or 1.) |
| 624 | |
| 625 | \end{description} % Mutable sequences |
| 626 | |
| 627 | \end{description} % Sequences |
| 628 | |
| 629 | \item[Mapping types] |
| 630 | These represent finite sets of objects indexed by arbitrary index sets. |
| 631 | The subscript notation \verb\a[k]\ selects the element indexed |
| 632 | by \verb\k\ from the mapping \verb\a\; this can be used in |
| 633 | expressions and as the target of assignments or \verb\del\ statements. |
| 634 | The built-in function \verb\len()\ returns the number of elements |
| 635 | in a mapping. |
| 636 | |
| 637 | There is currently a single mapping type: |
| 638 | |
| 639 | \begin{description} |
| 640 | |
| 641 | \item[Dictionaries] |
| 642 | These represent finite sets of objects indexed by strings. |
| 643 | Dictionaries are created by the \verb\{...}\ notation (see section |
| 644 | \ref{dict}). (Implementation note: the strings used for indexing must |
| 645 | not contain null bytes.) |
| 646 | |
| 647 | \end{description} % Mapping types |
| 648 | |
| 649 | \item[Callable types] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 650 | These are the types to which the function call operation (written as |
| 651 | \verb\function(argument, argument, ...)\) can be applied: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 652 | |
| 653 | \begin{description} |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 655 | \item[User-defined functions] |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 656 | A user-defined function is created by a function definition (see |
| 657 | section \ref{function}). It should be called with an argument list |
| 658 | containing the same number of items as the function's formal parameter |
| 659 | list. |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 660 | |
| 661 | Special read-only attributes: \verb\func_code\ is the code object |
| 662 | representing the compiled function body, and \verb\func_globals\ is (a |
| 663 | reference to) the dictionary that holds the function's global |
| 664 | variables -- it implements the global name space of the module in |
| 665 | which the function was defined. |
| 666 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 667 | \item[User-defined methods] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 668 | A user-defined method (a.k.a. {\tt object closure}) is a pair of a |
| 669 | class instance object and a user-defined function. It should be |
| 670 | called with an argument list containing one item less than the number |
| 671 | of items in the function's formal parameter list. When called, the |
| 672 | class instance becomes the first argument, and the call arguments are |
| 673 | shifted one to the right. |
| 674 | |
| 675 | Special read-only attributes: \verb\im_self\ is the class instance |
| 676 | object, \verb\im_func\ is the function object. |
| 677 | |
| 678 | \item[Built-in functions] |
| 679 | A built-in function object is a wrapper around a C function. Examples |
| 680 | of built-in functions are \verb\len\ and \verb\math.sin\. There |
| 681 | are no special attributes. The number and type of the arguments are |
| 682 | determined by the C function. |
| 683 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 684 | \item[Built-in methods] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 685 | This is really a different disguise of a built-in function, this time |
| 686 | containing an object passed to the C function as an implicit extra |
| 687 | argument. An example of a built-in method is \verb\list.append\ if |
| 688 | \verb\list\ is a list object. |
| 689 | |
| 690 | \item[Classes] |
| 691 | Class objects are described below. When a class object is called as a |
| 692 | parameterless function, a new class instance (also described below) is |
| 693 | created and returned. The class's initialization function is not |
| 694 | called -- this is the responsibility of the caller. It is illegal to |
| 695 | call a class object with one or more arguments. |
| 696 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 697 | \end{description} |
| 698 | |
| 699 | \item[Modules] |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 700 | Modules are imported by the \verb\import\ statement (see section |
| 701 | \ref{import}). A module object is a container for a module's name |
| 702 | space, which is a dictionary (the same dictionary as referenced by the |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 703 | \ver\func_globals\ attribute of functions defined in the module). |
| 704 | Module attribute references are translated to lookups in this |
| 705 | dictionary. A module object does not contain the code object used to |
| 706 | initialize the module (since it isn't needed once the initialization |
| 707 | is done). |
| 708 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 709 | Attribute assignment update the module's name space dictionary. |
| 710 | |
| 711 | Special read-only attributes: \verb\__dict__\ yields the module's name |
| 712 | space as a dictionary object; \verb\__name__\ yields the module's name. |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 713 | |
| 714 | \item[Classes] |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 715 | Class objects are created by class definitions (see section |
| 716 | \ref{class}). A class is a container for a dictionary containing the |
| 717 | class's name space. Class attribute references are translated to |
| 718 | lookups in this dictionary. When an attribute name is not found |
| 719 | there, the attribute search continues in the base classes. The search |
| 720 | is depth-first, left-to-right in the order of their occurrence in the |
| 721 | base class list. |
| 722 | |
| 723 | Attribute assignments update the class's dictionary, never the |
| 724 | dictionary of a base class. |
| 725 | |
| 726 | A class can be called as a parameterless function to yield a class |
| 727 | instance (see above). |
| 728 | |
| 729 | Special read-only attributes: \verb\__dict__\ yields te dictionary |
| 730 | containing the class's name space; \verb\__bases__\ yields a tuple |
| 731 | (possibly empty or a singleton) containing the base classes, in the |
| 732 | order of their occurrence in the base class list. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 733 | |
| 734 | \item[Class instances] |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 735 | A class instance is created by calling a class object as a |
| 736 | parameterless function. A class instance has a dictionary in which |
| 737 | attribute references are searched. When an attribute is not found |
| 738 | there, and the instance's class has an attribute by that name, and |
| 739 | that class attribute is a user-defined function (and in no other |
| 740 | cases), the instance attribute reference yields a user-defined method |
| 741 | object (see above) constructed from the instance and the function. |
| 742 | |
| 743 | Attribute assignments update the instance's dictionary. |
| 744 | |
| 745 | Special read-only attributes: \verb\__dict__\ yields the attribute |
| 746 | dictionary; \verb\__class__\ yields the instance's class. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 747 | |
| 748 | \item[Files] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 749 | A file object represents an open file. (It is a wrapper around a C |
| 750 | {\tt stdio} file pointer.) File objects are created by the |
| 751 | \verb\open()\ built-in function, and also by \verb\posix.popen()\ and |
| 752 | the \verb\makefile\ method of socket objects. \verb\sys.stdin\, |
| 753 | \verb\sys.stdout\ and \verb\sys.stderr\ are file objects corresponding |
| 754 | the the interpreter's standard input, output and error streams. |
| 755 | See the Python Library Reference for methods of file objects and other |
| 756 | details. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 757 | |
| 758 | \item[Internal types] |
| 759 | A few types used internally by the interpreter are exposed to the user. |
| 760 | Their definition may change with future versions of the interpreter, |
| 761 | but they are mentioned here for completeness. |
| 762 | |
| 763 | \begin{description} |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 765 | \item[Code objects] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 766 | Code objects represent executable code. The difference between a code |
| 767 | object and a function object is that the function object contains an |
| 768 | explicit reference to the function's context (the module in which it |
| 769 | was defined) which a code object contains no context. There is no way |
| 770 | to execute a bare code object. |
| 771 | |
| 772 | Special read-only attributes: \verb\co_code\ is a string representing |
| 773 | the sequence of instructions; \verb\co_consts\ is a list of literals |
| 774 | used by the code; \verb\co_names\ is a list of names (strings) used by |
| 775 | the code; \verb\co_filename\ is the filename from which the code was |
| 776 | compiled. (To find out the line numbers, you would have to decode the |
| 777 | instructions; the standard library module \verb\dis\ contains an |
| 778 | example of how to do this.) |
| 779 | |
| 780 | \item[Frame objects] |
| 781 | Frame objects represent execution frames. They may occur in traceback |
| 782 | objects (see below). |
| 783 | |
| 784 | Special read-only attributes: \verb\f_back\ is to the previous |
| 785 | stack frame (towards the caller), or \verb\None\ if this is the bottom |
| 786 | stack frame; \verb\f_code\ is the code object being executed in this |
| 787 | frame; \verb\f_globals\ is the dictionary used to look up global |
| 788 | variables; \verb\f_locals\ is used for local variables; |
| 789 | \verb\f_lineno\ gives the line number and \verb\f_lasti\ gives the |
| 790 | precise instruction (this is an index into the instruction string of |
| 791 | the code object). |
| 792 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 793 | \item[Traceback objects] |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 794 | Traceback objects represent a stack trace of an exception. A |
| 795 | traceback object is created when an exception occurs. When the search |
| 796 | for an exception handler unwinds the execution stack, at each unwound |
| 797 | level a traceback object is inserted in front of the current |
| 798 | traceback. When an exception handler is entered, the stack trace is |
| 799 | made available to the program as \verb\sys.exc_traceback\. When the |
| 800 | program contains no suitable handler, the stack trace is written |
| 801 | (nicely formatted) to the standard error stream; if the interpreter is |
| 802 | interactive, it is made available to the user as |
| 803 | \verb\sys.last_traceback\. |
| 804 | |
| 805 | Special read-only attributes: \verb\tb_next\ is the next level in the |
| 806 | stack trace (towards the frame where the exception occurred), or |
| 807 | \verb\None\ if there is no next level; \verb\tb_frame\ points to the |
| 808 | execution frame of the current level; \verb\tb_lineno\ gives the line |
| 809 | number where the exception occurred; \verb\tb_lasti\ indicates the |
| 810 | precise instruction. The line number and last instruction in the |
| 811 | traceback may differ from the line number of its frame object if the |
| 812 | exception occurred in a \verb\try\ statement with no matching |
| 813 | \verb\except\ clause or with a \verb\finally\ clause. |
| 814 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 815 | \end{description} % Internal types |
| 816 | |
| 817 | \end{description} % Types |
| 818 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 819 | \chapter{Expressions and conditions} |
| 820 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 821 | From now on, extended BNF notation will be used to describe syntax, |
| 822 | not lexical analysis. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 823 | |
| 824 | This chapter explains the meaning of the elements of expressions and |
| 825 | conditions. Conditions are a superset of expressions, and a condition |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 826 | may be used wherever an expression is required by enclosing it in |
| 827 | parentheses. The only places where expressions are used in the syntax |
| 828 | instead of conditions is in expression statements and on the |
| 829 | right-hand side of assignments; this catches some nasty bugs like |
| 830 | accedentally writing \verb\x == 1\ instead of \verb\x = 1\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 831 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 832 | 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] | 833 | operator with a lower precedence than all others, but occasionally |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 834 | serves other purposes as well; e.g., it separates function arguments, |
| 835 | is used in list and dictionary constructors, and has special semantics |
| 836 | in \verb\print\ statements. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 837 | |
| 838 | When (one alternative of) a syntax rule has the form |
| 839 | |
| 840 | \begin{verbatim} |
| 841 | name: othername |
| 842 | \end{verbatim} |
| 843 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 844 | and no semantics are given, the semantics of this form of \verb\name\ |
| 845 | are the same as for \verb\othername\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 846 | |
| 847 | \section{Arithmetic conversions} |
| 848 | |
| 849 | When a description of an arithmetic operator below uses the phrase |
| 850 | ``the numeric arguments are converted to a common type'', |
| 851 | 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] | 852 | \verb\TypeError\ exception is raised, and that otherwise |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 853 | the following conversions are applied: |
| 854 | |
| 855 | \begin{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 856 | \item first, if either argument is a floating point number, |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 857 | the other is converted to floating point; |
| 858 | \item else, if either argument is a long integer, |
| 859 | the other is converted to long integer; |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 860 | \item otherwise, both must be plain integers and no conversion |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 861 | is necessary. |
| 862 | \end{itemize} |
| 863 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 864 | \section{Atoms} |
| 865 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 866 | Atoms are the most basic elements of expressions. Forms enclosed in |
| 867 | reverse quotes or in parentheses, brackets or braces are also |
| 868 | categorized syntactically as atoms. The syntax for atoms is: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 869 | |
| 870 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 871 | atom: identifier | literal | enclosure |
| 872 | enclosure: parenth_form | list_display | dict_display | string_conversion |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 873 | \end{verbatim} |
| 874 | |
| 875 | \subsection{Identifiers (Names)} |
| 876 | |
| 877 | 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] | 878 | or built-in name binding. If a name can be assigned to anywhere in a |
| 879 | code block, and is not mentioned in a \verb\global\ statement in that |
| 880 | 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] | 881 | Otherwise, it refers to a global name if one exists, else to a |
| 882 | built-in name. |
| 883 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 884 | When the name is bound to an object, evaluation of the atom yields |
| 885 | 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] | 886 | raises a \verb\NameError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 887 | |
| 888 | \subsection{Literals} |
| 889 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 890 | Python knows string and numeric literals: |
| 891 | |
| 892 | \begin{verbatim} |
| 893 | literal: stringliteral | integer | longinteger | floatnumber |
| 894 | \end{verbatim} |
| 895 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 896 | Evaluation of a literal yields an object of the given type |
| 897 | (string, integer, long integer, floating point number) |
| 898 | with the given value. |
| 899 | The value may be approximated in the case of floating point literals. |
| 900 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 901 | All literals correspond to immutable data types, and hence the |
| 902 | object's identity is less important than its value. Multiple |
| 903 | evaluations of literals with the same value (either the same |
| 904 | occurrence in the program text or a different occurrence) may obtain |
| 905 | the same object or a different object with the same value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 906 | |
| 907 | (In the original implementation, all literals in the same code block |
| 908 | with the same type and value yield the same object.) |
| 909 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 910 | \subsection{Parenthesized forms} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 911 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 912 | A parenthesized form is an optional condition list enclosed in |
| 913 | parentheses: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 915 | \begin{verbatim} |
| 916 | parenth_form: "(" [condition_list] ")" |
| 917 | \end{verbatim} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 918 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 919 | A parenthesized condition list yields whatever that condition list |
| 920 | yields. |
| 921 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 922 | An empty pair of parentheses yields an empty tuple object. Since |
| 923 | tuples are immutable, the rules for literals apply here. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 924 | |
| 925 | (Note that tuples are not formed by the parentheses, but rather by use |
| 926 | of the comma operator. The exception is the empty tuple, for which |
| 927 | parentheses {\em are} required -- allowing unparenthesized ``nothing'' |
| 928 | in expressions would causes ambiguities and allow common typos to |
| 929 | pass uncaught.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 930 | |
| 931 | \subsection{List displays} |
| 932 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 933 | A list display is a possibly empty series of conditions enclosed in |
| 934 | square brackets: |
| 935 | |
| 936 | \begin{verbatim} |
| 937 | list_display: "[" [condition_list] "]" |
| 938 | \end{verbatim} |
| 939 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 940 | A list display yields a new list object. |
| 941 | |
| 942 | If it has no condition list, the list object has no items. |
| 943 | Otherwise, the elements of the condition list are evaluated |
| 944 | from left to right and inserted in the list object in that order. |
| 945 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 946 | \subsection{Dictionary displays} \label{dict} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 948 | A dictionary display is a possibly empty series of key/datum pairs |
| 949 | enclosed in curly braces: |
| 950 | |
| 951 | \begin{verbatim} |
| 952 | dict_display: "{" [key_datum_list] "}" |
| 953 | key_datum_list: [key_datum ("," key_datum)* [","] |
| 954 | key_datum: condition ":" condition |
| 955 | \end{verbatim} |
| 956 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 957 | A dictionary display yields a new dictionary object. |
| 958 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 959 | The key/datum pairs are evaluated from left to right to define the |
| 960 | entries of the dictionary: each key object is used as a key into the |
| 961 | dictionary to store the corresponding datum. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 963 | Keys must be strings, otherwise a \verb\TypeError\ exception is raised. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 964 | Clashes between duplicate keys are not detected; the last datum |
| 965 | (textually rightmost in the display) stored for a given key value |
| 966 | prevails. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 967 | |
| 968 | \subsection{String conversions} |
| 969 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 970 | A string conversion is a condition list enclosed in reverse (or |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 971 | backward) quotes: |
| 972 | |
| 973 | \begin{verbatim} |
| 974 | string_conversion: "`" condition_list "`" |
| 975 | \end{verbatim} |
| 976 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 977 | A string conversion evaluates the contained condition list and converts the |
| 978 | resulting object into a string according to rules specific to its type. |
| 979 | |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 980 | 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] | 981 | dictionary containing only objects whose type is one of these, the |
| 982 | resulting string is a valid Python expression which can be passed to |
| 983 | 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] | 984 | same value (or an approximation, if floating point numbers are |
| 985 | involved). |
| 986 | |
| 987 | (In particular, converting a string adds quotes around it and converts |
| 988 | ``funny'' characters to escape sequences that are safe to print.) |
| 989 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 990 | It is illegal to attempt to convert recursive objects (e.g., lists or |
| 991 | dictionaries that contain a reference to themselves, directly or |
| 992 | indirectly.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 993 | |
| 994 | \section{Primaries} |
| 995 | |
| 996 | Primaries represent the most tightly bound operations of the language. |
| 997 | Their syntax is: |
| 998 | |
| 999 | \begin{verbatim} |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1000 | primary: atom | attributeref | subscription | slicing | call |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1001 | \end{verbatim} |
| 1002 | |
| 1003 | \subsection{Attribute references} |
| 1004 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1005 | An attribute reference is a primary followed by a period and a name: |
| 1006 | |
| 1007 | \begin{verbatim} |
| 1008 | attributeref: primary "." identifier |
| 1009 | \end{verbatim} |
| 1010 | |
| 1011 | The primary must evaluate to an object of a type that supports |
| 1012 | attribute references, e.g., a module or a list. This object is then |
| 1013 | asked to produce the attribute whose name is the identifier. If this |
| 1014 | attribute is not available, the exception \verb\AttributeError\ is |
| 1015 | raised. Otherwise, the type and value of the object produced is |
| 1016 | determined by the object. Multiple evaluations of the same attribute |
| 1017 | reference may yield different objects. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1018 | |
| 1019 | \subsection{Subscriptions} |
| 1020 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1021 | A subscription selects an item of a sequence or mapping object: |
| 1022 | |
| 1023 | \begin{verbatim} |
| 1024 | subscription: primary "[" condition "]" |
| 1025 | \end{verbatim} |
| 1026 | |
| 1027 | The primary must evaluate to an object of a sequence or mapping type. |
| 1028 | |
| 1029 | If it is a mapping, the condition must evaluate to an object whose |
| 1030 | value is one of the keys of the mapping, and the subscription selects |
| 1031 | the value in the mapping that corresponds to that key. |
| 1032 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1033 | If it is a sequence, the condition must evaluate to a plain integer. |
| 1034 | If this value is negative, the length of the sequence is added to it |
| 1035 | (so that, e.g., \verb\x[-1]\ selects the last item of \verb\x\.) |
| 1036 | The resulting value must be a nonnegative integer smaller than the |
| 1037 | number of items in the sequence, and the subscription selects the item |
| 1038 | whose index is that value (counting from zero). |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1040 | 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] | 1041 | type but a string of exactly one character. |
| 1042 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1043 | \subsection{Slicings} |
| 1044 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1045 | A slicing selects a range of items in a sequence object: |
| 1046 | |
| 1047 | \begin{verbatim} |
| 1048 | slicing: primary "[" [condition] ":" [condition] "]" |
| 1049 | \end{verbatim} |
| 1050 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1051 | The primary must evaluate to a sequence object. The lower and upper |
| 1052 | bound expressions, if present, must evaluate to plain integers; |
| 1053 | defaults are zero and the sequence's length, respectively. If either |
| 1054 | bound is negative, the sequence's length is added to it. The slicing |
| 1055 | now selects all items with index $k$ such that $i <= k < j$ where $i$ |
| 1056 | and $j$ are the specified lower and upper bounds. This may be an |
| 1057 | empty sequence. It is not an error if $i$ or $j$ lie outside the |
| 1058 | range of valid indexes (such items don't exist so they aren't |
| 1059 | selected). |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1060 | |
| 1061 | \subsection{Calls} |
| 1062 | |
| 1063 | A call calls a function with a possibly empty series of arguments: |
| 1064 | |
| 1065 | \begin{verbatim} |
| 1066 | call: primary "(" [condition_list] ")" |
| 1067 | \end{verbatim} |
| 1068 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1069 | The primary must evaluate to a callable object (user-defined |
| 1070 | functions, built-in functions, methods of built-in objects, class |
| 1071 | objects, and methods of class instances are callable). If it is a |
| 1072 | class, the argument list must be empty. |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1073 | |
| 1074 | XXX explain what happens on function call |
| 1075 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1076 | \section{Factors} |
| 1077 | |
| 1078 | Factors represent the unary numeric operators. |
| 1079 | Their syntax is: |
| 1080 | |
| 1081 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1082 | factor: primary | "-" factor | "+" factor | "~" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1083 | \end{verbatim} |
| 1084 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1085 | The unary \verb\"-"\ operator yields the negative of its |
| 1086 | numeric argument. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1087 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1088 | The unary \verb\"+"\ operator yields its numeric argument unchanged. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1089 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1090 | The unary \verb\"~"\ operator yields the bit-wise negation of its |
| 1091 | plain or long integer argument. The bit-wise negation negation of |
| 1092 | \verb\x\ is defined as \verb\-(x+1)\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1093 | |
| 1094 | 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] | 1095 | a \verb\TypeError\ exception is raised. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1096 | |
| 1097 | \section{Terms} |
| 1098 | |
| 1099 | Terms represent the most tightly binding binary operators: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1100 | % |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1101 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1102 | term: factor | term "*" factor | term "/" factor | term "%" factor |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1103 | \end{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1104 | % |
| 1105 | The \verb\"*"\ (multiplication) operator yields the product of its |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1106 | arguments. The arguments must either both be numbers, or one argument |
| 1107 | must be a plain integer and the other must be a sequence. In the |
| 1108 | former case, the numbers are converted to a common type and then |
| 1109 | multiplied together. In the latter case, sequence repetition is |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1110 | performed; a negative repetition factor yields an empty sequence. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1112 | The \verb\"/"\ (division) operator yields the quotient of its |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1113 | arguments. The numeric arguments are first converted to a common |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1114 | type. Plain or long integer division yields an integer of the same |
| 1115 | type; the result is that of mathematical division with the `floor' |
| 1116 | function applied to the result. Division by zero raises the |
| 1117 | \verb\ZeroDivisionError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1118 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1119 | The \verb\"%"\ (modulo) operator yields the remainder from the |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1120 | division of the first argument by the second. The numeric arguments |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1121 | are first converted to a common type. A zero right argument raises the |
| 1122 | \verb\ZeroDivisionError\ exception. The arguments may be floating point |
| 1123 | 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] | 1124 | always yields a result with the same sign as its second operand (or |
| 1125 | zero); the absolute value of the result is strictly smaller than the |
| 1126 | second operand. |
| 1127 | |
| 1128 | The integer division and modulo operators are connected by the |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1129 | following identity: \verb\x == (x/y)*y + (x%y)\. |
| 1130 | Integer division and modulo are also connected with the built-in |
| 1131 | function \verb\divmod()\: \verb\divmod(x, y) == (x/y, x%y)\. |
| 1132 | These identities don't hold for floating point numbers; there a |
| 1133 | similar identity holds where \verb\x/y\ is replaced by |
| 1134 | \verb\floor(x/y)\). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1135 | |
| 1136 | \section{Arithmetic expressions} |
| 1137 | |
| 1138 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1139 | arith_expr: term | arith_expr "+" term | arith_expr "-" term |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1140 | \end{verbatim} |
| 1141 | |
Guido van Rossum | 670e5a0 | 1992-01-17 14:03:20 +0000 | [diff] [blame] | 1142 | The \verb|"+"| operator yields the sum of its arguments. The |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1143 | arguments must either both be numbers, or both sequences of the same |
| 1144 | type. In the former case, the numbers are converted to a common type |
| 1145 | and then added together. In the latter case, the sequences are |
| 1146 | concatenated. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1148 | The \verb|"-"| operator yields the difference of its arguments. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1149 | The numeric arguments are first converted to a common type. |
| 1150 | |
| 1151 | \section{Shift expressions} |
| 1152 | |
| 1153 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1154 | shift_expr: arith_expr | shift_expr ( "<<" | ">>" ) arith_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1155 | \end{verbatim} |
| 1156 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1157 | These operators accept plain or long integers as arguments. The |
| 1158 | arguments are converted to a common type. They shift the first |
| 1159 | argument to the left or right by the number of bits given by the |
| 1160 | second argument. |
| 1161 | |
| 1162 | A right shift by $n$ bits is defined as division by $2^n$. A left |
| 1163 | shift by $n$ bits is defined as multiplication with $2^n$ without |
| 1164 | overflow check; for plain integers this drops bits if the result is |
| 1165 | not less than $2^{31} - 1$ in absolute value. |
| 1166 | |
| 1167 | Negative shift counts raise a \verb\ValueError\ exception. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1168 | |
| 1169 | \section{Bitwise AND expressions} |
| 1170 | |
| 1171 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1172 | and_expr: shift_expr | and_expr "&" shift_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1173 | \end{verbatim} |
| 1174 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1175 | This operator yields the bitwise AND of its arguments, which must be |
| 1176 | 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] | 1177 | |
| 1178 | \section{Bitwise XOR expressions} |
| 1179 | |
| 1180 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1181 | xor_expr: and_expr | xor_expr "^" and_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1182 | \end{verbatim} |
| 1183 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1184 | This operator yields the bitwise exclusive OR of its arguments, which |
| 1185 | must be plain or long integers. The arguments are converted to a |
| 1186 | common type. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1187 | |
| 1188 | \section{Bitwise OR expressions} |
| 1189 | |
| 1190 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1191 | or_expr: xor_expr | or_expr "|" xor_expr |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1192 | \end{verbatim} |
| 1193 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1194 | This operator yields the bitwise OR of its arguments, which must be |
| 1195 | 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] | 1196 | |
| 1197 | \section{Comparisons} |
| 1198 | |
| 1199 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1200 | comparison: or_expr (comp_operator or_expr)* |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1201 | comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1202 | \end{verbatim} |
| 1203 | |
| 1204 | Comparisons yield integer value: 1 for true, 0 for false. |
| 1205 | |
| 1206 | Comparisons can be chained arbitrarily, |
| 1207 | e.g., $x < y <= z$ is equivalent to |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1208 | $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] | 1209 | (but in both cases $z$ is not evaluated at all when $x < y$ is |
| 1210 | found to be false). |
| 1211 | |
| 1212 | 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] | 1213 | $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] | 1214 | $e_{n-1} op_n e_n$, except that each expression is evaluated at most once. |
| 1215 | |
| 1216 | Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison |
| 1217 | between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. |
| 1218 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1219 | The forms \verb\<>\ and \verb\!=\ are equivalent; for consistency with |
| 1220 | C, \verb\!=\ is preferred; where \verb\!=\ is mentioned below |
| 1221 | \verb\<>\ is also implied. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1223 | The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1224 | 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] | 1225 | If both are numbers, they are coverted to a common type. Otherwise, |
| 1226 | objects of different types {\em always} compare unequal, and are |
| 1227 | ordered consistently but arbitrarily. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1228 | |
| 1229 | (This unusual |
| 1230 | definition of comparison is done to simplify the definition of |
Guido van Rossum | 4fc43bc | 1991-11-25 17:26:57 +0000 | [diff] [blame] | 1231 | 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] | 1232 | |
| 1233 | Comparison of objects of the same type depends on the type: |
| 1234 | |
| 1235 | \begin{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1236 | |
| 1237 | \item |
| 1238 | Numbers are compared arithmetically. |
| 1239 | |
| 1240 | \item |
| 1241 | Strings are compared lexicographically using the numeric equivalents |
| 1242 | (the result of the built-in function \verb\ord\) of their characters. |
| 1243 | |
| 1244 | \item |
| 1245 | Tuples and lists are compared lexicographically using comparison of |
| 1246 | corresponding items. |
| 1247 | |
| 1248 | \item |
| 1249 | Mappings (dictionaries) are compared through lexicographic |
| 1250 | comparison of their sorted (key, value) lists.% |
| 1251 | \footnote{This is expensive since it requires sorting the keys first, |
| 1252 | but about the only sensible definition. It was tried to compare |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1253 | dictionaries using the rule below for most other types, but this gave |
| 1254 | surprises in cases like \verb|if d == {}: ...|.} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1255 | |
| 1256 | \item |
| 1257 | Most other types compare unequal unless they are the same object; |
| 1258 | the choice whether one object is considered smaller or larger than |
| 1259 | another one is made arbitrarily but consistently within one |
| 1260 | execution of a program. |
| 1261 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1262 | \end{itemize} |
| 1263 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1264 | The operators \verb\in\ and \verb\not in\ test for sequence |
| 1265 | membership: if $y$ is a sequence, $x ~\verb\in\~ y$ is true if and |
| 1266 | only if there exists an index $i$ such that $x = y[i]$. |
| 1267 | $x ~\verb\not in\~ y$ yields the inverse truth value. The exception |
| 1268 | \verb\TypeError\ is raised when $y$ is not a sequence, or when $y$ is |
| 1269 | a string and $x$ is not a string of length one.% |
| 1270 | \footnote{The latter restriction is sometimes a nuisance.} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1271 | |
| 1272 | The operators \verb\is\ and \verb\is not\ compare object identity: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1273 | $x ~\verb\is\~ y$ is true if and only if $x$ and $y$ are the same |
| 1274 | object. $x ~\verb\is not\~ y$ yields the inverse truth value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1275 | |
| 1276 | \section{Boolean operators} |
| 1277 | |
| 1278 | \begin{verbatim} |
| 1279 | condition: or_test |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1280 | or_test: and_test | or_test "or" and_test |
| 1281 | and_test: not_test | and_test "and" not_test |
| 1282 | not_test: comparison | "not" not_test |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1283 | \end{verbatim} |
| 1284 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1285 | In the context of Boolean operators, and also when conditions are used |
| 1286 | by control flow statements, the following values are interpreted as |
| 1287 | false: \verb\None\, numeric zero of all types, empty sequences |
| 1288 | (strings, tuples and lists), and empty mappings (dictionaries). All |
| 1289 | other values are interpreted as true. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1290 | |
| 1291 | The operator \verb\not\ yields 1 if its argument is false, 0 otherwise. |
| 1292 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1293 | 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] | 1294 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 1295 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1296 | 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] | 1297 | $x$ is returned; otherwise, $y$ is evaluated and returned. |
| 1298 | |
| 1299 | (Note that \verb\and\ and \verb\or\ do not restrict the value and type |
| 1300 | 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] | 1301 | This is sometimes useful, e.g., if \verb\s\ is a string, which should be |
| 1302 | 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] | 1303 | returns the desired value. Because \verb\not\ has to invent a value |
| 1304 | 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] | 1305 | argument, so \verb\not 'foo'\ yields \verb\0\, not \verb\''\.) |
| 1306 | |
| 1307 | \section{Expression lists and condition lists} |
| 1308 | |
| 1309 | \begin{verbatim} |
| 1310 | expr_list: or_expr ("," or_expr)* [","] |
| 1311 | cond_list: condition ("," condition)* [","] |
| 1312 | \end{verbatim} |
| 1313 | |
| 1314 | The only difference between expression lists and condition lists is |
| 1315 | the lowest priority of operators that can be used in them without |
| 1316 | being enclosed in parentheses; condition lists allow all operators, |
| 1317 | while expression lists don't allow comparisons and Boolean operators |
| 1318 | (they do allow bitwise and shift operators though). |
| 1319 | |
| 1320 | Expression lists are used in expression statements and assignments; |
| 1321 | condition lists are used everywhere else. |
| 1322 | |
| 1323 | An expression (condition) list containing at least one comma yields a |
| 1324 | tuple. The length of the tuple is the number of expressions |
| 1325 | (conditions) in the list. The expressions (conditions) are evaluated |
| 1326 | from left to right. |
| 1327 | |
| 1328 | The trailing comma is required only to create a single tuple (a.k.a. a |
| 1329 | {\em singleton}); it is optional in all other cases. A single |
| 1330 | expression (condition) without a trailing comma doesn't create a |
| 1331 | tuple, but rather yields the value of that expression (condition). |
| 1332 | |
| 1333 | 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] | 1334 | |
| 1335 | \chapter{Simple statements} |
| 1336 | |
| 1337 | Simple statements are comprised within a single logical line. |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1338 | Several simple statements may occur on a single line separated |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1339 | by semicolons. The syntax for simple statements is: |
| 1340 | |
| 1341 | \begin{verbatim} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1342 | simple_stmt: expression_stmt |
| 1343 | | assignment |
| 1344 | | pass_stmt |
| 1345 | | del_stmt |
| 1346 | | print_stmt |
| 1347 | | return_stmt |
| 1348 | | raise_stmt |
| 1349 | | break_stmt |
| 1350 | | continue_stmt |
| 1351 | | import_stmt |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1352 | | global_stmt |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1353 | \end{verbatim} |
| 1354 | |
| 1355 | \section{Expression statements} |
| 1356 | |
| 1357 | \begin{verbatim} |
| 1358 | expression_stmt: expression_list |
| 1359 | \end{verbatim} |
| 1360 | |
| 1361 | An expression statement evaluates the expression list (which may |
| 1362 | be a single expression). |
| 1363 | If the value is not \verb\None\, it is converted to a string |
| 1364 | using the rules for string conversions, and the resulting string |
| 1365 | is written to standard output on a line by itself. |
| 1366 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1367 | (The exception for \verb\None\ is made so that procedure calls, which |
| 1368 | are syntactically equivalent to expressions, do not cause any output. |
| 1369 | A tuple with only \verb\None\ items is written normally.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1370 | |
| 1371 | \section{Assignments} |
| 1372 | |
| 1373 | \begin{verbatim} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1374 | assignment: (target_list "=")+ expression_list |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1375 | target_list: target ("," target)* [","] |
| 1376 | target: identifier | "(" target_list ")" | "[" target_list "]" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1377 | | attributeref | subscription | slicing |
| 1378 | \end{verbatim} |
| 1379 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1380 | (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] | 1381 | three symbols.) |
| 1382 | |
| 1383 | An assignment evaluates the expression list (remember that this can |
| 1384 | be a single expression or a comma-separated list, |
| 1385 | the latter yielding a tuple) |
| 1386 | and assigns the single resulting object to each of the target lists, |
| 1387 | from left to right. |
| 1388 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1389 | Assignment is defined recursively depending on the form of the target. |
| 1390 | When a target is part of a mutable object (an attribute reference, |
| 1391 | subscription or slicing), the mutable object must ultimately perform |
| 1392 | the assignment and decide about its validity, and may raise an |
| 1393 | exception if the assignment is unacceptable. The rules observed by |
| 1394 | various types and the exceptions raised are given with the definition |
| 1395 | of the object types (some of which are defined in the library |
| 1396 | reference). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1397 | |
| 1398 | Assignment of an object to a target list is recursively |
| 1399 | defined as follows. |
| 1400 | |
| 1401 | \begin{itemize} |
| 1402 | \item |
| 1403 | If the target list contains no commas (except in nested constructs): |
| 1404 | the object is assigned to the single target contained in the list. |
| 1405 | |
| 1406 | \item |
| 1407 | 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] | 1408 | 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] | 1409 | as the list contains targets, and the items are assigned, from left |
| 1410 | to right, to the corresponding targets. |
| 1411 | |
| 1412 | \end{itemize} |
| 1413 | |
| 1414 | Assignment of an object to a (non-list) |
| 1415 | target is recursively defined as follows. |
| 1416 | |
| 1417 | \begin{itemize} |
| 1418 | |
| 1419 | \item |
| 1420 | If the target is an identifier (name): |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1421 | \begin{itemize} |
| 1422 | \item |
| 1423 | If the name does not occur in a \verb\global\ statement in the current |
| 1424 | code block: the object is bound to that name in the current local |
| 1425 | name space. |
| 1426 | \item |
| 1427 | Otherwise: the object is bound to that name in the current global name |
| 1428 | space. |
| 1429 | \end{itemize} |
| 1430 | 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] | 1431 | |
| 1432 | \item |
| 1433 | If the target is a target list enclosed in parentheses: |
| 1434 | the object is assigned to that target list. |
| 1435 | |
| 1436 | \item |
| 1437 | If the target is a target list enclosed in square brackets: |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1438 | 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] | 1439 | as the target list contains targets, |
| 1440 | and the list's items are assigned, from left to right, |
| 1441 | to the corresponding targets. |
| 1442 | |
| 1443 | \item |
| 1444 | If the target is an attribute reference: |
| 1445 | The primary expression in the reference is evaluated. |
| 1446 | It should yield an object with assignable attributes; |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1447 | if this is not the case, \verb\TypeError\ is raised. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1448 | That object is then asked to assign the assigned object |
| 1449 | to the given attribute; if it cannot perform the assignment, |
| 1450 | it raises an exception. |
| 1451 | |
| 1452 | \item |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1453 | If the target is a subscription: The primary expression in the |
| 1454 | reference is evaluated. It should yield either a mutable sequence |
| 1455 | (list) object or a mapping (dictionary) object. Next, the subscript |
| 1456 | expression is evaluated. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1457 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1458 | If the primary is a sequence object, the subscript must yield a plain |
| 1459 | integer. If it is negative, the sequence's length is added to it. |
| 1460 | The resulting value must be a nonnegative integer less than the |
| 1461 | sequence's length, and the sequence is asked to assign the assigned |
| 1462 | object to its item with that index. If the index is out of range, |
| 1463 | \verb\IndexError\ is raised (assignment to a subscripted sequence |
| 1464 | cannot add new items to a list). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1465 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1466 | If the primary is a mapping object, the subscript must have a type |
| 1467 | compatible with the mapping's key type, and the mapping is then asked |
| 1468 | to to create a key/datum pair which maps the subscript to the assigned |
| 1469 | object. This can either replace an existing key/value pair with the |
| 1470 | same key value, or insert a new key/value pair (if no key with the |
| 1471 | same value existed). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1472 | |
| 1473 | \item |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1474 | If the target is a slicing: The primary expression in the reference is |
| 1475 | evaluated. It should yield a mutable sequence (list) object. The |
| 1476 | assigned object should be a sequence object of the same type. Next, |
| 1477 | the lower and upper bound expressions are evaluated, insofar they are |
| 1478 | present; defaults are zero and the sequence's length. The bounds |
| 1479 | should evaluate to (small) integers. If either bound is negative, the |
| 1480 | sequence's length is added to it. The resulting bounds are clipped to |
| 1481 | lie between zero and the sequence's length, inclusive. Finally, the |
| 1482 | sequence object is asked to replace the items indicated by the slice |
| 1483 | with the items of the assigned sequence. This may change the |
| 1484 | sequence's length, if it allows it. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1485 | |
| 1486 | \end{itemize} |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1488 | (In the original implementation, the syntax for targets is taken |
| 1489 | to be the same as for expressions, and invalid syntax is rejected |
| 1490 | during the code generation phase, causing less detailed error |
| 1491 | messages.) |
| 1492 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1493 | \section{The {\tt pass} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1494 | |
| 1495 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1496 | pass_stmt: "pass" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1497 | \end{verbatim} |
| 1498 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1499 | \verb\pass\ is a null operation -- when it is executed, nothing |
| 1500 | happens. It is useful as a placeholder when a statement is |
| 1501 | required syntactically, but no code needs to be executed, for example: |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1502 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1503 | \begin{verbatim} |
| 1504 | def f(arg): pass # a no-op function |
| 1505 | |
| 1506 | class C: pass # an empty class |
| 1507 | \end{verbatim} |
| 1508 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1509 | \section{The {\tt del} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1510 | |
| 1511 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1512 | del_stmt: "del" target_list |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1513 | \end{verbatim} |
| 1514 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1515 | Deletion is recursively defined very similar to the way assignment is |
| 1516 | defined. Rather that spelling it out in full details, here are some |
| 1517 | hints. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1518 | |
| 1519 | Deletion of a target list recursively deletes each target, |
| 1520 | from left to right. |
| 1521 | |
| 1522 | 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] | 1523 | from the local or global name space, depending on whether the name |
| 1524 | occurs in a \verb\global\ statement in the same code block. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1525 | |
| 1526 | Deletion of attribute references, subscriptions and slicings |
| 1527 | is passed to the primary object involved; deletion of a slicing |
| 1528 | is in general equivalent to assignment of an empty slice of the |
| 1529 | right type (but even this is determined by the sliced object). |
| 1530 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1531 | \section{The {\tt print} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1532 | |
| 1533 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1534 | print_stmt: "print" [ condition ("," condition)* [","] ] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1535 | \end{verbatim} |
| 1536 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1537 | \verb\print\ evaluates each condition in turn and writes the resulting |
| 1538 | object to standard output (see below). If an object is not a string, |
| 1539 | it is first converted to a string using the rules for string |
| 1540 | conversions. The (resulting or original) string is then written. A |
| 1541 | space is written before each object is (converted and) written, unless |
| 1542 | the output system believes it is positioned at the beginning of a |
| 1543 | line. This is the case: (1) when no characters have yet been written |
| 1544 | to standard output; or (2) when the last character written to standard |
| 1545 | output is \verb/\n/; or (3) when the last write operation on standard |
| 1546 | output was not a \verb\print\ statement. (In some cases it may be |
| 1547 | functional to write an empty string to standard output for this |
| 1548 | reason.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1549 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1550 | A \verb/"\n"/ character is written at the end, unless the \verb\print\ |
| 1551 | statement ends with a comma. This is the only action if the statement |
| 1552 | contains just the keyword \verb\print\. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1553 | |
| 1554 | Standard output is defined as the file object named \verb\stdout\ |
| 1555 | 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] | 1556 | 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] | 1557 | (The original implementation attempts to write to the system's original |
| 1558 | standard output instead, but this is not safe, and should be fixed.) |
| 1559 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1560 | \section{The {\tt return} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1561 | |
| 1562 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1563 | return_stmt: "return" [condition_list] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1564 | \end{verbatim} |
| 1565 | |
| 1566 | \verb\return\ may only occur syntactically nested in a function |
| 1567 | definition, not within a nested class definition. |
| 1568 | |
| 1569 | If a condition list is present, it is evaluated, else \verb\None\ |
| 1570 | is substituted. |
| 1571 | |
| 1572 | \verb\return\ leaves the current function call with the condition |
| 1573 | list (or \verb\None\) as return value. |
| 1574 | |
| 1575 | When \verb\return\ passes control out of a \verb\try\ statement |
| 1576 | with a \verb\finally\ clause, that finally clause is executed |
| 1577 | before really leaving the function. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1578 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1579 | \section{The {\tt raise} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1580 | |
| 1581 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1582 | raise_stmt: "raise" condition ["," condition] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1583 | \end{verbatim} |
| 1584 | |
| 1585 | \verb\raise\ evaluates its first condition, which must yield |
| 1586 | a string object. If there is a second condition, this is evaluated, |
| 1587 | else \verb\None\ is substituted. |
| 1588 | |
| 1589 | It then raises the exception identified by the first object, |
| 1590 | with the second one (or \verb\None\) as its parameter. |
| 1591 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1592 | \section{The {\tt break} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1593 | |
| 1594 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1595 | break_stmt: "break" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1596 | \end{verbatim} |
| 1597 | |
| 1598 | \verb\break\ may only occur syntactically nested in a \verb\for\ |
| 1599 | or \verb\while\ loop, not nested in a function or class definition. |
| 1600 | |
| 1601 | It terminates the neares enclosing loop, skipping the optional |
| 1602 | \verb\else\ clause if the loop has one. |
| 1603 | |
| 1604 | 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] | 1605 | target keeps its current value. |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1606 | |
| 1607 | When \verb\break\ passes control out of a \verb\try\ statement |
| 1608 | with a \verb\finally\ clause, that finally clause is executed |
| 1609 | before really leaving the loop. |
| 1610 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1611 | \section{The {\tt continue} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1612 | |
| 1613 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1614 | continue_stmt: "continue" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1615 | \end{verbatim} |
| 1616 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1617 | \verb\continue\ may only occur syntactically nested in a \verb\for\ or |
| 1618 | \verb\while\ loop, not nested in a function or class definition, and |
| 1619 | not nested in the \verb\try\ clause of a \verb\try\ statement with a |
| 1620 | \verb\finally\ clause (it may occur nested in a \verb\except\ or |
| 1621 | \verb\finally\ clause of a \verb\try\ statement though). |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1622 | |
| 1623 | It continues with the next cycle of the nearest enclosing loop. |
| 1624 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1625 | \section{The {\tt import} statement} \label{import} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1626 | |
| 1627 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1628 | import_stmt: "import" identifier ("," identifier)* |
| 1629 | | "from" identifier "import" identifier ("," identifier)* |
| 1630 | | "from" identifier "import" "*" |
| 1631 | \end{verbatim} |
| 1632 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1633 | Import statements are executed in two steps: (1) find a module, and |
| 1634 | initialize it if necessary; (2) define a name or names in the local |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1635 | name space (of the scope where the \verb\import\ statement occurs). |
| 1636 | The first form (without \verb\from\) repeats these steps for each |
| 1637 | identifier in the list, the \verb\from\ form performs them once, with |
| 1638 | the first identifier specifying the module name. |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1640 | The system maintains a table of modules that have been initialized, |
| 1641 | indexed by module name. (The current implementation makes this table |
| 1642 | accessible as \verb\sys.modules\.) When a module name is found in |
| 1643 | this table, step (1) is finished. If not, a search for a module |
| 1644 | definition is started. This first looks for a built-in module |
| 1645 | definition, and if no built-in module if the given name is found, it |
| 1646 | searches a user-specified list of directories for a file whose name is |
| 1647 | the module name with extension \verb\".py"\. (The current |
| 1648 | implementation uses the list of strings \verb\sys.path\ as the search |
| 1649 | path; it is initialized from the shell environment variable |
| 1650 | \verb\$PYTHONPATH\, with an installation-dependent default.) |
| 1651 | |
| 1652 | If a built-in module is found, its built-in initialization code is |
| 1653 | executed and step (1) is finished. If no matching file is found, |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1654 | \verb\ImportError\ is raised. If a file is found, it is parsed, |
| 1655 | yielding an executable code block. If a syntax error occurs, |
| 1656 | \verb\SyntaxError\ is raised. Otherwise, an empty module of the given |
| 1657 | name is created and inserted in the module table, and then the code |
| 1658 | block is executed in the context of this module. Exceptions during |
| 1659 | this execution terminate step (1). |
| 1660 | |
| 1661 | When step (1) finishes without raising an exception, step (2) can |
| 1662 | begin. |
| 1663 | |
| 1664 | The first form of \verb\import\ statement binds the module name in the |
| 1665 | local name space to the module object, and then goes on to import the |
| 1666 | next identifier, if any. The \verb\from\ from does not bind the |
| 1667 | module name: it goes through the list of identifiers, looks each one |
| 1668 | of them up in the module found in step (1), and binds the name in the |
| 1669 | local name space to the object thus found. If a name is not found, |
| 1670 | \verb\ImportError\ is raised. If the list of identifiers is replaced |
| 1671 | by a star (\verb\*\), all names defined in the module are bound, |
| 1672 | except those beginning with an underscore(\verb\_\). |
| 1673 | |
| 1674 | Names bound by import statements may not occur in \verb\global\ |
| 1675 | statements in the same scope. |
| 1676 | |
| 1677 | The \verb\from\ form with \verb\*\ may only occur in a module scope. |
| 1678 | |
| 1679 | (The current implementation does not enforce the latter two |
| 1680 | restrictions, but programs should not abuse this freedom, as future |
| 1681 | implementations may enforce them or silently change the meaning of the |
| 1682 | program.) |
Guido van Rossum | 0f1f9da | 1992-01-20 17:10:21 +0000 | [diff] [blame] | 1683 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1684 | \section{The {\tt global} statement} \label{global} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1685 | |
| 1686 | \begin{verbatim} |
| 1687 | global_stmt: "global" identifier ("," identifier)* |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1688 | \end{verbatim} |
| 1689 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1690 | The \verb\global\ statement is a declaration which holds for the |
| 1691 | entire current scope. It means that the listed identifiers are to be |
| 1692 | interpreted as globals. While {\em using} global names is automatic |
| 1693 | if they are not defined in the local scope, {\em assigning} to global |
| 1694 | names would be impossible without \verb\global\. |
| 1695 | |
| 1696 | Names listed in a \verb\global\ statement must not be used in the same |
| 1697 | scope before that \verb\global\ statement is executed. |
| 1698 | |
| 1699 | Name listed in a \verb\global\ statement must not be defined as formal |
| 1700 | parameters or in a \verb\for\ loop control target, \verb\class\ |
| 1701 | definition, function definition, or \verb\import\ statement. |
| 1702 | |
| 1703 | (The current implementation does not enforce the latter two |
| 1704 | restrictions, but programs should not abuse this freedom, as future |
| 1705 | implementations may enforce them or silently change the meaning of the |
| 1706 | program.) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1707 | |
| 1708 | \chapter{Compound statements} |
| 1709 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1710 | Compound statements contain (groups of) other statements; they affect |
| 1711 | or control the execution of those other statements in some way. |
| 1712 | |
| 1713 | The \verb\if\, \verb\while\ and \verb\for\ statements implement |
| 1714 | traditional control flow constructs. \verb\try\ specifies exception |
| 1715 | handlers and/or cleanup code for a group of statements. Function and |
| 1716 | class definitions are also syntactically compound statements. |
| 1717 | |
| 1718 | Compound statements consist of one or more `clauses'. A clause |
| 1719 | consists of a header and a `suite'. The clause headers of a |
| 1720 | particular compound statement are all at the same indentation level; |
| 1721 | all clauses begin with a uniquely identifying keyword and end with a |
| 1722 | colon. A suite is a group of statements controlled by a clause. A |
| 1723 | suite can be a bunch of semicolon-separated simple statements on the |
| 1724 | same line as the header, following the colon, or it can be a list of |
| 1725 | indented statements. Only the latter form of suite can contain nested |
| 1726 | compound statements; the following is illegal (mostly because it |
| 1727 | wouldn't be clear what to do with \verb\else\): |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1728 | |
| 1729 | \begin{verbatim} |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1730 | if test1: if test2: print x |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1731 | \end{verbatim} |
| 1732 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1733 | Also note that the semicolon binds tighter that the colon in this |
| 1734 | context (so to speak), so that in the following example, either all or |
| 1735 | none of the \verb\print\ statements are executed: |
| 1736 | |
| 1737 | \begin{verbatim} |
| 1738 | if some_test: print x; print y; print z |
| 1739 | \end{verbatim} |
| 1740 | |
| 1741 | Summarizing: |
| 1742 | |
| 1743 | \begin{verbatim} |
| 1744 | compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 1745 | suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT |
| 1746 | statement: stmt_list NEWLINE | compound_stmt |
| 1747 | stmt_list: simple_stmt (";" simple_stmt)* [";"] |
| 1748 | \end{verbatim} |
| 1749 | |
| 1750 | Note that statements always ends in a \verb\NEWLINE\ possibly followed |
| 1751 | by a \verb\DEDENT\. |
| 1752 | |
| 1753 | Also note that optional continuation clauses always begin with a |
| 1754 | keyword that cannot start a statement, thus there are no ambiguities |
| 1755 | (the `dangling \verb\else\' problem is solved in Python by requiring |
| 1756 | nested \verb\if\ statements to be indented). |
| 1757 | |
| 1758 | The formatting of the grammar rules in the following section places |
| 1759 | each clause on a separate line for clarity. |
| 1760 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1761 | \section{The {\tt if} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1762 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1763 | The \verb\if\ statement is used for conditional execution: |
| 1764 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1765 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1766 | if_stmt: "if" condition ":" suite |
| 1767 | ("elif" condition ":" suite)* |
| 1768 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1769 | \end{verbatim} |
| 1770 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1771 | It selects exactly one of the suites, by testing the conditions one by |
| 1772 | one until one is true; then that suite is executed. If all conditions |
| 1773 | are false, the suite of the \verb\else\ clause is executed, if present. |
| 1774 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1775 | \section{The {\tt while} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1776 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1777 | The \verb\while\ statement is used for repeated execution as long as a |
| 1778 | condition is true: |
| 1779 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1780 | \begin{verbatim} |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1781 | while_stmt: "while" condition ":" suite |
| 1782 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1783 | \end{verbatim} |
| 1784 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1785 | This repeatedly tests the condition and, if it is true, executes the |
| 1786 | first suite; if the condition is false (which may be the first time it |
| 1787 | is tested) the suite of the \verb\else\ clause is executed, if |
| 1788 | present, and the loop terminates. |
| 1789 | |
| 1790 | A \verb\break\ statement executed in the first suite terminates the |
| 1791 | loop without executing the \verb\else\ clause's suite. A |
| 1792 | \verb\continue\ statement executed in the first suited skips the rest |
| 1793 | of the suite and goes back to testing the condition. |
| 1794 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1795 | \section{The {\tt for} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1796 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1797 | The \verb\for\ statement is used to iterate over the elements of a |
| 1798 | sequence (string, tuple or list): |
| 1799 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1800 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1801 | for_stmt: "for" target_list "in" condition_list ":" suite |
| 1802 | ["else" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1803 | \end{verbatim} |
| 1804 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1805 | The suite is executed once for each item in the condition list, in the |
| 1806 | order of ascending indices. Each item in turn is assigned to the |
| 1807 | target list using the standard rules for assignments, and then the |
| 1808 | suite is executed. When the list is exhausted (which is immediately |
| 1809 | when the sequence is empty), the suite in the \verb\else\ clause is |
| 1810 | executed, if present. |
| 1811 | |
| 1812 | A \verb\break\ statement executed in the first suite terminates the |
| 1813 | loop without executing the \verb\else\ clause's suite. A |
| 1814 | \verb\continue\ statement executed in the first suited skips the rest |
| 1815 | of the suite and continues with the next item or with the \verb\else\ |
| 1816 | clause. |
| 1817 | |
| 1818 | The suite may assign to the variable(s) in the target list; this does |
| 1819 | not affect the next item assigned to it. |
| 1820 | |
| 1821 | The target list are not deleted when the loop is finished (but if the |
| 1822 | loop has executed 0 times it will not have been assigned to at all by |
| 1823 | the loop). |
| 1824 | |
| 1825 | The built-in function \verb\range()\ returns a sequence of integers |
| 1826 | suitable to emulate the effect of Pascal's \verb\for i := 1 to n do\. |
| 1827 | |
| 1828 | {\bf Warning:} There is a subtlety when the sequence is being modified |
| 1829 | by the loop (this can only occur for lists). An internal counter is |
| 1830 | used to keep track of which item is used next, and this is incremented |
| 1831 | on each iteration. When this counter has reached the end of the |
| 1832 | sequence the loop terminates. This means that if the suite deletes |
| 1833 | the current (or a previous) item from the sequence, the next item will |
| 1834 | be skipped (since it gets the index of the current item and this has |
| 1835 | already been treated). Likewise, if the suite inserts an item in the |
| 1836 | sequence before the current item, the current item will be treated |
| 1837 | again the next time through the loop. This can lead to nasty bugs |
| 1838 | that can be avoided by making a temporary copy using the \ |
| 1839 | |
Guido van Rossum | 68c172e | 1992-01-21 11:34:56 +0000 | [diff] [blame] | 1840 | \section{The {\tt try} statement} |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1841 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1842 | The \verb\try\ statement specifies exception handlers and/or cleanup |
| 1843 | code for a group of statements: |
| 1844 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1845 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1846 | try_stmt: "try" ":" suite |
| 1847 | ("except" condition ["," condition] ":" suite)* |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1848 | ["except" ":" suite] |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1849 | ["finally" ":" suite] |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1850 | \end{verbatim} |
| 1851 | |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1852 | There are really two forms: \verb\try...except\ and |
| 1853 | \verb\try...finally\. A \verb\try\ statement with both types of |
| 1854 | clauses is equivalent to a \verb\try...finally\ statement with a |
| 1855 | \verb\try...except\ statement in its \verb\try\ clause. A \verb\try\ |
| 1856 | statement with neither a \verb\except\ clause nor a \verb\finally\ |
| 1857 | clause just executes the suite of statements in its \verb\try\ clause. |
| 1858 | |
| 1859 | The \verb\try...except\ form specifies one or more exception handlers. |
| 1860 | When no exception occurs in the \verb\try\ clause, no exception |
| 1861 | handler is executed. When an exception occurs in the \verb\try\ |
| 1862 | suite, a search for an exception handler HIRO |
| 1863 | |
| 1864 | The \verb\try...finally\ form specifies a `cleanup' handler. The |
| 1865 | \verb\try\ clause is executed. When no exception occurs, the |
| 1866 | \verb\finally\ clause is executed. When an exception occurs on the |
| 1867 | \verb\try\ clause, the exception is temporarily saved, the |
| 1868 | \verb\finally\ clause is executed, and then the saved exception is |
| 1869 | re-raised. If the \verb\finally\ clause raises another exception or |
| 1870 | executes a \verb\return\, \verb\break\ or \verb\continue\ statement, |
| 1871 | the saved exception is lost. |
| 1872 | |
| 1873 | When a \verb\return\ or \verb\break\ statement is executed in the |
| 1874 | \verb\try suite of a \verb\try...finally\ statement, the |
| 1875 | \verb\finally\ clause is also executed `on the way out'. A |
| 1876 | \verb\continue\ statement is illegal in the \verb\try\ clause (the |
| 1877 | reason is a problem with the current implementation -- this |
| 1878 | restriction may be lifted in the future). |
| 1879 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1880 | \section{Function definitions} \label{function} |
Guido van Rossum | 255ad6e | 1992-01-28 18:10:46 +0000 | [diff] [blame] | 1881 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1882 | XXX |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1883 | |
| 1884 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1885 | funcdef: "def" identifier "(" [parameter_list] ")" ":" suite |
| 1886 | parameter_list: parameter ("," parameter)* |
| 1887 | parameter: identifier | "(" parameter_list ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1888 | \end{verbatim} |
| 1889 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1890 | XXX |
| 1891 | |
| 1892 | \section{Class definitions} \label{class} |
| 1893 | |
| 1894 | XXX |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1895 | |
| 1896 | \begin{verbatim} |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1897 | classdef: "class" identifier [inheritance] ":" suite |
| 1898 | inheritance: "(" expression ("," expression)* ")" |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1899 | \end{verbatim} |
| 1900 | |
Guido van Rossum | 862c6f1 | 1992-01-29 14:47:05 +0000 | [diff] [blame] | 1901 | XXX |
| 1902 | |
| 1903 | \section{P.M.} |
| 1904 | |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1905 | XXX Syntax for scripts, modules |
| 1906 | XXX Syntax for interactive input, eval, exec, input |
Guido van Rossum | 743d1e7 | 1992-01-07 16:43:53 +0000 | [diff] [blame] | 1907 | XXX New definition of expressions (as conditions) |
Guido van Rossum | f2612d1 | 1991-11-21 13:53:03 +0000 | [diff] [blame] | 1908 | |
| 1909 | \end{document} |