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