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