blob: 8cfda04b911d4a3e20d5b0ef06be1b4553263717 [file] [log] [blame]
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001\documentstyle[11pt,myformat]{report}
Guido van Rossumf2612d11991-11-21 13:53:03 +00002
Guido van Rossum862c6f11992-01-29 14:47:05 +00003\title{\bf Python Reference Manual}
4
Guido van Rossumf2612d11991-11-21 13:53:03 +00005\author{
6 Guido van Rossum \\
7 Dept. CST, CWI, Kruislaan 413 \\
8 1098 SJ Amsterdam, The Netherlands \\
9 E-mail: {\tt guido@cwi.nl}
10}
11
Guido van Rossumb5e1c181992-03-06 10:52:59 +000012% Tell \index to actually write the .idx file
13\makeindex
14
Guido van Rossumf2612d11991-11-21 13:53:03 +000015\begin{document}
16
17\pagenumbering{roman}
18
19\maketitle
20
21\begin{abstract}
22
23\noindent
Guido van Rossum0f1f9da1992-01-20 17:10:21 +000024Python is a simple, yet powerful, interpreted programming language
25that bridges the gap between C and shell programming, and is thus
26ideally suited for ``throw-away programming'' and rapid prototyping.
27Its syntax is put together from constructs borrowed from a variety of
28other languages; most prominent are influences from ABC, C, Modula-3
29and Icon.
Guido van Rossumf2612d11991-11-21 13:53:03 +000030
31The Python interpreter is easily extended with new functions and data
32types implemented in C. Python is also suitable as an extension
33language for highly customizable C applications such as editors or
34window managers.
35
36Python is available for various operating systems, amongst which
37several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
38and MS-DOS.
39
40This reference manual describes the syntax and ``core semantics'' of
Guido van Rossum0f1f9da1992-01-20 17:10:21 +000041the language. It is terse, but attempts to be exact and complete.
42The semantics of non-essential built-in object types and of the
43built-in functions and modules are described in the {\em Python
44Library Reference}. For an informal introduction to the language, see
45the {\em Python Tutorial}.
Guido van Rossumf2612d11991-11-21 13:53:03 +000046
47\end{abstract}
48
49\pagebreak
50
Guido van Rossum670e5a01992-01-17 14:03:20 +000051{
52\parskip = 0mm
Guido van Rossumf2612d11991-11-21 13:53:03 +000053\tableofcontents
Guido van Rossum670e5a01992-01-17 14:03:20 +000054}
Guido van Rossumf2612d11991-11-21 13:53:03 +000055
56\pagebreak
57
58\pagenumbering{arabic}
59
60\chapter{Introduction}
61
62This reference manual describes the Python programming language.
63It is not intended as a tutorial.
64
Guido van Rossum743d1e71992-01-07 16:43:53 +000065While I am trying to be as precise as possible, I chose to use English
66rather than formal specifications for everything except syntax and
67lexical analysis. This should make the document better understandable
68to the average reader, but will leave room for ambiguities.
69Consequently, if you were coming from Mars and tried to re-implement
Guido van Rossum7b632a61992-01-16 17:49:21 +000070Python from this document alone, you might have to guess things and in
Guido van Rossumb5e1c181992-03-06 10:52:59 +000071fact you would probably end up implementing quite a different language.
Guido van Rossum7b632a61992-01-16 17:49:21 +000072On the other hand, if you are using
Guido van Rossum743d1e71992-01-07 16:43:53 +000073Python and wonder what the precise rules about a particular area of
Guido van Rossumb5e1c181992-03-06 10:52:59 +000074the language are, you should definitely be able to find them here.
Guido van Rossum743d1e71992-01-07 16:43:53 +000075
76It is dangerous to add too many implementation details to a language
Guido van Rossumb5e1c181992-03-06 10:52:59 +000077reference document --- the implementation may change, and other
Guido van Rossum743d1e71992-01-07 16:43:53 +000078implementations of the same language may work differently. On the
79other hand, there is currently only one Python implementation, and
Guido van Rossum7b632a61992-01-16 17:49:21 +000080its particular quirks are sometimes worth being mentioned, especially
Guido van Rossumb5e1c181992-03-06 10:52:59 +000081where the implementation imposes additional limitations. Therefore,
82you'll find short ``implementation notes'' sprinkled throughout the
83text.
Guido van Rossum743d1e71992-01-07 16:43:53 +000084
85Every Python implementation comes with a number of built-in and
86standard modules. These are not documented here, but in the separate
87{\em Python Library Reference} document. A few built-in modules are
88mentioned when they interact in a significant way with the language
89definition.
90
91\section{Notation}
92
93The descriptions of lexical analysis and syntax use a modified BNF
94grammar notation. This uses the following style of definition:
Guido van Rossumb5e1c181992-03-06 10:52:59 +000095\index{BNF}
96\index{grammar}
97\index{syntax}
98\index{notation}
Guido van Rossum743d1e71992-01-07 16:43:53 +000099
100\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000101name: lc_letter (lc_letter | "_")*
102lc_letter: "a"..."z"
Guido van Rossum743d1e71992-01-07 16:43:53 +0000103\end{verbatim}
104
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000105The first line says that a \verb\name\ is an \verb\lc_letter\ followed by
106a sequence of zero or more \verb\lc_letter\s and underscores. An
107\verb\lc_letter\ in turn is any of the single characters `a' through `z'.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000108(This rule is actually adhered to for the names defined in lexical and
Guido van Rossum743d1e71992-01-07 16:43:53 +0000109grammar rules in this document.)
110
111Each rule begins with a name (which is the name defined by the rule)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000112and a colon. A vertical bar
Guido van Rossum7b632a61992-01-16 17:49:21 +0000113(\verb\|\) is used to separate alternatives; it is the least binding
114operator in this notation. A star (\verb\*\) means zero or more
115repetitions of the preceding item; likewise, a plus (\verb\+\) means
116one or more repetitions, and a question mark (\verb\?\) zero or one
117(in other words, the preceding item is optional). These three
118operators bind as tightly as possible; parentheses are used for
Guido van Rossum743d1e71992-01-07 16:43:53 +0000119grouping. Literal strings are enclosed in double quotes. White space
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000120is only meaningful to separate tokens. Rules are normally contained
121on a single line; rules with many alternatives may be formatted
122alternatively with each line after the first beginning with a
123vertical bar.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000124
125In lexical definitions (as the example above), two more conventions
126are used: Two literal characters separated by three dots mean a choice
127of any single character in the given (inclusive) range of ASCII
128characters. A phrase between angular brackets (\verb\<...>\) gives an
129informal description of the symbol defined; e.g., this could be used
130to describe the notion of `control character' if needed.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000131\index{lexical definitions}
132\index{ASCII}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000133
Guido van Rossum7b632a61992-01-16 17:49:21 +0000134Even though the notation used is almost the same, there is a big
Guido van Rossum743d1e71992-01-07 16:43:53 +0000135difference between the meaning of lexical and syntactic definitions:
136a lexical definition operates on the individual characters of the
137input source, while a syntax definition operates on the stream of
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000138tokens generated by the lexical analysis. All uses of BNF in the next
139chapter (``Lexical Analysis'') are lexical definitions; uses in
140subsequenc chapter are syntactic definitions.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000141
Guido van Rossumf2612d11991-11-21 13:53:03 +0000142\chapter{Lexical analysis}
143
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000144A Python program is read by a {\em parser}. Input to the parser is a
145stream of {\em tokens}, generated by the {\em lexical analyzer}. This
146chapter describes how the lexical analyzer breaks a file into tokens.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000147\index{lexical analysis}
148\index{parser}
149\index{token}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000150
151\section{Line structure}
152
Guido van Rossum7b632a61992-01-16 17:49:21 +0000153A Python program is divided in a number of logical lines. The end of
154a logical line is represented by the token NEWLINE. Statements cannot
155cross logical line boundaries except where NEWLINE is allowed by the
156syntax (e.g., between statements in compound statements).
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000157\index{line structure}
158\index{logical line}
159\index{NEWLINE token}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000160
161\subsection{Comments}
162
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000163A comment starts with a hash character (\verb\#\) that is not part of
Guido van Rossum7b632a61992-01-16 17:49:21 +0000164a string literal, and ends at the end of the physical line. A comment
165always signifies the end of the logical line. Comments are ignored by
166the syntax.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000167\index{comment}
168\index{logical line}
169\index{physical line}
170\index{hash character}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000171
172\subsection{Line joining}
173
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000174Two or more physical lines may be joined into logical lines using
Guido van Rossum7b632a61992-01-16 17:49:21 +0000175backslash characters (\verb/\/), as follows: when a physical line ends
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000176in a backslash that is not part of a string literal or comment, it is
177joined with the following forming a single logical line, deleting the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000178backslash and the following end-of-line character. For example:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000179\index{physical line}
180\index{line joining}
181\index{backslash character}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000182%
183\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000184moth_names = ['Januari', 'Februari', 'Maart', \
185 'April', 'Mei', 'Juni', \
186 'Juli', 'Augustus', 'September', \
187 'Oktober', 'November', 'December']
Guido van Rossum670e5a01992-01-17 14:03:20 +0000188\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000189
190\subsection{Blank lines}
191
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000192A logical line that contains only spaces, tabs, and possibly a
193comment, is ignored (i.e., no NEWLINE token is generated), except that
194during interactive input of statements, an entirely blank logical line
195terminates a multi-line statement.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000196\index{blank line}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000197
198\subsection{Indentation}
199
Guido van Rossum7b632a61992-01-16 17:49:21 +0000200Leading whitespace (spaces and tabs) at the beginning of a logical
201line is used to compute the indentation level of the line, which in
202turn is used to determine the grouping of statements.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000203\index{indentation}
204\index{whitespace}
205\index{leading whitespace}
206\index{space}
207\index{tab}
208\index{grouping}
209\index{statement grouping}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000210
Guido van Rossum7b632a61992-01-16 17:49:21 +0000211First, tabs are replaced (from left to right) by one to eight spaces
212such that the total number of characters up to there is a multiple of
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000213eight (this is intended to be the same rule as used by {\UNIX}). The
Guido van Rossum7b632a61992-01-16 17:49:21 +0000214total number of spaces preceding the first non-blank character then
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000215determines the line's indentation. Indentation cannot be split over
216multiple physical lines using backslashes.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000217
218The indentation levels of consecutive lines are used to generate
219INDENT and DEDENT tokens, using a stack, as follows.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000220\index{INDENT token}
221\index{DEDENT token}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000222
223Before the first line of the file is read, a single zero is pushed on
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000224the stack; this will never be popped off again. The numbers pushed on
225the stack will always be strictly increasing from bottom to top. At
226the beginning of each logical line, the line's indentation level is
227compared to the top of the stack. If it is equal, nothing happens.
228If it larger, it is pushed on the stack, and one INDENT token is
229generated. If it is smaller, it {\em must} be one of the numbers
230occurring on the stack; all numbers on the stack that are larger are
231popped off, and for each number popped off a DEDENT token is
232generated. At the end of the file, a DEDENT token is generated for
233each number remaining on the stack that is larger than zero.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000234
Guido van Rossum7b632a61992-01-16 17:49:21 +0000235Here is an example of a correctly (though confusingly) indented piece
236of Python code:
237
238\begin{verbatim}
239def perm(l):
Guido van Rossum670e5a01992-01-17 14:03:20 +0000240 # Compute the list of all permutations of l
241
Guido van Rossum7b632a61992-01-16 17:49:21 +0000242 if len(l) <= 1:
243 return [l]
244 r = []
245 for i in range(len(l)):
246 s = l[:i] + l[i+1:]
247 p = perm(s)
248 for x in p:
249 r.append(l[i:i+1] + x)
250 return r
251\end{verbatim}
252
253The following example shows various indentation errors:
254
255\begin{verbatim}
256 def perm(l): # error: first line indented
257 for i in range(len(l)): # error: not indented
258 s = l[:i] + l[i+1:]
259 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
260 for x in p:
261 r.append(l[i:i+1] + x)
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000262 return r # error: inconsistent dedent
Guido van Rossum7b632a61992-01-16 17:49:21 +0000263\end{verbatim}
264
265(Actually, the first three errors are detected by the parser; only the
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000266last error is found by the lexical analyzer --- the indentation of
Guido van Rossum7b632a61992-01-16 17:49:21 +0000267\verb\return r\ does not match a level popped off the stack.)
268
Guido van Rossumf2612d11991-11-21 13:53:03 +0000269\section{Other tokens}
270
271Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
272exist: identifiers, keywords, literals, operators, and delimiters.
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000273Spaces and tabs are not tokens, but serve to delimit tokens. Where
274ambiguity exists, a token comprises the longest possible string that
275forms a legal token, when read from left to right.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000276
Guido van Rossumf2612d11991-11-21 13:53:03 +0000277\section{Identifiers}
278
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000279Identifiers (also referred to as names) are described by the following
280lexical definitions:
281\index{identifier}
282\index{name}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000283
284\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000285identifier: (letter|"_") (letter|digit|"_")*
Guido van Rossumf2612d11991-11-21 13:53:03 +0000286letter: lowercase | uppercase
Guido van Rossum743d1e71992-01-07 16:43:53 +0000287lowercase: "a"..."z"
288uppercase: "A"..."Z"
289digit: "0"..."9"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000290\end{verbatim}
291
Guido van Rossum670e5a01992-01-17 14:03:20 +0000292Identifiers are unlimited in length. Case is significant.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000293
Guido van Rossum670e5a01992-01-17 14:03:20 +0000294\subsection{Keywords}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000295
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000296The following identifiers are used as reserved words, or {\em
Guido van Rossum7b632a61992-01-16 17:49:21 +0000297keywords} of the language, and cannot be used as ordinary
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000298identifiers. They must be spelled exactly as written here:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000299\index{keyword}
300\index{reserved word}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000301
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000302\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000303and del for in print
304break elif from is raise
305class else global not return
306continue except if or try
307def finally import pass while
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000308\end{verbatim}
309
Guido van Rossum743d1e71992-01-07 16:43:53 +0000310% # This Python program sorts and formats the above table
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000311% import string
312% l = []
313% try:
314% while 1:
315% l = l + string.split(raw_input())
316% except EOFError:
317% pass
318% l.sort()
319% for i in range((len(l)+4)/5):
320% for j in range(i, len(l), 5):
321% print string.ljust(l[j], 10),
322% print
Guido van Rossumf2612d11991-11-21 13:53:03 +0000323
324\section{Literals}
325
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000326Literals are notations for constant values of some built-in types.
327\index{literal}
328\index{constant}
329
Guido van Rossumf2612d11991-11-21 13:53:03 +0000330\subsection{String literals}
331
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000332String literals are described by the following lexical definitions:
333\index{string literal}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000334
335\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000336stringliteral: "'" stringitem* "'"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000337stringitem: stringchar | escapeseq
Guido van Rossum743d1e71992-01-07 16:43:53 +0000338stringchar: <any ASCII character except newline or "\" or "'">
339escapeseq: "'" <any ASCII character except newline>
Guido van Rossumf2612d11991-11-21 13:53:03 +0000340\end{verbatim}
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000341\index{ASCII}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000342
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000343String literals cannot span physical line boundaries. Escape
344sequences in strings are actually interpreted according to rules
345simular to those used by Standard C. The recognized escape sequences
346are:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000347\index{physical line}
348\index{escape sequence}
349\index{Standard C}
350\index{C}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000351
352\begin{center}
353\begin{tabular}{|l|l|}
354\hline
355\verb/\\/ & Backslash (\verb/\/) \\
356\verb/\'/ & Single quote (\verb/'/) \\
357\verb/\a/ & ASCII Bell (BEL) \\
358\verb/\b/ & ASCII Backspace (BS) \\
Guido van Rossum7b632a61992-01-16 17:49:21 +0000359%\verb/\E/ & ASCII Escape (ESC) \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000360\verb/\f/ & ASCII Formfeed (FF) \\
361\verb/\n/ & ASCII Linefeed (LF) \\
362\verb/\r/ & ASCII Carriage Return (CR) \\
363\verb/\t/ & ASCII Horizontal Tab (TAB) \\
364\verb/\v/ & ASCII Vertical Tab (VT) \\
365\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000366\verb/\x/{\em xx...} & ASCII character with hex value {\em xx...} \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000367\hline
368\end{tabular}
369\end{center}
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000370\index{ASCII}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000371
Guido van Rossum7b632a61992-01-16 17:49:21 +0000372In strict compatibility with in Standard C, up to three octal digits are
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000373accepted, but an unlimited number of hex digits is taken to be part of
374the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum7b632a61992-01-16 17:49:21 +0000375are used in all current implementations...).
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000376
Guido van Rossum7b632a61992-01-16 17:49:21 +0000377All unrecognized escape sequences are left in the string unchanged,
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000378i.e., {\em the backslash is left in the string.} (This behavior is
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000379useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum743d1e71992-01-07 16:43:53 +0000380resulting output is more easily recognized as broken. It also helps a
381great deal for string literals used as regular expressions or
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000382otherwise passed to other modules that do their own escape handling.)
383\index{unrecognized escape sequence}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000384
385\subsection{Numeric literals}
386
Guido van Rossum670e5a01992-01-17 14:03:20 +0000387There are three types of numeric literals: plain integers, long
388integers, and floating point numbers.
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000389\index{number}
390\index{numeric literal}
391\index{integer literal}
392\index{plain integer literal}
393\index{long integer literal}
394\index{floating point literal}
395\index{hexadecimal literal}
396\index{octal literal}
397\index{decimal literal}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000398
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000399Integer and long integer literals are described by the following
400lexical definitions:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000401
402\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000403longinteger: integer ("l"|"L")
Guido van Rossumf2612d11991-11-21 13:53:03 +0000404integer: decimalinteger | octinteger | hexinteger
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000405decimalinteger: nonzerodigit digit* | "0"
406octinteger: "0" octdigit+
407hexinteger: "0" ("x"|"X") hexdigit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000408
Guido van Rossum743d1e71992-01-07 16:43:53 +0000409nonzerodigit: "1"..."9"
410octdigit: "0"..."7"
411hexdigit: digit|"a"..."f"|"A"..."F"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000412\end{verbatim}
413
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000414Although both lower case `l' and upper case `L' are allowed as suffix
Guido van Rossum670e5a01992-01-17 14:03:20 +0000415for long integers, it is strongly recommended to always use `L', since
416the letter `l' looks too much like the digit `1'.
417
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000418Plain integer decimal literals must be at most $2^{31} - 1$ (i.e., the
Guido van Rossumcb9d66d1992-03-20 14:59:04 +0000419largest positive integer, assuming 32-bit arithmetic). Plain octal and
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000420hexadecimal literals may be as large as $2^{32} - 1$, but values
Guido van Rossumcb9d66d1992-03-20 14:59:04 +0000421larger than $2^{31} - 1$ are converted to a negative value by
422subtracting $2^{32}$. There is no limit for long integer literals.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000423
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000424Some examples of plain and long integer literals:
Guido van Rossum670e5a01992-01-17 14:03:20 +0000425
426\begin{verbatim}
4277 2147483647 0177 0x80000000
Guido van Rossumb5e1c181992-03-06 10:52:59 +00004283L 79228162514264337593543950336L 0377L 0x100000000L
Guido van Rossum670e5a01992-01-17 14:03:20 +0000429\end{verbatim}
430
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000431Floating point literals are described by the following lexical
432definitions:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000433
434\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000435floatnumber: pointfloat | exponentfloat
436pointfloat: [intpart] fraction | intpart "."
437exponentfloat: (intpart | pointfloat) exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000438intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000439fraction: "." digit+
440exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000441\end{verbatim}
442
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000443The allowed range of floating point literals is
444implementation-dependent.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000445
446Some examples of floating point literals:
Guido van Rossum7b632a61992-01-16 17:49:21 +0000447
448\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00004493.14 10. .001 1e100 3.14e-10
Guido van Rossum7b632a61992-01-16 17:49:21 +0000450\end{verbatim}
451
Guido van Rossum670e5a01992-01-17 14:03:20 +0000452Note that numeric literals do not include a sign; a phrase like
453\verb\-1\ is actually an expression composed of the operator
Guido van Rossum7b632a61992-01-16 17:49:21 +0000454\verb\-\ and the literal \verb\1\.
455
Guido van Rossumf2612d11991-11-21 13:53:03 +0000456\section{Operators}
457
458The following tokens are operators:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000459\index{operators}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000460
461\begin{verbatim}
462+ - * / %
463<< >> & | ^ ~
Guido van Rossum743d1e71992-01-07 16:43:53 +0000464< == > <= <> != >=
Guido van Rossumf2612d11991-11-21 13:53:03 +0000465\end{verbatim}
466
Guido van Rossum743d1e71992-01-07 16:43:53 +0000467The comparison operators \verb\<>\ and \verb\!=\ are alternate
468spellings of the same operator.
469
Guido van Rossumf2612d11991-11-21 13:53:03 +0000470\section{Delimiters}
471
Guido van Rossum743d1e71992-01-07 16:43:53 +0000472The following tokens serve as delimiters or otherwise have a special
473meaning:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000474\index{delimiters}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000475
476\begin{verbatim}
477( ) [ ] { }
Guido van Rossum743d1e71992-01-07 16:43:53 +0000478; , : . ` =
Guido van Rossumf2612d11991-11-21 13:53:03 +0000479\end{verbatim}
480
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000481The following printing ASCII characters are not used in Python. Their
482occurrence outside string literals and comments is an unconditional
483error:
484\index{ASCII}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000485
486\begin{verbatim}
487! @ $ " ?
488\end{verbatim}
489
Guido van Rossum7b632a61992-01-16 17:49:21 +0000490They may be used by future versions of the language though!
491
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000492\chapter{Data model}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000493
Guido van Rossum743d1e71992-01-07 16:43:53 +0000494\section{Objects, values and types}
495
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000496{\em Objects} are Python's abstraction for data. All data in a Python
497program is represented by objects or by relations between objects.
498(In a sense, and in conformance to Von Neumann's model of a
499``stored program computer'', code is also represented by objects.)
500\index{object}
501\index{data}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000502
503Every object has an identity, a type and a value. An object's {\em
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000504identity} never changes once it has been created; you may think of it
505as the object's address in memory. An object's {\em type} is also
506unchangeable. It determines the operations that an object supports
507(e.g., ``does it have a length?'') and also defines the possible
508values for objects of that type. The {\em value} of some objects can
509change. Objects whose value can change are said to be {\em mutable};
510objects whose value is unchangeable once they are created are called
511{\em immutable}. The type determines an object's (im)mutability.
512\index{identity of an object}
513\index{value of an object}
514\index{type of an object}
515\index{mutable object}
516\index{immutable object}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000517
518Objects are never explicitly destroyed; however, when they become
Guido van Rossum670e5a01992-01-17 14:03:20 +0000519unreachable they may be garbage-collected. An implementation is
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000520allowed to delay garbage collection or omit it altogether --- it is a
Guido van Rossum670e5a01992-01-17 14:03:20 +0000521matter of implementation quality how garbage collection is
522implemented, as long as no objects are collected that are still
523reachable. (Implementation note: the current implementation uses a
Guido van Rossum743d1e71992-01-07 16:43:53 +0000524reference-counting scheme which collects most objects as soon as they
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000525become unreachable, but never collects garbage containing circular
Guido van Rossum743d1e71992-01-07 16:43:53 +0000526references.)
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000527\index{garbage collection}
528\index{reference counting}
529\index{unreachable object}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000530
Guido van Rossum670e5a01992-01-17 14:03:20 +0000531Note that the use of the implementation's tracing or debugging
532facilities may keep objects alive that would normally be collectable.
533
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000534Some objects contain references to ``external'' resources such as open
535files or windows. It is understood that these resources are freed
536when the object is garbage-collected, but since garbage collection is
537not guaranteed to happen, such objects also provide an explicit way to
538release the external resource, usually a \verb\close\ method.
539Programs are strongly recommended to always explicitly close such
540objects.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000541
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000542Some objects contain references to other objects; these are called
543{\em containers}. Examples of containers are tuples, lists and
544dictionaries. The references are part of a container's value. In
545most cases, when we talk about the value of a container, we imply the
546values, not the identities of the contained objects; however, when we
547talk about the (im)mutability of a container, only the identities of
548the immediately contained objects are implied. (So, if an immutable
549container contains a reference to a mutable object, its value changes
550if that mutable object is changed.)
551\index{container}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000552
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000553Types affect almost all aspects of objects' lives. Even the meaning
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000554of object identity is affected in some sense: for immutable types,
555operations that compute new values may actually return a reference to
556any existing object with the same type and value, while for mutable
557objects this is not allowed. E.g., after
Guido van Rossum743d1e71992-01-07 16:43:53 +0000558
559\begin{verbatim}
560a = 1; b = 1; c = []; d = []
561\end{verbatim}
562
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000563\verb\a\ and \verb\b\ may or may not refer to the same object with the
564value one, depending on the implementation, but \verb\c\ and \verb\d\
565are guaranteed to refer to two different, unique, newly created empty
566lists.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000567
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000568\section{The standard type hierarchy}
569
570Below is a list of the types that are built into Python. Extension
571modules written in C can define additional types. Future versions of
572Python may add types to the type hierarchy (e.g., rational or complex
Guido van Rossumcb9d66d1992-03-20 14:59:04 +0000573numbers, efficiently stored arrays of integers, etc.).
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000574\index{type}
575\index{type hierarchy}
576\index{extension module}
577\index{C}
578
579Some of the type descriptions below contain a paragraph listing
580`special attributes'. These are attributes that provide access to the
581implementation and are not intended for general use. Their definition
582may change in the future. There are also some `generic' special
583attributes, not listed with the individual objects: \verb\__methods__\
584is a list of the method names of a built-in object, if it has any;
585\verb\__members__\ is a list of the data attribute names of a built-in
586object, if it has any.
587\index{attribute}
588\index{special attribute}
589\index{generic special attribute}
590\ttindex{__methods__}
591\ttindex{__members__}
592
593\begin{description}
594
595\item[None]
596This type has a single value. There is a single object with this value.
597This object is accessed through the built-in name \verb\None\.
598It is returned from functions that don't explicitly return an object.
599\ttindex{None}
600
601\item[Numbers]
602These are created by numeric literals and returned as results
603by arithmetic operators and arithmetic built-in functions.
604Numeric objects are immutable; once created their value never changes.
605Python numbers are of course strongly related to mathematical numbers,
606but subject to the limitations of numerical representation in computers.
607\index{number}
608
609Python distinguishes between integers and floating point numbers:
610
611\begin{description}
612\item[Integers]
613These represent elements from the mathematical set of whole numbers.
614\index{integer}
615
616There are two types of integers:
617
618\begin{description}
619
620\item[Plain integers]
621These represent numbers in the range $-2^{31}$ through $2^{31}-1$.
622(The range may be larger on machines with a larger natural word
623size, but not smaller.)
624When the result of an operation falls outside this range, the
625exception \verb\OverflowError\ is raised.
626For the purpose of shift and mask operations, integers are assumed to
627have a binary, 2's complement notation using 32 or more bits, and
628hiding no bits from the user (i.e., all $2^{32}$ different bit
629patterns correspond to different values).
630\index{plain integer}
631
632\item[Long integers]
633These represent numbers in an unlimited range, subject to avaiable
634(virtual) memory only. For the purpose of shift and mask operations,
635a binary representation is assumed, and negative numbers are
636represented in a variant of 2's complement which gives the illusion of
637an infinite string of sign bits extending to the left.
638\index{long integer}
639
640\end{description} % Integers
641
642The rules for integer representation are intended to give the most
643meaningful interpretation of shift and mask operations involving
644negative integers and the least surprises when switching between the
645plain and long integer domains. For any operation except left shift,
646if it yields a result in the plain integer domain without causing
647overflow, it will yield the same result in the long integer domain or
648when using mixed operands.
649\index{integer representation}
650
651\item[Floating point numbers]
652These represent machine-level double precision floating point numbers.
653You are at the mercy of the underlying machine architecture and
654C implementation for the accepted range and handling of overflow.
655\index{floating point number}
656\index{C}
657
658\end{description} % Numbers
659
660\item[Sequences]
661These represent finite ordered sets indexed by natural numbers.
662The built-in function \verb\len()\ returns the number of elements
663of a sequence. When this number is $n$, the index set contains
664the numbers $0, 1, \ldots, n-1$. Element \verb\i\ of sequence
665\verb\a\ is selected by \verb\a[i]\.
666\index{seqence}
667\bifuncindex{len}
668\index{index operation}
669\index{item selection}
670\index{subscription}
671
672Sequences also support slicing: \verb\a[i:j]\ selects all elements
673with index $k$ such that $i < k < j$. When used as an expression,
674a slice is a sequence of the same type --- this implies that the
675index set is renumbered so that it starts at 0 again.
676\index{slicing}
677
678Sequences are distinguished according to their mutability:
679
680\begin{description}
681%
682\item[Immutable sequences]
683An object of an immutable sequence type cannot change once it is
684created. (If the object contains references to other objects,
685these other objects may be mutable and may be changed; however
686the collection of objects directly referenced by an immutable object
687cannot change.)
688\index{immutable sequence}
689
690The following types are immutable sequences:
691
692\begin{description}
693
694\item[Strings]
695The elements of a string are characters. There is no separate
696character type; a character is represented by a string of one element.
697Characters represent (at least) 8-bit bytes. The built-in
698functions \verb\chr()\ and \verb\ord()\ convert between characters
699and nonnegative integers representing the byte values.
700Bytes with the values 0-127 represent the corresponding ASCII values.
701The string data type is also used to represent arrays of bytes, e.g.,
702to hold data read from a file.
703\index{string}
704\index{character}
705\index{byte}
706\index{ASCII}
707\bifuncindex{chr}
708\bifuncindex{ord}
709
710(On systems whose native character set is not ASCII, strings may use
711EBCDIC in their internal representation, provided the functions
712\verb\chr()\ and \verb\ord()\ implement a mapping between ASCII and
713EBCDIC, and string comparison preserves the ASCII order.
714Or perhaps someone can propose a better rule?)
715\index{ASCII}
716\index{EBCDIC}
717\index{character set}
718\index{string comparison}
719\bifuncindex{chr}
720\bifuncindex{ord}
721
722\item[Tuples]
723The elements of a tuple are arbitrary Python objects.
724Tuples of two or more elements are formed by comma-separated lists
725of expressions. A tuple of one element (a `singleton') can be formed
726by affixing a comma to an expression (an expression by itself does
727not create a tuple, since parentheses must be usable for grouping of
728expressions). An empty tuple can be formed by enclosing `nothing' in
729parentheses.
730\index{tuple}
731\index{singleton tuple}
732\index{empty tuple}
733
734\end{description} % Immutable sequences
735
736\item[Mutable sequences]
737Mutable sequences can be changed after they are created. The
738subscription and slicing notations can be used as the target of
739assignment and \verb\del\ (delete) statements.
740\index{mutable sequece}
741\index{assignment statement}
742\kwindex{del}
743\index{subscription}
744\index{slicing}
745
746There is currently a single mutable sequence type:
747
748\begin{description}
749
750\item[Lists]
751The elements of a list are arbitrary Python objects. Lists are formed
752by placing a comma-separated list of expressions in square brackets.
753(Note that there are no special cases needed to form lists of length 0
754or 1.)
755\index{list}
756
757\end{description} % Mutable sequences
758
759\end{description} % Sequences
760
761\item[Mapping types]
762These represent finite sets of objects indexed by arbitrary index sets.
763The subscript notation \verb\a[k]\ selects the element indexed
764by \verb\k\ from the mapping \verb\a\; this can be used in
765expressions and as the target of assignments or \verb\del\ statements.
766The built-in function \verb\len()\ returns the number of elements
767in a mapping.
768\bifuncindex{len}
769\index{subscription}
770\index{mapping}
771
772There is currently a single mapping type:
773
774\begin{description}
775
776\item[Dictionaries]
777These represent finite sets of objects indexed by strings.
778Dictionaries are created by the \verb\{...}\ notation (see section
779\ref{dict}). (Implementation note: the strings used for indexing must
780not contain null bytes.)
781\index{dictionary}
782
783\end{description} % Mapping types
784
785\item[Callable types]
786These are the types to which the function call (invocation) operation,
787written as \verb\function(argument, argument, ...)\, can be applied:
788\index{callable type}
789\indexii{function}{call}
790\index{invocation}
791
792\begin{description}
793
794\item[User-defined functions]
795A user-defined function object is created by a function definition
796(see section \ref{function}). It should be called with an argument
797list containing the same number of items as the function's formal
798parameter list.
799\indexii{user-defined}{function}
800\index{function object}
801
802Special read-only attributes: \verb\func_code\ is the code object
803representing the compiled function body, and \verb\func_globals\ is (a
804reference to) the dictionary that holds the function's global
805variables --- it implements the global name space of the module in
806which the function was defined.
807\ttindex{func_code}
808\ttindex{func_globals}
809\indexii{global}{name space}
810
811\item[User-defined methods]
812A user-defined method (a.k.a. {\tt object closure}) is a pair of a
813class instance object and a user-defined function. It should be
814called with an argument list containing one item less than the number
815of items in the function's formal parameter list. When called, the
816class instance becomes the first argument, and the call arguments are
817shifted one to the right.
818\indexii{object}{closure}
819indexii{user-defined}{method}
820
821Special read-only attributes: \verb\im_self\ is the class instance
822object, \verb\im_func\ is the function object.
823\ttindex{im_func}
824\ttindex{im_self}
825
826\item[Built-in functions]
827A built-in function object is a wrapper around a C function. Examples
828of built-in functions are \verb\len\ and \verb\math.sin\. There
829are no special attributes. The number and type of the arguments are
830determined by the C function.
831\index{C}
832
833\item[Built-in methods]
834This is really a different disguise of a built-in function, this time
835containing an object passed to the C function as an implicit extra
836argument. An example of a built-in method is \verb\list.append\ if
837\verb\list\ is a list object.
838\indexii{built-in}{method}
839
840\item[Classes]
841Class objects are described below. When a class object is called as a
842parameterless function, a new class instance (also described below) is
843created and returned. The class's initialization function is not
844called --- this is the responsibility of the caller. It is illegal to
845call a class object with one or more arguments.
846\index{class}
847
848\end{description}
849
850\item[Modules]
851Modules are imported by the \verb\import\ statement (see section
852\ref{import}). A module object is a container for a module's name
853space, which is a dictionary (the same dictionary as referenced by the
854\verb\func_globals\ attribute of functions defined in the module).
855Module attribute references are translated to lookups in this
856dictionary. A module object does not contain the code object used to
857initialize the module (since it isn't needed once the initialization
858is done).
859\stindex{import}
860\index{module}
861
862Attribute assignment update the module's name space dictionary.
863
864Special read-only attributes: \verb\__dict__\ yields the module's name
865space as a dictionary object; \verb\__name__\ yields the module's name
866as a string object.
867\ttindex{__dict__}
868\ttindex{__name__}
869
870\item[Classes]
871Class objects are created by class definitions (see section
872\ref{class}). A class is a container for a dictionary containing the
873class's name space. Class attribute references are translated to
874lookups in this dictionary. When an attribute name is not found
875there, the attribute search continues in the base classes. The search
876is depth-first, left-to-right in the order of their occurrence in the
877base class list.
878\index{class}
879\index{container}
880\index{dictionary}
881\indexii{class}{attribute}
882
883Class attribute assignments update the class's dictionary, never the
884dictionary of a base class.
885\indexiii{class}{attribute}{assignment}
886
887A class can be called as a parameterless function to yield a class
888instance (see above).
889
890Special read-only attributes: \verb\__dict__\ yields te dictionary
891containing the class's name space; \verb\__bases__\ yields a tuple
892(possibly empty or a singleton) containing the base classes, in the
893order of their occurrence in the base class list.
894\ttindex{__dict__}
895\ttindex{__bases__}
896
897\item[Class instances]
898A class instance is created by calling a class object as a
899parameterless function. A class instance has a dictionary in which
900attribute references are searched. When an attribute is not found
901there, and the instance's class has an attribute by that name, and
902that class attribute is a user-defined function (and in no other
903cases), the instance attribute reference yields a user-defined method
904object (see above) constructed from the instance and the function.
905\indexii{class}{instance}
906\indexii{class instance}{attribute}
907
908Attribute assignments update the instance's dictionary.
909\indexiii{class instance}{attribute}{assignment}
910
911Special read-only attributes: \verb\__dict__\ yields the attribute
912dictionary; \verb\__class__\ yields the instance's class.
913\ttindex{__dict__}
914\ttindex{__class__}
915
916\item[Files]
917A file object represents an open file. (It is a wrapper around a C
918{\tt stdio} file pointer.) File objects are created by the
919\verb\open()\ built-in function, and also by \verb\posix.popen()\ and
920the \verb\makefile\ method of socket objects. \verb\sys.stdin\,
921\verb\sys.stdout\ and \verb\sys.stderr\ are file objects corresponding
922the the interpreter's standard input, output and error streams.
923See the Python Library Reference for methods of file objects and other
924details.
925\index{file}
926\index{C}
927\index{stdio}
928\bifuncindex{open}
929\bifuncindex{popen}
930\bifuncindex{makefile}
931\ttindex{stdin}
932\ttindex{stdout}
933\ttindex{stderr}
934
935\item[Internal types]
936A few types used internally by the interpreter are exposed to the user.
937Their definition may change with future versions of the interpreter,
938but they are mentioned here for completeness.
939\index{internal type}
940
941\begin{description}
942
943\item[Code objects]
944Code objects represent executable code. The difference between a code
945object and a function object is that the function object contains an
946explicit reference to the function's context (the module in which it
947was defined) which a code object contains no context. There is no way
948to execute a bare code object.
949\index{code object}
950
951Special read-only attributes: \verb\co_code\ is a string representing
952the sequence of instructions; \verb\co_consts\ is a list of literals
953used by the code; \verb\co_names\ is a list of names (strings) used by
954the code; \verb\co_filename\ is the filename from which the code was
955compiled. (To find out the line numbers, you would have to decode the
956instructions; the standard library module \verb\dis\ contains an
957example of how to do this.)
958\ttindex{co_code}
959\ttindex{co_consts}
960\ttindex{co_names}
961\ttindex{co_filename}
962
963\item[Frame objects]
964Frame objects represent execution frames. They may occur in traceback
965objects (see below).
966\index{frame object}
967
968Special read-only attributes: \verb\f_back\ is to the previous
969stack frame (towards the caller), or \verb\None\ if this is the bottom
970stack frame; \verb\f_code\ is the code object being executed in this
971frame; \verb\f_globals\ is the dictionary used to look up global
972variables; \verb\f_locals\ is used for local variables;
973\verb\f_lineno\ gives the line number and \verb\f_lasti\ gives the
974precise instruction (this is an index into the instruction string of
975the code object).
976\ttindex{f_back}
977\ttindex{f_code}
978\ttindex{f_globals}
979\ttindex{f_locals}
980\ttindex{f_lineno}
981\ttindex{f_lasti}
982
983\item[Traceback objects]
984Traceback objects represent a stack trace of an exception. A
985traceback object is created when an exception occurs. When the search
986for an exception handler unwinds the execution stack, at each unwound
987level a traceback object is inserted in front of the current
988traceback. When an exception handler is entered, the stack trace is
989made available to the program as \verb\sys.exc_traceback\. When the
990program contains no suitable handler, the stack trace is written
991(nicely formatted) to the standard error stream; if the interpreter is
992interactive, it is also made available to the user as
993\verb\sys.last_traceback\.
994\index{traceback object}
995\indexii{stack}{trace}
996\index{exception handler}
997\index{execution stack}
998\ttindex{exc_traceback}
999\ttindex{last_traceback}
1000
1001Special read-only attributes: \verb\tb_next\ is the next level in the
1002stack trace (towards the frame where the exception occurred), or
1003\verb\None\ if there is no next level; \verb\tb_frame\ points to the
1004execution frame of the current level; \verb\tb_lineno\ gives the line
1005number where the exception occurred; \verb\tb_lasti\ indicates the
1006precise instruction. The line number and last instruction in the
1007traceback may differ from the line number of its frame object if the
1008exception occurred in a \verb\try\ statement with no matching
1009\verb\except\ clause or with a \verb\finally\ clause.
1010\ttindex{tb_next}
1011\ttindex{tb_frame}
1012\ttindex{tb_lineno}
1013\ttindex{tb_lasti}
1014\stindex{try}
1015
1016\end{description} % Internal types
1017
1018\end{description} % Types
1019
1020\chapter{Execution model}
1021
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001022\section{Code blocks, execution frames, and name spaces}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001023
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001024A {\em code block} is a piece of Python program text that can be
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001025executed as a unit, such as a module, a class definition or a function
1026body. Some code blocks (like modules) are executed only once, others
1027(like function bodies) may be executed many times. Code block may
1028textually contain other code blocks. Code blocks may invoke other
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001029code blocks (that may or may not be textually contained in them) as
1030part of their execution, e.g., by invoking (calling) a function.
1031\index{code block}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001032
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001033The following are code blocks: A module is a code block. A function
1034body is a code block. A class definition is a code block. Each
1035command typed interactively is a separate code block; a script file is
1036a code block. The string argument passed to the built-in functions
1037\verb\eval\ and \verb\exec\ are code blocks. And finally, the
1038expression read and evaluated by the built-in function \verb\input\ is
1039a code block.
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001040
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001041A code block is executed in an execution frame. An {\em execution
1042frame} contains some administrative information (used for debugging),
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001043determines where and how execution continues after the code block's
1044execution has completed, and (perhaps most importantly) defines two
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001045name spaces, the local and the global name space, that affect
1046execution of the code block.
1047\index{execution frame}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001048
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001049A {\em name space} is a mapping from names (identifiers) to objects.
1050A particular name space may be referenced by more than one execution
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001051frame, and from other places as well. Adding a name to a name space
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001052is called {\em binding} a name (to an object); changing the mapping of
1053a name is called {\em rebinding}; removing a name is {\em unbinding}.
1054Name spaces are functionally equivalent to dictionaries.
1055\index{name space}
1056\indexii{binding}{name}
1057\indexii{rebinding}{name}
1058\indexii{unbinding}{name}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001059
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001060The {\em local name space} of an execution frame determines the default
1061place where names are defined and searched. The {\em global name
1062space} determines the place where names listed in \verb\global\
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001063statements are defined and searched, and where names that are not
1064explicitly bound in the current code block are searched.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001065\indexii{local}{name space}
1066\indexii{global}{name space}
1067\stindex{global}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001068
1069Whether a name is local or global in a code block is determined by
1070static inspection of the source text for the code block: in the
1071absence of \verb\global\ statements, a name that is bound anywhere in
1072the code block is local in the entire code block; all other names are
1073considered global. The \verb\global\ statement forces global
1074interpretation of selected names throughout the code block. The
1075following constructs bind names: formal parameters, \verb\import\
1076statements, class and function definitions (these bind the class or
1077function name), and targets that are identifiers if occurring in an
1078assignment, \verb\for\ loop header, or \verb\except\ clause header.
1079(A target occurring in a \verb\del\ statement does not bind a name.)
1080
1081When a global name is not found in the global name space, it is
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001082searched in the list of ``built-in'' names (which is actually the
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001083global name space of the module \verb\builtin\). When a name is not
1084found at all, the \verb\NameError\ exception is raised.
1085
1086The following table lists the meaning of the local and global name
1087space for various types of code blocks. The name space for a
1088particular module is automatically created when the module is first
1089referenced.
1090
1091\begin{center}
1092\begin{tabular}{|l|l|l|l|}
1093\hline
1094Code block type & Global name space & Local name space & Notes \\
1095\hline
1096Module & n.s. for this module & same as global & \\
1097Script & n.s. for \verb\__main__\ & same as global & \\
1098Interactive command & n.s. for \verb\__main__\ & same as global & \\
1099Class definition & global n.s. of containing block & new n.s. & \\
1100Function body & global n.s. of containing block & new n.s. & \\
1101String passed to \verb\exec\ or \verb\eval\
1102 & global n.s. of caller & local n.s. of caller & (1) \\
1103File read by \verb\execfile\
1104 & global n.s. of caller & local n.s. of caller & (1) \\
1105Expression read by \verb\input\
1106 & global n.s. of caller & local n.s. of caller & \\
1107\hline
1108\end{tabular}
1109\end{center}
1110
1111Notes:
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001112
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001113\begin{description}
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001114
1115\item[n.s.] means {\em name space}
1116
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001117\item[(1)] The global and local name space for these functions can be
1118overridden with optional extra arguments.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001119
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001120\end{description}
1121
1122\section{Exceptions}
1123
1124Exceptions are a means of breaking out of the normal flow of control
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001125of a code block in order to handle errors or other exceptional
1126conditions. An exception is {\em raised} at the point where the error
1127is detected; it may be {\em handled} by the surrounding code block or
1128by any code block that directly or indirectly invoked the code block
1129where the error occurred.
1130\index{exception}
1131\index{raise an exception}
1132\index{handle an exception}
1133\index{exception handler}
1134\index{errors}
1135\index{error handling}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001136
1137The Python interpreter raises an exception when it detects an run-time
1138error (such as division by zero). A Python program can also
1139explicitly raise an exception with the \verb\raise\ statement.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001140Exception handlers are specified with the \verb\try...except\
1141statement.
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001142
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001143Python uses the ``termination'' model of error handling: an exception
1144handler can find out what happened and continue execution at an outer
1145level, but it cannot repair the cause of the error and retry the
1146failing operation (except by re-entering the the offending piece of
1147code from the top).
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001148
1149When an exception is not handled at all, the interpreter terminates
1150execution of the program, or returns to its interactive main loop.
1151
1152Exceptions are identified by string objects. Two different string
1153objects with the same value identify different exceptions.
1154
1155When an exception is raised, an object (maybe \verb\None\) is passed
1156as the exception's ``parameter''; this object does not affect the
1157selection of an exception handler, but is passed to the selected
1158exception handler as additional information.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001159
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001160See also the description of the \verb\try\ and \verb\raise\
1161statements.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001162
Guido van Rossumf2612d11991-11-21 13:53:03 +00001163\chapter{Expressions and conditions}
1164
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001165In this and the following chapters, extended BNF notation will be used
1166to describe syntax, not lexical analysis.
1167\index{BNF}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001168
1169This chapter explains the meaning of the elements of expressions and
1170conditions. Conditions are a superset of expressions, and a condition
Guido van Rossum670e5a01992-01-17 14:03:20 +00001171may be used wherever an expression is required by enclosing it in
1172parentheses. The only places where expressions are used in the syntax
1173instead of conditions is in expression statements and on the
1174right-hand side of assignments; this catches some nasty bugs like
1175accedentally writing \verb\x == 1\ instead of \verb\x = 1\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001176
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001177The comma plays several roles in Python's syntax. It is usually an
Guido van Rossum743d1e71992-01-07 16:43:53 +00001178operator with a lower precedence than all others, but occasionally
Guido van Rossum670e5a01992-01-17 14:03:20 +00001179serves other purposes as well; e.g., it separates function arguments,
1180is used in list and dictionary constructors, and has special semantics
1181in \verb\print\ statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001182
1183When (one alternative of) a syntax rule has the form
1184
1185\begin{verbatim}
1186name: othername
1187\end{verbatim}
1188
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001189and no semantics are given, the semantics of this form of \verb\name\
1190are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001191
1192\section{Arithmetic conversions}
1193
1194When a description of an arithmetic operator below uses the phrase
1195``the numeric arguments are converted to a common type'',
1196this both means that if either argument is not a number, a
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001197\verb\TypeError\ exception is raised, and that otherwise
Guido van Rossumf2612d11991-11-21 13:53:03 +00001198the following conversions are applied:
1199
1200\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001201\item first, if either argument is a floating point number,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001202 the other is converted to floating point;
1203\item else, if either argument is a long integer,
1204 the other is converted to long integer;
Guido van Rossum670e5a01992-01-17 14:03:20 +00001205\item otherwise, both must be plain integers and no conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +00001206 is necessary.
1207\end{itemize}
1208
Guido van Rossumf2612d11991-11-21 13:53:03 +00001209\section{Atoms}
1210
Guido van Rossum670e5a01992-01-17 14:03:20 +00001211Atoms are the most basic elements of expressions. Forms enclosed in
1212reverse quotes or in parentheses, brackets or braces are also
1213categorized syntactically as atoms. The syntax for atoms is:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001214
1215\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001216atom: identifier | literal | enclosure
1217enclosure: parenth_form | list_display | dict_display | string_conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +00001218\end{verbatim}
1219
1220\subsection{Identifiers (Names)}
1221
1222An identifier occurring as an atom is a reference to a local, global
Guido van Rossum670e5a01992-01-17 14:03:20 +00001223or built-in name binding. If a name can be assigned to anywhere in a
1224code block, and is not mentioned in a \verb\global\ statement in that
1225code block, it refers to a local name throughout that code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001226Otherwise, it refers to a global name if one exists, else to a
1227built-in name.
1228
Guido van Rossum670e5a01992-01-17 14:03:20 +00001229When the name is bound to an object, evaluation of the atom yields
1230that object. When a name is not bound, an attempt to evaluate it
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001231raises a \verb\NameError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001232
1233\subsection{Literals}
1234
Guido van Rossum670e5a01992-01-17 14:03:20 +00001235Python knows string and numeric literals:
1236
1237\begin{verbatim}
1238literal: stringliteral | integer | longinteger | floatnumber
1239\end{verbatim}
1240
Guido van Rossumf2612d11991-11-21 13:53:03 +00001241Evaluation of a literal yields an object of the given type
1242(string, integer, long integer, floating point number)
1243with the given value.
1244The value may be approximated in the case of floating point literals.
1245
Guido van Rossum670e5a01992-01-17 14:03:20 +00001246All literals correspond to immutable data types, and hence the
1247object's identity is less important than its value. Multiple
1248evaluations of literals with the same value (either the same
1249occurrence in the program text or a different occurrence) may obtain
1250the same object or a different object with the same value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001251
1252(In the original implementation, all literals in the same code block
1253with the same type and value yield the same object.)
1254
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001255\subsection{Parenthesized forms}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001256
Guido van Rossum670e5a01992-01-17 14:03:20 +00001257A parenthesized form is an optional condition list enclosed in
1258parentheses:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001259
Guido van Rossum670e5a01992-01-17 14:03:20 +00001260\begin{verbatim}
1261parenth_form: "(" [condition_list] ")"
1262\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001263
Guido van Rossum670e5a01992-01-17 14:03:20 +00001264A parenthesized condition list yields whatever that condition list
1265yields.
1266
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001267An empty pair of parentheses yields an empty tuple object. Since
1268tuples are immutable, the rules for literals apply here.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001269
1270(Note that tuples are not formed by the parentheses, but rather by use
1271of the comma operator. The exception is the empty tuple, for which
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001272parentheses {\em are} required --- allowing unparenthesized ``nothing''
Guido van Rossum670e5a01992-01-17 14:03:20 +00001273in expressions would causes ambiguities and allow common typos to
1274pass uncaught.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001275
1276\subsection{List displays}
1277
Guido van Rossum670e5a01992-01-17 14:03:20 +00001278A list display is a possibly empty series of conditions enclosed in
1279square brackets:
1280
1281\begin{verbatim}
1282list_display: "[" [condition_list] "]"
1283\end{verbatim}
1284
Guido van Rossumf2612d11991-11-21 13:53:03 +00001285A list display yields a new list object.
1286
1287If it has no condition list, the list object has no items.
1288Otherwise, the elements of the condition list are evaluated
1289from left to right and inserted in the list object in that order.
1290
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001291\subsection{Dictionary displays} \label{dict}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001292
Guido van Rossum670e5a01992-01-17 14:03:20 +00001293A dictionary display is a possibly empty series of key/datum pairs
1294enclosed in curly braces:
1295
1296\begin{verbatim}
1297dict_display: "{" [key_datum_list] "}"
1298key_datum_list: [key_datum ("," key_datum)* [","]
1299key_datum: condition ":" condition
1300\end{verbatim}
1301
Guido van Rossumf2612d11991-11-21 13:53:03 +00001302A dictionary display yields a new dictionary object.
1303
Guido van Rossum670e5a01992-01-17 14:03:20 +00001304The key/datum pairs are evaluated from left to right to define the
1305entries of the dictionary: each key object is used as a key into the
1306dictionary to store the corresponding datum.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001307
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001308Keys must be strings, otherwise a \verb\TypeError\ exception is raised.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001309Clashes between duplicate keys are not detected; the last datum
1310(textually rightmost in the display) stored for a given key value
1311prevails.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001312
1313\subsection{String conversions}
1314
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001315A string conversion is a condition list enclosed in reverse (or
Guido van Rossum670e5a01992-01-17 14:03:20 +00001316backward) quotes:
1317
1318\begin{verbatim}
1319string_conversion: "`" condition_list "`"
1320\end{verbatim}
1321
Guido van Rossumf2612d11991-11-21 13:53:03 +00001322A string conversion evaluates the contained condition list and converts the
1323resulting object into a string according to rules specific to its type.
1324
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001325If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossum670e5a01992-01-17 14:03:20 +00001326dictionary containing only objects whose type is one of these, the
1327resulting string is a valid Python expression which can be passed to
1328the built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +00001329same value (or an approximation, if floating point numbers are
1330involved).
1331
1332(In particular, converting a string adds quotes around it and converts
1333``funny'' characters to escape sequences that are safe to print.)
1334
Guido van Rossum670e5a01992-01-17 14:03:20 +00001335It is illegal to attempt to convert recursive objects (e.g., lists or
1336dictionaries that contain a reference to themselves, directly or
1337indirectly.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001338
1339\section{Primaries}
1340
1341Primaries represent the most tightly bound operations of the language.
1342Their syntax is:
1343
1344\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001345primary: atom | attributeref | subscription | slicing | call
Guido van Rossumf2612d11991-11-21 13:53:03 +00001346\end{verbatim}
1347
1348\subsection{Attribute references}
1349
Guido van Rossum670e5a01992-01-17 14:03:20 +00001350An attribute reference is a primary followed by a period and a name:
1351
1352\begin{verbatim}
1353attributeref: primary "." identifier
1354\end{verbatim}
1355
1356The primary must evaluate to an object of a type that supports
1357attribute references, e.g., a module or a list. This object is then
1358asked to produce the attribute whose name is the identifier. If this
1359attribute is not available, the exception \verb\AttributeError\ is
1360raised. Otherwise, the type and value of the object produced is
1361determined by the object. Multiple evaluations of the same attribute
1362reference may yield different objects.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001363
1364\subsection{Subscriptions}
1365
Guido van Rossum670e5a01992-01-17 14:03:20 +00001366A subscription selects an item of a sequence or mapping object:
1367
1368\begin{verbatim}
1369subscription: primary "[" condition "]"
1370\end{verbatim}
1371
1372The primary must evaluate to an object of a sequence or mapping type.
1373
1374If it is a mapping, the condition must evaluate to an object whose
1375value is one of the keys of the mapping, and the subscription selects
1376the value in the mapping that corresponds to that key.
1377
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001378If it is a sequence, the condition must evaluate to a plain integer.
1379If this value is negative, the length of the sequence is added to it
1380(so that, e.g., \verb\x[-1]\ selects the last item of \verb\x\.)
1381The resulting value must be a nonnegative integer smaller than the
1382number of items in the sequence, and the subscription selects the item
1383whose index is that value (counting from zero).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001384
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001385A string's items are characters. A character is not a separate data
Guido van Rossum670e5a01992-01-17 14:03:20 +00001386type but a string of exactly one character.
1387
Guido van Rossumf2612d11991-11-21 13:53:03 +00001388\subsection{Slicings}
1389
Guido van Rossum670e5a01992-01-17 14:03:20 +00001390A slicing selects a range of items in a sequence object:
1391
1392\begin{verbatim}
1393slicing: primary "[" [condition] ":" [condition] "]"
1394\end{verbatim}
1395
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001396The primary must evaluate to a sequence object. The lower and upper
1397bound expressions, if present, must evaluate to plain integers;
1398defaults are zero and the sequence's length, respectively. If either
1399bound is negative, the sequence's length is added to it. The slicing
1400now selects all items with index $k$ such that $i <= k < j$ where $i$
1401and $j$ are the specified lower and upper bounds. This may be an
1402empty sequence. It is not an error if $i$ or $j$ lie outside the
1403range of valid indexes (such items don't exist so they aren't
1404selected).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001405
1406\subsection{Calls}
1407
1408A call calls a function with a possibly empty series of arguments:
1409
1410\begin{verbatim}
1411call: primary "(" [condition_list] ")"
1412\end{verbatim}
1413
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001414The primary must evaluate to a callable object (user-defined
1415functions, built-in functions, methods of built-in objects, class
1416objects, and methods of class instances are callable). If it is a
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001417class, the argument list must be empty; otherwise, the arguments are
1418evaluated.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001419
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001420A call always returns some value, possibly \verb\None\, unless it
1421raises an exception. How this value is computed depends on the type
1422of the callable object. If it is:
1423
1424\begin{description}
1425
1426\item[a user-defined function:] the code block for the function is
1427executed, passing it the argument list. The first thing the code
1428block will do is bind the formal parameters to the arguments. When
1429the code block executes a \verb\return\ statement, this specifies the
1430return value of the function call.
1431
1432\item[a built-in function or method:] the result is up to the
1433interpreter; see the library reference manual for the descriptions of
1434built-in functions and methods.
1435
1436\item[a class object:] a new instance of that class is returned.
1437
1438\item[a class instance method:] the corresponding user-defined
1439function is called, with an argument list that is one longer than the
1440argument list of the call: the instance becomes the first argument.
1441
1442\end{description}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001443
Guido van Rossumf2612d11991-11-21 13:53:03 +00001444\section{Factors}
1445
1446Factors represent the unary numeric operators.
1447Their syntax is:
1448
1449\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001450factor: primary | "-" factor | "+" factor | "~" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001451\end{verbatim}
1452
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001453The unary \verb\"-"\ operator yields the negative of its
1454numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001455
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001456The unary \verb\"+"\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001457
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001458The unary \verb\"~"\ operator yields the bit-wise negation of its
1459plain or long integer argument. The bit-wise negation negation of
1460\verb\x\ is defined as \verb\-(x+1)\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001461
1462In all three cases, if the argument does not have the proper type,
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001463a \verb\TypeError\ exception is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001464
1465\section{Terms}
1466
1467Terms represent the most tightly binding binary operators:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001468%
Guido van Rossumf2612d11991-11-21 13:53:03 +00001469\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001470term: factor | term "*" factor | term "/" factor | term "%" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001471\end{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001472%
1473The \verb\"*"\ (multiplication) operator yields the product of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001474arguments. The arguments must either both be numbers, or one argument
1475must be a plain integer and the other must be a sequence. In the
1476former case, the numbers are converted to a common type and then
1477multiplied together. In the latter case, sequence repetition is
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001478performed; a negative repetition factor yields an empty sequence.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001479
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001480The \verb\"/"\ (division) operator yields the quotient of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001481arguments. The numeric arguments are first converted to a common
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001482type. Plain or long integer division yields an integer of the same
1483type; the result is that of mathematical division with the `floor'
1484function applied to the result. Division by zero raises the
1485\verb\ZeroDivisionError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001486
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001487The \verb\"%"\ (modulo) operator yields the remainder from the
Guido van Rossum670e5a01992-01-17 14:03:20 +00001488division of the first argument by the second. The numeric arguments
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001489are first converted to a common type. A zero right argument raises the
1490\verb\ZeroDivisionError\ exception. The arguments may be floating point
1491numbers, e.g., \verb\3.14 % 0.7\ equals \verb\0.34\. The modulo operator
Guido van Rossum670e5a01992-01-17 14:03:20 +00001492always yields a result with the same sign as its second operand (or
1493zero); the absolute value of the result is strictly smaller than the
1494second operand.
1495
1496The integer division and modulo operators are connected by the
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001497following identity: \verb\x == (x/y)*y + (x%y)\.
1498Integer division and modulo are also connected with the built-in
1499function \verb\divmod()\: \verb\divmod(x, y) == (x/y, x%y)\.
1500These identities don't hold for floating point numbers; there a
1501similar identity holds where \verb\x/y\ is replaced by
1502\verb\floor(x/y)\).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001503
1504\section{Arithmetic expressions}
1505
1506\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001507arith_expr: term | arith_expr "+" term | arith_expr "-" term
Guido van Rossumf2612d11991-11-21 13:53:03 +00001508\end{verbatim}
1509
Guido van Rossum670e5a01992-01-17 14:03:20 +00001510The \verb|"+"| operator yields the sum of its arguments. The
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001511arguments must either both be numbers, or both sequences of the same
1512type. In the former case, the numbers are converted to a common type
1513and then added together. In the latter case, the sequences are
1514concatenated.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001515
Guido van Rossum743d1e71992-01-07 16:43:53 +00001516The \verb|"-"| operator yields the difference of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001517The numeric arguments are first converted to a common type.
1518
1519\section{Shift expressions}
1520
1521\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001522shift_expr: arith_expr | shift_expr ( "<<" | ">>" ) arith_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001523\end{verbatim}
1524
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001525These operators accept plain or long integers as arguments. The
1526arguments are converted to a common type. They shift the first
1527argument to the left or right by the number of bits given by the
1528second argument.
1529
1530A right shift by $n$ bits is defined as division by $2^n$. A left
1531shift by $n$ bits is defined as multiplication with $2^n$ without
1532overflow check; for plain integers this drops bits if the result is
1533not less than $2^{31} - 1$ in absolute value.
1534
1535Negative shift counts raise a \verb\ValueError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001536
1537\section{Bitwise AND expressions}
1538
1539\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001540and_expr: shift_expr | and_expr "&" shift_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001541\end{verbatim}
1542
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001543This operator yields the bitwise AND of its arguments, which must be
1544plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001545
1546\section{Bitwise XOR expressions}
1547
1548\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001549xor_expr: and_expr | xor_expr "^" and_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001550\end{verbatim}
1551
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001552This operator yields the bitwise exclusive OR of its arguments, which
1553must be plain or long integers. The arguments are converted to a
1554common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001555
1556\section{Bitwise OR expressions}
1557
1558\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001559or_expr: xor_expr | or_expr "|" xor_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001560\end{verbatim}
1561
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001562This operator yields the bitwise OR of its arguments, which must be
1563plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001564
1565\section{Comparisons}
1566
1567\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001568comparison: or_expr (comp_operator or_expr)*
Guido van Rossum743d1e71992-01-07 16:43:53 +00001569comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001570\end{verbatim}
1571
1572Comparisons yield integer value: 1 for true, 0 for false.
1573
1574Comparisons can be chained arbitrarily,
1575e.g., $x < y <= z$ is equivalent to
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001576$x < y$ \verb\and\ $y <= z$, except that $y$ is evaluated only once
Guido van Rossumf2612d11991-11-21 13:53:03 +00001577(but in both cases $z$ is not evaluated at all when $x < y$ is
1578found to be false).
1579
1580Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001581$e_0 op_1 e_1$ \verb\and\ $e_1 op_2 e_2$ \verb\and\ ... \verb\and\
Guido van Rossumf2612d11991-11-21 13:53:03 +00001582$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
1583
1584Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
1585between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
1586
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001587The forms \verb\<>\ and \verb\!=\ are equivalent; for consistency with
1588C, \verb\!=\ is preferred; where \verb\!=\ is mentioned below
1589\verb\<>\ is also implied.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001590
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001591The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare
Guido van Rossumf2612d11991-11-21 13:53:03 +00001592the values of two objects. The objects needn't have the same type.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001593If both are numbers, they are coverted to a common type. Otherwise,
1594objects of different types {\em always} compare unequal, and are
1595ordered consistently but arbitrarily.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001596
1597(This unusual
1598definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001599operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001600
1601Comparison of objects of the same type depends on the type:
1602
1603\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001604
1605\item
1606Numbers are compared arithmetically.
1607
1608\item
1609Strings are compared lexicographically using the numeric equivalents
1610(the result of the built-in function \verb\ord\) of their characters.
1611
1612\item
1613Tuples and lists are compared lexicographically using comparison of
1614corresponding items.
1615
1616\item
1617Mappings (dictionaries) are compared through lexicographic
1618comparison of their sorted (key, value) lists.%
1619\footnote{This is expensive since it requires sorting the keys first,
1620but about the only sensible definition. It was tried to compare
Guido van Rossumcb9d66d1992-03-20 14:59:04 +00001621dictionaries by identity only, but this caused surprises because
1622people expected to be able to test a dictionary for emptiness by
1623comparing it to {\tt \{\}}.}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001624
1625\item
1626Most other types compare unequal unless they are the same object;
1627the choice whether one object is considered smaller or larger than
1628another one is made arbitrarily but consistently within one
1629execution of a program.
1630
Guido van Rossumf2612d11991-11-21 13:53:03 +00001631\end{itemize}
1632
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001633The operators \verb\in\ and \verb\not in\ test for sequence
1634membership: if $y$ is a sequence, $x ~\verb\in\~ y$ is true if and
1635only if there exists an index $i$ such that $x = y[i]$.
1636$x ~\verb\not in\~ y$ yields the inverse truth value. The exception
1637\verb\TypeError\ is raised when $y$ is not a sequence, or when $y$ is
1638a string and $x$ is not a string of length one.%
1639\footnote{The latter restriction is sometimes a nuisance.}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001640
1641The operators \verb\is\ and \verb\is not\ compare object identity:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001642$x ~\verb\is\~ y$ is true if and only if $x$ and $y$ are the same
1643object. $x ~\verb\is not\~ y$ yields the inverse truth value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001644
1645\section{Boolean operators}
1646
1647\begin{verbatim}
1648condition: or_test
Guido van Rossum743d1e71992-01-07 16:43:53 +00001649or_test: and_test | or_test "or" and_test
1650and_test: not_test | and_test "and" not_test
1651not_test: comparison | "not" not_test
Guido van Rossumf2612d11991-11-21 13:53:03 +00001652\end{verbatim}
1653
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001654In the context of Boolean operators, and also when conditions are used
1655by control flow statements, the following values are interpreted as
1656false: \verb\None\, numeric zero of all types, empty sequences
1657(strings, tuples and lists), and empty mappings (dictionaries). All
1658other values are interpreted as true.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001659
1660The operator \verb\not\ yields 1 if its argument is false, 0 otherwise.
1661
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001662The condition $x ~\verb\and\~ y$ first evaluates $x$; if $x$ is false,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001663$x$ is returned; otherwise, $y$ is evaluated and returned.
1664
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001665The condition $x ~\verb\or\~ y$ first evaluates $x$; if $x$ is true,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001666$x$ is returned; otherwise, $y$ is evaluated and returned.
1667
1668(Note that \verb\and\ and \verb\or\ do not restrict the value and type
1669they return to 0 and 1, but rather return the last evaluated argument.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001670This is sometimes useful, e.g., if \verb\s\ is a string, which should be
1671replaced by a default value if it is empty, \verb\s or 'foo'\
Guido van Rossumf2612d11991-11-21 13:53:03 +00001672returns the desired value. Because \verb\not\ has to invent a value
1673anyway, it does not bother to return a value of the same type as its
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001674argument, so \verb\not 'foo'\ yields \verb\0\, not \verb\''\.)
1675
1676\section{Expression lists and condition lists}
1677
1678\begin{verbatim}
1679expr_list: or_expr ("," or_expr)* [","]
1680cond_list: condition ("," condition)* [","]
1681\end{verbatim}
1682
1683The only difference between expression lists and condition lists is
1684the lowest priority of operators that can be used in them without
1685being enclosed in parentheses; condition lists allow all operators,
1686while expression lists don't allow comparisons and Boolean operators
1687(they do allow bitwise and shift operators though).
1688
1689Expression lists are used in expression statements and assignments;
1690condition lists are used everywhere else.
1691
1692An expression (condition) list containing at least one comma yields a
1693tuple. The length of the tuple is the number of expressions
1694(conditions) in the list. The expressions (conditions) are evaluated
1695from left to right.
1696
1697The trailing comma is required only to create a single tuple (a.k.a. a
1698{\em singleton}); it is optional in all other cases. A single
1699expression (condition) without a trailing comma doesn't create a
1700tuple, but rather yields the value of that expression (condition).
1701
1702To create an empty tuple, use an empty pair of parentheses: \verb\()\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001703
1704\chapter{Simple statements}
1705
1706Simple statements are comprised within a single logical line.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001707Several simple statements may occur on a single line separated
Guido van Rossumf2612d11991-11-21 13:53:03 +00001708by semicolons. The syntax for simple statements is:
1709
1710\begin{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001711simple_stmt: expression_stmt
1712 | assignment
1713 | pass_stmt
1714 | del_stmt
1715 | print_stmt
1716 | return_stmt
1717 | raise_stmt
1718 | break_stmt
1719 | continue_stmt
1720 | import_stmt
Guido van Rossum743d1e71992-01-07 16:43:53 +00001721 | global_stmt
Guido van Rossumf2612d11991-11-21 13:53:03 +00001722\end{verbatim}
1723
1724\section{Expression statements}
1725
1726\begin{verbatim}
1727expression_stmt: expression_list
1728\end{verbatim}
1729
1730An expression statement evaluates the expression list (which may
1731be a single expression).
1732If the value is not \verb\None\, it is converted to a string
1733using the rules for string conversions, and the resulting string
1734is written to standard output on a line by itself.
1735
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001736(The exception for \verb\None\ is made so that procedure calls, which
1737are syntactically equivalent to expressions, do not cause any output.
1738A tuple with only \verb\None\ items is written normally.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001739
1740\section{Assignments}
1741
1742\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001743assignment: (target_list "=")+ expression_list
Guido van Rossum743d1e71992-01-07 16:43:53 +00001744target_list: target ("," target)* [","]
1745target: identifier | "(" target_list ")" | "[" target_list "]"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001746 | attributeref | subscription | slicing
1747\end{verbatim}
1748
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001749(See the section on primaries for the syntax definition of the last
Guido van Rossumf2612d11991-11-21 13:53:03 +00001750three symbols.)
1751
1752An assignment evaluates the expression list (remember that this can
1753be a single expression or a comma-separated list,
1754the latter yielding a tuple)
1755and assigns the single resulting object to each of the target lists,
1756from left to right.
1757
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001758Assignment is defined recursively depending on the form of the target.
1759When a target is part of a mutable object (an attribute reference,
1760subscription or slicing), the mutable object must ultimately perform
1761the assignment and decide about its validity, and may raise an
1762exception if the assignment is unacceptable. The rules observed by
1763various types and the exceptions raised are given with the definition
1764of the object types (some of which are defined in the library
1765reference).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001766
1767Assignment of an object to a target list is recursively
1768defined as follows.
1769
1770\begin{itemize}
1771\item
1772If the target list contains no commas (except in nested constructs):
1773the object is assigned to the single target contained in the list.
1774
1775\item
1776If the target list contains commas (that are not in nested constructs):
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001777the object must be a tuple with the same number of items
Guido van Rossumf2612d11991-11-21 13:53:03 +00001778as the list contains targets, and the items are assigned, from left
1779to right, to the corresponding targets.
1780
1781\end{itemize}
1782
1783Assignment of an object to a (non-list)
1784target is recursively defined as follows.
1785
1786\begin{itemize}
1787
1788\item
1789If the target is an identifier (name):
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001790\begin{itemize}
1791\item
1792If the name does not occur in a \verb\global\ statement in the current
1793code block: the object is bound to that name in the current local
1794name space.
1795\item
1796Otherwise: the object is bound to that name in the current global name
1797space.
1798\end{itemize}
1799A previous binding of the same name in the same name space is undone.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001800
1801\item
1802If the target is a target list enclosed in parentheses:
1803the object is assigned to that target list.
1804
1805\item
1806If the target is a target list enclosed in square brackets:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001807the object must be a list with the same number of items
Guido van Rossumf2612d11991-11-21 13:53:03 +00001808as the target list contains targets,
1809and the list's items are assigned, from left to right,
1810to the corresponding targets.
1811
1812\item
1813If the target is an attribute reference:
1814The primary expression in the reference is evaluated.
1815It should yield an object with assignable attributes;
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001816if this is not the case, \verb\TypeError\ is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001817That object is then asked to assign the assigned object
1818to the given attribute; if it cannot perform the assignment,
1819it raises an exception.
1820
1821\item
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001822If the target is a subscription: The primary expression in the
1823reference is evaluated. It should yield either a mutable sequence
1824(list) object or a mapping (dictionary) object. Next, the subscript
1825expression is evaluated.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001826
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001827If the primary is a sequence object, the subscript must yield a plain
1828integer. If it is negative, the sequence's length is added to it.
1829The resulting value must be a nonnegative integer less than the
1830sequence's length, and the sequence is asked to assign the assigned
1831object to its item with that index. If the index is out of range,
1832\verb\IndexError\ is raised (assignment to a subscripted sequence
1833cannot add new items to a list).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001834
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001835If the primary is a mapping object, the subscript must have a type
1836compatible with the mapping's key type, and the mapping is then asked
1837to to create a key/datum pair which maps the subscript to the assigned
1838object. This can either replace an existing key/value pair with the
1839same key value, or insert a new key/value pair (if no key with the
1840same value existed).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001841
1842\item
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001843If the target is a slicing: The primary expression in the reference is
1844evaluated. It should yield a mutable sequence (list) object. The
1845assigned object should be a sequence object of the same type. Next,
1846the lower and upper bound expressions are evaluated, insofar they are
1847present; defaults are zero and the sequence's length. The bounds
1848should evaluate to (small) integers. If either bound is negative, the
1849sequence's length is added to it. The resulting bounds are clipped to
1850lie between zero and the sequence's length, inclusive. Finally, the
1851sequence object is asked to replace the items indicated by the slice
1852with the items of the assigned sequence. This may change the
1853sequence's length, if it allows it.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001854
1855\end{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001856
Guido van Rossumf2612d11991-11-21 13:53:03 +00001857(In the original implementation, the syntax for targets is taken
1858to be the same as for expressions, and invalid syntax is rejected
1859during the code generation phase, causing less detailed error
1860messages.)
1861
Guido van Rossum68c172e1992-01-21 11:34:56 +00001862\section{The {\tt pass} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001863
1864\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001865pass_stmt: "pass"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001866\end{verbatim}
1867
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001868\verb\pass\ is a null operation --- when it is executed, nothing
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001869happens. It is useful as a placeholder when a statement is
1870required syntactically, but no code needs to be executed, for example:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001871
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001872\begin{verbatim}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001873def f(arg): pass # a function that does nothing (yet)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001874
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001875class C: pass # an class with no methods (yet)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001876\end{verbatim}
1877
Guido van Rossum68c172e1992-01-21 11:34:56 +00001878\section{The {\tt del} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001879
1880\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001881del_stmt: "del" target_list
Guido van Rossumf2612d11991-11-21 13:53:03 +00001882\end{verbatim}
1883
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001884Deletion is recursively defined very similar to the way assignment is
1885defined. Rather that spelling it out in full details, here are some
1886hints.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001887
1888Deletion of a target list recursively deletes each target,
1889from left to right.
1890
1891Deletion of a name removes the binding of that name (which must exist)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001892from the local or global name space, depending on whether the name
1893occurs in a \verb\global\ statement in the same code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001894
1895Deletion of attribute references, subscriptions and slicings
1896is passed to the primary object involved; deletion of a slicing
1897is in general equivalent to assignment of an empty slice of the
1898right type (but even this is determined by the sliced object).
1899
Guido van Rossum68c172e1992-01-21 11:34:56 +00001900\section{The {\tt print} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001901
1902\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001903print_stmt: "print" [ condition ("," condition)* [","] ]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001904\end{verbatim}
1905
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001906\verb\print\ evaluates each condition in turn and writes the resulting
1907object to standard output (see below). If an object is not a string,
1908it is first converted to a string using the rules for string
1909conversions. The (resulting or original) string is then written. A
1910space is written before each object is (converted and) written, unless
1911the output system believes it is positioned at the beginning of a
1912line. This is the case: (1) when no characters have yet been written
1913to standard output; or (2) when the last character written to standard
1914output is \verb/\n/; or (3) when the last write operation on standard
1915output was not a \verb\print\ statement. (In some cases it may be
1916functional to write an empty string to standard output for this
1917reason.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001918
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001919A \verb/"\n"/ character is written at the end, unless the \verb\print\
1920statement ends with a comma. This is the only action if the statement
1921contains just the keyword \verb\print\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001922
1923Standard output is defined as the file object named \verb\stdout\
1924in the built-in module \verb\sys\. If no such object exists,
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001925or if it is not a writable file, a \verb\RuntimeError\ exception is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001926(The original implementation attempts to write to the system's original
1927standard output instead, but this is not safe, and should be fixed.)
1928
Guido van Rossum68c172e1992-01-21 11:34:56 +00001929\section{The {\tt return} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001930
1931\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001932return_stmt: "return" [condition_list]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001933\end{verbatim}
1934
1935\verb\return\ may only occur syntactically nested in a function
1936definition, not within a nested class definition.
1937
1938If a condition list is present, it is evaluated, else \verb\None\
1939is substituted.
1940
1941\verb\return\ leaves the current function call with the condition
1942list (or \verb\None\) as return value.
1943
1944When \verb\return\ passes control out of a \verb\try\ statement
1945with a \verb\finally\ clause, that finally clause is executed
1946before really leaving the function.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001947
Guido van Rossum68c172e1992-01-21 11:34:56 +00001948\section{The {\tt raise} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001949
1950\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001951raise_stmt: "raise" condition ["," condition]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001952\end{verbatim}
1953
1954\verb\raise\ evaluates its first condition, which must yield
1955a string object. If there is a second condition, this is evaluated,
1956else \verb\None\ is substituted.
1957
1958It then raises the exception identified by the first object,
1959with the second one (or \verb\None\) as its parameter.
1960
Guido van Rossum68c172e1992-01-21 11:34:56 +00001961\section{The {\tt break} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001962
1963\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001964break_stmt: "break"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001965\end{verbatim}
1966
1967\verb\break\ may only occur syntactically nested in a \verb\for\
1968or \verb\while\ loop, not nested in a function or class definition.
1969
1970It terminates the neares enclosing loop, skipping the optional
1971\verb\else\ clause if the loop has one.
1972
1973If a \verb\for\ loop is terminated by \verb\break\, the loop control
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001974target keeps its current value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001975
1976When \verb\break\ passes control out of a \verb\try\ statement
1977with a \verb\finally\ clause, that finally clause is executed
1978before really leaving the loop.
1979
Guido van Rossum68c172e1992-01-21 11:34:56 +00001980\section{The {\tt continue} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001981
1982\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001983continue_stmt: "continue"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001984\end{verbatim}
1985
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001986\verb\continue\ may only occur syntactically nested in a \verb\for\ or
1987\verb\while\ loop, not nested in a function or class definition, and
1988not nested in the \verb\try\ clause of a \verb\try\ statement with a
1989\verb\finally\ clause (it may occur nested in a \verb\except\ or
1990\verb\finally\ clause of a \verb\try\ statement though).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001991
1992It continues with the next cycle of the nearest enclosing loop.
1993
Guido van Rossum862c6f11992-01-29 14:47:05 +00001994\section{The {\tt import} statement} \label{import}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001995
1996\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001997import_stmt: "import" identifier ("," identifier)*
1998 | "from" identifier "import" identifier ("," identifier)*
1999 | "from" identifier "import" "*"
2000\end{verbatim}
2001
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00002002Import statements are executed in two steps: (1) find a module, and
2003initialize it if necessary; (2) define a name or names in the local
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002004name space (of the scope where the \verb\import\ statement occurs).
2005The first form (without \verb\from\) repeats these steps for each
2006identifier in the list, the \verb\from\ form performs them once, with
2007the first identifier specifying the module name.
Guido van Rossum743d1e71992-01-07 16:43:53 +00002008
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00002009The system maintains a table of modules that have been initialized,
2010indexed by module name. (The current implementation makes this table
2011accessible as \verb\sys.modules\.) When a module name is found in
2012this table, step (1) is finished. If not, a search for a module
2013definition is started. This first looks for a built-in module
2014definition, and if no built-in module if the given name is found, it
2015searches a user-specified list of directories for a file whose name is
2016the module name with extension \verb\".py"\. (The current
2017implementation uses the list of strings \verb\sys.path\ as the search
2018path; it is initialized from the shell environment variable
2019\verb\$PYTHONPATH\, with an installation-dependent default.)
2020
2021If a built-in module is found, its built-in initialization code is
2022executed and step (1) is finished. If no matching file is found,
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002023\verb\ImportError\ is raised. If a file is found, it is parsed,
2024yielding an executable code block. If a syntax error occurs,
2025\verb\SyntaxError\ is raised. Otherwise, an empty module of the given
2026name is created and inserted in the module table, and then the code
2027block is executed in the context of this module. Exceptions during
2028this execution terminate step (1).
2029
2030When step (1) finishes without raising an exception, step (2) can
2031begin.
2032
2033The first form of \verb\import\ statement binds the module name in the
2034local name space to the module object, and then goes on to import the
2035next identifier, if any. The \verb\from\ from does not bind the
2036module name: it goes through the list of identifiers, looks each one
2037of them up in the module found in step (1), and binds the name in the
2038local name space to the object thus found. If a name is not found,
2039\verb\ImportError\ is raised. If the list of identifiers is replaced
2040by a star (\verb\*\), all names defined in the module are bound,
2041except those beginning with an underscore(\verb\_\).
2042
2043Names bound by import statements may not occur in \verb\global\
2044statements in the same scope.
2045
2046The \verb\from\ form with \verb\*\ may only occur in a module scope.
2047
2048(The current implementation does not enforce the latter two
2049restrictions, but programs should not abuse this freedom, as future
2050implementations may enforce them or silently change the meaning of the
2051program.)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00002052
Guido van Rossum862c6f11992-01-29 14:47:05 +00002053\section{The {\tt global} statement} \label{global}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002054
2055\begin{verbatim}
2056global_stmt: "global" identifier ("," identifier)*
Guido van Rossumf2612d11991-11-21 13:53:03 +00002057\end{verbatim}
2058
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002059The \verb\global\ statement is a declaration which holds for the
2060entire current scope. It means that the listed identifiers are to be
2061interpreted as globals. While {\em using} global names is automatic
2062if they are not defined in the local scope, {\em assigning} to global
2063names would be impossible without \verb\global\.
2064
2065Names listed in a \verb\global\ statement must not be used in the same
2066scope before that \verb\global\ statement is executed.
2067
2068Name listed in a \verb\global\ statement must not be defined as formal
2069parameters or in a \verb\for\ loop control target, \verb\class\
2070definition, function definition, or \verb\import\ statement.
2071
2072(The current implementation does not enforce the latter two
2073restrictions, but programs should not abuse this freedom, as future
2074implementations may enforce them or silently change the meaning of the
2075program.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00002076
2077\chapter{Compound statements}
2078
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002079Compound statements contain (groups of) other statements; they affect
2080or control the execution of those other statements in some way.
2081
2082The \verb\if\, \verb\while\ and \verb\for\ statements implement
2083traditional control flow constructs. \verb\try\ specifies exception
2084handlers and/or cleanup code for a group of statements. Function and
2085class definitions are also syntactically compound statements.
2086
2087Compound statements consist of one or more `clauses'. A clause
2088consists of a header and a `suite'. The clause headers of a
2089particular compound statement are all at the same indentation level;
2090all clauses begin with a uniquely identifying keyword and end with a
2091colon. A suite is a group of statements controlled by a clause. A
2092suite can be a bunch of semicolon-separated simple statements on the
2093same line as the header, following the colon, or it can be a list of
2094indented statements. Only the latter form of suite can contain nested
2095compound statements; the following is illegal (mostly because it
2096wouldn't be clear what to do with \verb\else\):
Guido van Rossumf2612d11991-11-21 13:53:03 +00002097
2098\begin{verbatim}
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002099if test1: if test2: print x
Guido van Rossumf2612d11991-11-21 13:53:03 +00002100\end{verbatim}
2101
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002102Also note that the semicolon binds tighter that the colon in this
2103context (so to speak), so that in the following example, either all or
2104none of the \verb\print\ statements are executed:
2105
2106\begin{verbatim}
2107if some_test: print x; print y; print z
2108\end{verbatim}
2109
2110Summarizing:
2111
2112\begin{verbatim}
2113compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
2114suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
2115statement: stmt_list NEWLINE | compound_stmt
2116stmt_list: simple_stmt (";" simple_stmt)* [";"]
2117\end{verbatim}
2118
2119Note that statements always ends in a \verb\NEWLINE\ possibly followed
2120by a \verb\DEDENT\.
2121
2122Also note that optional continuation clauses always begin with a
2123keyword that cannot start a statement, thus there are no ambiguities
2124(the `dangling \verb\else\' problem is solved in Python by requiring
2125nested \verb\if\ statements to be indented).
2126
2127The formatting of the grammar rules in the following section places
2128each clause on a separate line for clarity.
2129
Guido van Rossum68c172e1992-01-21 11:34:56 +00002130\section{The {\tt if} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00002131
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002132The \verb\if\ statement is used for conditional execution:
2133
Guido van Rossumf2612d11991-11-21 13:53:03 +00002134\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002135if_stmt: "if" condition ":" suite
2136 ("elif" condition ":" suite)*
2137 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00002138\end{verbatim}
2139
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002140It selects exactly one of the suites, by testing the conditions one by
2141one until one is true; then that suite is executed. If all conditions
2142are false, the suite of the \verb\else\ clause is executed, if present.
2143
Guido van Rossum68c172e1992-01-21 11:34:56 +00002144\section{The {\tt while} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00002145
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002146The \verb\while\ statement is used for repeated execution as long as a
2147condition is true:
2148
Guido van Rossumf2612d11991-11-21 13:53:03 +00002149\begin{verbatim}
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002150while_stmt: "while" condition ":" suite
2151 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00002152\end{verbatim}
2153
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002154This repeatedly tests the condition and, if it is true, executes the
2155first suite; if the condition is false (which may be the first time it
2156is tested) the suite of the \verb\else\ clause is executed, if
2157present, and the loop terminates.
2158
2159A \verb\break\ statement executed in the first suite terminates the
2160loop without executing the \verb\else\ clause's suite. A
2161\verb\continue\ statement executed in the first suited skips the rest
2162of the suite and goes back to testing the condition.
2163
Guido van Rossum68c172e1992-01-21 11:34:56 +00002164\section{The {\tt for} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00002165
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002166The \verb\for\ statement is used to iterate over the elements of a
2167sequence (string, tuple or list):
2168
Guido van Rossumf2612d11991-11-21 13:53:03 +00002169\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002170for_stmt: "for" target_list "in" condition_list ":" suite
2171 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00002172\end{verbatim}
2173
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002174The suite is executed once for each item in the condition list, in the
2175order of ascending indices. Each item in turn is assigned to the
2176target list using the standard rules for assignments, and then the
2177suite is executed. When the list is exhausted (which is immediately
2178when the sequence is empty), the suite in the \verb\else\ clause is
2179executed, if present.
2180
2181A \verb\break\ statement executed in the first suite terminates the
2182loop without executing the \verb\else\ clause's suite. A
2183\verb\continue\ statement executed in the first suited skips the rest
2184of the suite and continues with the next item or with the \verb\else\
2185clause.
2186
2187The suite may assign to the variable(s) in the target list; this does
2188not affect the next item assigned to it.
2189
2190The target list are not deleted when the loop is finished (but if the
2191loop has executed 0 times it will not have been assigned to at all by
2192the loop).
2193
2194The built-in function \verb\range()\ returns a sequence of integers
2195suitable to emulate the effect of Pascal's \verb\for i := 1 to n do\.
2196
2197{\bf Warning:} There is a subtlety when the sequence is being modified
2198by the loop (this can only occur for lists). An internal counter is
2199used to keep track of which item is used next, and this is incremented
2200on each iteration. When this counter has reached the end of the
2201sequence the loop terminates. This means that if the suite deletes
2202the current (or a previous) item from the sequence, the next item will
2203be skipped (since it gets the index of the current item and this has
2204already been treated). Likewise, if the suite inserts an item in the
2205sequence before the current item, the current item will be treated
2206again the next time through the loop. This can lead to nasty bugs
2207that can be avoided by making a temporary copy using the \
2208
Guido van Rossum68c172e1992-01-21 11:34:56 +00002209\section{The {\tt try} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00002210
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002211The \verb\try\ statement specifies exception handlers and/or cleanup
2212code for a group of statements:
2213
Guido van Rossumf2612d11991-11-21 13:53:03 +00002214\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002215try_stmt: "try" ":" suite
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002216 ("except" condition ["," target] ":" suite)*
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002217 ["except" ":" suite]
Guido van Rossum743d1e71992-01-07 16:43:53 +00002218 ["finally" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00002219\end{verbatim}
2220
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002221There are really two forms: \verb\try...except\ and
2222\verb\try...finally\. A \verb\try\ statement with both types of
2223clauses is equivalent to a \verb\try...finally\ statement with a
2224\verb\try...except\ statement in its \verb\try\ clause. A \verb\try\
2225statement with neither a \verb\except\ clause nor a \verb\finally\
2226clause just executes the suite of statements in its \verb\try\ clause.
2227
2228The \verb\try...except\ form specifies one or more exception handlers.
2229When no exception occurs in the \verb\try\ clause, no exception
2230handler is executed. When an exception occurs in the \verb\try\
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002231suite, a search for an exception handler is started. This inspects
2232the except clauses (exception handlers) in turn until one is found
2233that matches the exception. A condition-less except clause (which
2234must be last) matches any exception. For except clause with a
2235condition, that condition is evaluated, and the clause matches the
2236exception if the resulting object is ``compatible'' with the
2237exception. An object is compatible with an exception if it is either
2238the object that identifies the exception or it is a tuple containing
2239an item that is compatible with the exception.
2240
2241If no except clause matches the exception, the search for an exception
2242handler continues in the surrounding code and on the invocation stack.
2243
2244If the evaluation of a condition in the header of an except clause
2245raises an exception, the original search for a handler is cancelled
2246and a search starts for the new exception in the surrounding code and
2247on the call stack.
2248
2249When a matching except clause is found in a try statement, the
2250exception's parameter is assigned to the target specified in the
2251except clause (if present), and the except clause's suite is executed.
2252When the end of this suite is reached, execution continues normally
2253at the point following the entire try statement. (This means that if
2254two nested handlers exist for the same exception, and the exception
2255occurs in the try clause of the inner handler, the outer handler will
2256not notice the exception.)
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002257
2258The \verb\try...finally\ form specifies a `cleanup' handler. The
2259\verb\try\ clause is executed. When no exception occurs, the
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002260\verb\finally\ clause is executed. When an exception occurs in the
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002261\verb\try\ clause, the exception is temporarily saved, the
2262\verb\finally\ clause is executed, and then the saved exception is
2263re-raised. If the \verb\finally\ clause raises another exception or
2264executes a \verb\return\, \verb\break\ or \verb\continue\ statement,
2265the saved exception is lost.
2266
2267When a \verb\return\ or \verb\break\ statement is executed in the
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002268\verb\try\ suite of a \verb\try...finally\ statement, the
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002269\verb\finally\ clause is also executed `on the way out'. A
2270\verb\continue\ statement is illegal in the \verb\try\ clause (the
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002271reason is a problem with the current implementation --- this
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002272restriction may be lifted in the future).
2273
Guido van Rossum862c6f11992-01-29 14:47:05 +00002274\section{Function definitions} \label{function}
Guido van Rossum255ad6e1992-01-28 18:10:46 +00002275
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002276A function definition defines a function:
Guido van Rossumf2612d11991-11-21 13:53:03 +00002277
2278\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002279funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
2280parameter_list: parameter ("," parameter)*
2281parameter: identifier | "(" parameter_list ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00002282\end{verbatim}
2283
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002284A function definition is an executable statement. Its execution binds
2285the function name in the current local name space to a function object
2286(a wrapper around the executable code for the function). This
2287function object contains a reference to the current global name space
2288as the global name space to be used when the function is called.
2289
2290The function definition does not execute the function body; this gets
2291executed only when the function is called. Function call semantics
2292are described elsewhere (see XXX).
Guido van Rossum862c6f11992-01-29 14:47:05 +00002293
2294\section{Class definitions} \label{class}
2295
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002296A class definition defines a class:
Guido van Rossumf2612d11991-11-21 13:53:03 +00002297
2298\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00002299classdef: "class" identifier [inheritance] ":" suite
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002300inheritance: "(" condition_list ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00002301\end{verbatim}
2302
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002303A class definition is an executable statement. It first executes the
2304inheritance list, if present. The class's suite is executed in a new
2305execution frame, using a newly created local name space and the
2306original global name space. (Usually, the suite contains only
2307function definitions.) When the class's suite finishes execution, its
2308execution frame is discarded but its local name space is saved. A
2309class object (see XXX) is created using the inheritance list for the
2310base classes and the saved local name space for the attribute
2311dictionary. The class name is then bound to this class object in the
2312original local name space.
Guido van Rossum862c6f11992-01-29 14:47:05 +00002313
2314\section{P.M.}
2315
Guido van Rossum743d1e71992-01-07 16:43:53 +00002316XXX New definition of expressions (as conditions)
Guido van Rossumf2612d11991-11-21 13:53:03 +00002317
Guido van Rossumcb9d66d1992-03-20 14:59:04 +00002318\chapter{Top-level components}
2319
2320The Python interpreter can get its input from a number of sources:
2321from a script passed to it as standard input or as program argument,
2322typed in interactively, from a module source file, etc. This chapter
2323gives the syntax used in these cases.
2324
2325\section{Complete Python programs}
2326
2327While a language specification need not prescribe how the language
2328interpreter is invoked, it is useful to have a notion of a complete
2329Python program. A complete Python program is executed in a minimally
2330initialized environment: all built-in and standard modules are
2331available, but none have been initialized, except for \verb\sys\
2332(various system services), \verb\builtin\ (built-in functions,
2333exceptions and \verb\None\) and \verb\__main__\. The latter is used
2334to provide the local and global name space for execution of the
2335complete program.
2336
2337The syntax for a complete Python program is that for file input,
2338described in the next section.
2339
2340The interpreter may also be invoked in interactive mode; in this case,
2341it does not read and execute a complete program but reads and executes
2342one statement (possibly compound) at a time. The initial environment
2343is identical to that of a complete program; each statement is executed
2344in the name space of \verb\__main__\.
2345
2346Under {\UNIX}, a complete program can be passed to the interpreter in
2347three forms: with the {\bf -c} {\it string} command line option, as a
2348file passed as the first command line argument, or as standard input.
2349If the file or standard input is a tty device, the interpreter enters
2350interactive mode; otherwise, it executes the file as a complete
2351program.
2352
2353\section{File input}
2354
2355All input read from non-interactive files has the same form:
2356
2357\begin{verbatim}
2358file_input: (NEWLINE | statement)*
2359\end{verbatim}
2360
2361This syntax is used in the following situations:
2362
2363\begin{itemize}
2364
2365\item when parsing a complete Python program (from a file or from a string);
2366
2367\item when parsing a module;
2368
2369\item when parsing a string passed to \verb\exec()\;
2370
2371\item when parsing a file passed to \verb\execfile()\;
2372
2373\end{itemize}
2374
2375\section{Interactive input}
2376
2377Input in interactive mode is parsed using the following grammar:
2378
2379\begin{verbatim}
2380interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
2381\end{verbatim}
2382
2383Note that a (top-level) compound statement must be followed by a blank
2384line in interactive mode; this is needed to help the parser detect the
2385end of the input.
2386
2387\section{Expression input}
2388
2389There are two forms of expression input. Both ignore leading
2390whitespace.
2391
2392The string argument to \verb\eval()\ must have the following form:
2393
2394\begin{verbatim}
2395eval_input: condition_list NEWLINE*
2396\end{verbatim}
2397
2398The input line read by \verb\input()\ must have the following form:
2399
2400\begin{verbatim}
2401input_input: condition_list NEWLINE
2402\end{verbatim}
2403
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002404\input{ref.ind} % The index
2405
Guido van Rossumf2612d11991-11-21 13:53:03 +00002406\end{document}