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