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