blob: 7a9fd5cae15cd712b158678d9bcab9b08807541d [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 Rossumb5e1c181992-03-06 10:52:59 +0000419largest positive integer, assuming 32-bit arithmetic); plain octal and
420hexadecimal literals may be as large as $2^{32} - 1$, but values
421larger than $2^{31} - 1$ are converted to a signed value in the range
422$-2^{31} \dots 2^{31} - 1$ by subtracting $2^{32}$. There is no limit
Guido van Rossum670e5a01992-01-17 14:03:20 +0000423for long integer literals.
424
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000425Some examples of plain and long integer literals:
Guido van Rossum670e5a01992-01-17 14:03:20 +0000426
427\begin{verbatim}
4287 2147483647 0177 0x80000000
Guido van Rossumb5e1c181992-03-06 10:52:59 +00004293L 79228162514264337593543950336L 0377L 0x100000000L
Guido van Rossum670e5a01992-01-17 14:03:20 +0000430\end{verbatim}
431
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000432Floating point literals are described by the following lexical
433definitions:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000434
435\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000436floatnumber: pointfloat | exponentfloat
437pointfloat: [intpart] fraction | intpart "."
438exponentfloat: (intpart | pointfloat) exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000439intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000440fraction: "." digit+
441exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000442\end{verbatim}
443
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000444The allowed range of floating point literals is
445implementation-dependent.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000446
447Some examples of floating point literals:
Guido van Rossum7b632a61992-01-16 17:49:21 +0000448
449\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00004503.14 10. .001 1e100 3.14e-10
Guido van Rossum7b632a61992-01-16 17:49:21 +0000451\end{verbatim}
452
Guido van Rossum670e5a01992-01-17 14:03:20 +0000453Note that numeric literals do not include a sign; a phrase like
454\verb\-1\ is actually an expression composed of the operator
Guido van Rossum7b632a61992-01-16 17:49:21 +0000455\verb\-\ and the literal \verb\1\.
456
Guido van Rossumf2612d11991-11-21 13:53:03 +0000457\section{Operators}
458
459The following tokens are operators:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000460\index{operators}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000461
462\begin{verbatim}
463+ - * / %
464<< >> & | ^ ~
Guido van Rossum743d1e71992-01-07 16:43:53 +0000465< == > <= <> != >=
Guido van Rossumf2612d11991-11-21 13:53:03 +0000466\end{verbatim}
467
Guido van Rossum743d1e71992-01-07 16:43:53 +0000468The comparison operators \verb\<>\ and \verb\!=\ are alternate
469spellings of the same operator.
470
Guido van Rossumf2612d11991-11-21 13:53:03 +0000471\section{Delimiters}
472
Guido van Rossum743d1e71992-01-07 16:43:53 +0000473The following tokens serve as delimiters or otherwise have a special
474meaning:
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000475\index{delimiters}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000476
477\begin{verbatim}
478( ) [ ] { }
Guido van Rossum743d1e71992-01-07 16:43:53 +0000479; , : . ` =
Guido van Rossumf2612d11991-11-21 13:53:03 +0000480\end{verbatim}
481
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000482The following printing ASCII characters are not used in Python. Their
483occurrence outside string literals and comments is an unconditional
484error:
485\index{ASCII}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000486
487\begin{verbatim}
488! @ $ " ?
489\end{verbatim}
490
Guido van Rossum7b632a61992-01-16 17:49:21 +0000491They may be used by future versions of the language though!
492
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000493\chapter{Data model}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000494
Guido van Rossum743d1e71992-01-07 16:43:53 +0000495\section{Objects, values and types}
496
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000497{\em Objects} are Python's abstraction for data. All data in a Python
498program is represented by objects or by relations between objects.
499(In a sense, and in conformance to Von Neumann's model of a
500``stored program computer'', code is also represented by objects.)
501\index{object}
502\index{data}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000503
504Every object has an identity, a type and a value. An object's {\em
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000505identity} never changes once it has been created; you may think of it
506as the object's address in memory. An object's {\em type} is also
507unchangeable. It determines the operations that an object supports
508(e.g., ``does it have a length?'') and also defines the possible
509values for objects of that type. The {\em value} of some objects can
510change. Objects whose value can change are said to be {\em mutable};
511objects whose value is unchangeable once they are created are called
512{\em immutable}. The type determines an object's (im)mutability.
513\index{identity of an object}
514\index{value of an object}
515\index{type of an object}
516\index{mutable object}
517\index{immutable object}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000518
519Objects are never explicitly destroyed; however, when they become
Guido van Rossum670e5a01992-01-17 14:03:20 +0000520unreachable they may be garbage-collected. An implementation is
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000521allowed to delay garbage collection or omit it altogether --- it is a
Guido van Rossum670e5a01992-01-17 14:03:20 +0000522matter of implementation quality how garbage collection is
523implemented, as long as no objects are collected that are still
524reachable. (Implementation note: the current implementation uses a
Guido van Rossum743d1e71992-01-07 16:43:53 +0000525reference-counting scheme which collects most objects as soon as they
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000526become unreachable, but never collects garbage containing circular
Guido van Rossum743d1e71992-01-07 16:43:53 +0000527references.)
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000528\index{garbage collection}
529\index{reference counting}
530\index{unreachable object}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000531
Guido van Rossum670e5a01992-01-17 14:03:20 +0000532Note that the use of the implementation's tracing or debugging
533facilities may keep objects alive that would normally be collectable.
534
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000535Some objects contain references to ``external'' resources such as open
536files or windows. It is understood that these resources are freed
537when the object is garbage-collected, but since garbage collection is
538not guaranteed to happen, such objects also provide an explicit way to
539release the external resource, usually a \verb\close\ method.
540Programs are strongly recommended to always explicitly close such
541objects.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000542
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000543Some objects contain references to other objects; these are called
544{\em containers}. Examples of containers are tuples, lists and
545dictionaries. The references are part of a container's value. In
546most cases, when we talk about the value of a container, we imply the
547values, not the identities of the contained objects; however, when we
548talk about the (im)mutability of a container, only the identities of
549the immediately contained objects are implied. (So, if an immutable
550container contains a reference to a mutable object, its value changes
551if that mutable object is changed.)
552\index{container}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000553
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000554Types affect almost all aspects of objects' lives. Even the meaning
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000555of object identity is affected in some sense: for immutable types,
556operations that compute new values may actually return a reference to
557any existing object with the same type and value, while for mutable
558objects this is not allowed. E.g., after
Guido van Rossum743d1e71992-01-07 16:43:53 +0000559
560\begin{verbatim}
561a = 1; b = 1; c = []; d = []
562\end{verbatim}
563
Guido van Rossumcf8148b1992-03-02 16:13:50 +0000564\verb\a\ and \verb\b\ may or may not refer to the same object with the
565value one, depending on the implementation, but \verb\c\ and \verb\d\
566are guaranteed to refer to two different, unique, newly created empty
567lists.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000568
Guido van Rossumb5e1c181992-03-06 10:52:59 +0000569\section{The standard type hierarchy}
570
571Below is a list of the types that are built into Python. Extension
572modules written in C can define additional types. Future versions of
573Python may add types to the type hierarchy (e.g., rational or complex
574numbers, efficientlt stored arrays of integers, etc.).
575\index{type}
576\index{type hierarchy}
577\index{extension module}
578\index{C}
579
580Some of the type descriptions below contain a paragraph listing
581`special attributes'. These are attributes that provide access to the
582implementation and are not intended for general use. Their definition
583may change in the future. There are also some `generic' special
584attributes, not listed with the individual objects: \verb\__methods__\
585is a list of the method names of a built-in object, if it has any;
586\verb\__members__\ is a list of the data attribute names of a built-in
587object, if it has any.
588\index{attribute}
589\index{special attribute}
590\index{generic special attribute}
591\ttindex{__methods__}
592\ttindex{__members__}
593
594\begin{description}
595
596\item[None]
597This type has a single value. There is a single object with this value.
598This object is accessed through the built-in name \verb\None\.
599It is returned from functions that don't explicitly return an object.
600\ttindex{None}
601
602\item[Numbers]
603These are created by numeric literals and returned as results
604by arithmetic operators and arithmetic built-in functions.
605Numeric objects are immutable; once created their value never changes.
606Python numbers are of course strongly related to mathematical numbers,
607but subject to the limitations of numerical representation in computers.
608\index{number}
609
610Python distinguishes between integers and floating point numbers:
611
612\begin{description}
613\item[Integers]
614These represent elements from the mathematical set of whole numbers.
615\index{integer}
616
617There are two types of integers:
618
619\begin{description}
620
621\item[Plain integers]
622These represent numbers in the range $-2^{31}$ through $2^{31}-1$.
623(The range may be larger on machines with a larger natural word
624size, but not smaller.)
625When the result of an operation falls outside this range, the
626exception \verb\OverflowError\ is raised.
627For the purpose of shift and mask operations, integers are assumed to
628have a binary, 2's complement notation using 32 or more bits, and
629hiding no bits from the user (i.e., all $2^{32}$ different bit
630patterns correspond to different values).
631\index{plain integer}
632
633\item[Long integers]
634These represent numbers in an unlimited range, subject to avaiable
635(virtual) memory only. For the purpose of shift and mask operations,
636a binary representation is assumed, and negative numbers are
637represented in a variant of 2's complement which gives the illusion of
638an infinite string of sign bits extending to the left.
639\index{long integer}
640
641\end{description} % Integers
642
643The rules for integer representation are intended to give the most
644meaningful interpretation of shift and mask operations involving
645negative integers and the least surprises when switching between the
646plain and long integer domains. For any operation except left shift,
647if it yields a result in the plain integer domain without causing
648overflow, it will yield the same result in the long integer domain or
649when using mixed operands.
650\index{integer representation}
651
652\item[Floating point numbers]
653These represent machine-level double precision floating point numbers.
654You are at the mercy of the underlying machine architecture and
655C implementation for the accepted range and handling of overflow.
656\index{floating point number}
657\index{C}
658
659\end{description} % Numbers
660
661\item[Sequences]
662These represent finite ordered sets indexed by natural numbers.
663The built-in function \verb\len()\ returns the number of elements
664of a sequence. When this number is $n$, the index set contains
665the numbers $0, 1, \ldots, n-1$. Element \verb\i\ of sequence
666\verb\a\ is selected by \verb\a[i]\.
667\index{seqence}
668\bifuncindex{len}
669\index{index operation}
670\index{item selection}
671\index{subscription}
672
673Sequences also support slicing: \verb\a[i:j]\ selects all elements
674with index $k$ such that $i < k < j$. When used as an expression,
675a slice is a sequence of the same type --- this implies that the
676index set is renumbered so that it starts at 0 again.
677\index{slicing}
678
679Sequences are distinguished according to their mutability:
680
681\begin{description}
682%
683\item[Immutable sequences]
684An object of an immutable sequence type cannot change once it is
685created. (If the object contains references to other objects,
686these other objects may be mutable and may be changed; however
687the collection of objects directly referenced by an immutable object
688cannot change.)
689\index{immutable sequence}
690
691The following types are immutable sequences:
692
693\begin{description}
694
695\item[Strings]
696The elements of a string are characters. There is no separate
697character type; a character is represented by a string of one element.
698Characters represent (at least) 8-bit bytes. The built-in
699functions \verb\chr()\ and \verb\ord()\ convert between characters
700and nonnegative integers representing the byte values.
701Bytes with the values 0-127 represent the corresponding ASCII values.
702The string data type is also used to represent arrays of bytes, e.g.,
703to hold data read from a file.
704\index{string}
705\index{character}
706\index{byte}
707\index{ASCII}
708\bifuncindex{chr}
709\bifuncindex{ord}
710
711(On systems whose native character set is not ASCII, strings may use
712EBCDIC in their internal representation, provided the functions
713\verb\chr()\ and \verb\ord()\ implement a mapping between ASCII and
714EBCDIC, and string comparison preserves the ASCII order.
715Or perhaps someone can propose a better rule?)
716\index{ASCII}
717\index{EBCDIC}
718\index{character set}
719\index{string comparison}
720\bifuncindex{chr}
721\bifuncindex{ord}
722
723\item[Tuples]
724The elements of a tuple are arbitrary Python objects.
725Tuples of two or more elements are formed by comma-separated lists
726of expressions. A tuple of one element (a `singleton') can be formed
727by affixing a comma to an expression (an expression by itself does
728not create a tuple, since parentheses must be usable for grouping of
729expressions). An empty tuple can be formed by enclosing `nothing' in
730parentheses.
731\index{tuple}
732\index{singleton tuple}
733\index{empty tuple}
734
735\end{description} % Immutable sequences
736
737\item[Mutable sequences]
738Mutable sequences can be changed after they are created. The
739subscription and slicing notations can be used as the target of
740assignment and \verb\del\ (delete) statements.
741\index{mutable sequece}
742\index{assignment statement}
743\kwindex{del}
744\index{subscription}
745\index{slicing}
746
747There is currently a single mutable sequence type:
748
749\begin{description}
750
751\item[Lists]
752The elements of a list are arbitrary Python objects. Lists are formed
753by placing a comma-separated list of expressions in square brackets.
754(Note that there are no special cases needed to form lists of length 0
755or 1.)
756\index{list}
757
758\end{description} % Mutable sequences
759
760\end{description} % Sequences
761
762\item[Mapping types]
763These represent finite sets of objects indexed by arbitrary index sets.
764The subscript notation \verb\a[k]\ selects the element indexed
765by \verb\k\ from the mapping \verb\a\; this can be used in
766expressions and as the target of assignments or \verb\del\ statements.
767The built-in function \verb\len()\ returns the number of elements
768in a mapping.
769\bifuncindex{len}
770\index{subscription}
771\index{mapping}
772
773There is currently a single mapping type:
774
775\begin{description}
776
777\item[Dictionaries]
778These represent finite sets of objects indexed by strings.
779Dictionaries are created by the \verb\{...}\ notation (see section
780\ref{dict}). (Implementation note: the strings used for indexing must
781not contain null bytes.)
782\index{dictionary}
783
784\end{description} % Mapping types
785
786\item[Callable types]
787These are the types to which the function call (invocation) operation,
788written as \verb\function(argument, argument, ...)\, can be applied:
789\index{callable type}
790\indexii{function}{call}
791\index{invocation}
792
793\begin{description}
794
795\item[User-defined functions]
796A user-defined function object is created by a function definition
797(see section \ref{function}). It should be called with an argument
798list containing the same number of items as the function's formal
799parameter list.
800\indexii{user-defined}{function}
801\index{function object}
802
803Special read-only attributes: \verb\func_code\ is the code object
804representing the compiled function body, and \verb\func_globals\ is (a
805reference to) the dictionary that holds the function's global
806variables --- it implements the global name space of the module in
807which the function was defined.
808\ttindex{func_code}
809\ttindex{func_globals}
810\indexii{global}{name space}
811
812\item[User-defined methods]
813A user-defined method (a.k.a. {\tt object closure}) is a pair of a
814class instance object and a user-defined function. It should be
815called with an argument list containing one item less than the number
816of items in the function's formal parameter list. When called, the
817class instance becomes the first argument, and the call arguments are
818shifted one to the right.
819\indexii{object}{closure}
820indexii{user-defined}{method}
821
822Special read-only attributes: \verb\im_self\ is the class instance
823object, \verb\im_func\ is the function object.
824\ttindex{im_func}
825\ttindex{im_self}
826
827\item[Built-in functions]
828A built-in function object is a wrapper around a C function. Examples
829of built-in functions are \verb\len\ and \verb\math.sin\. There
830are no special attributes. The number and type of the arguments are
831determined by the C function.
832\index{C}
833
834\item[Built-in methods]
835This is really a different disguise of a built-in function, this time
836containing an object passed to the C function as an implicit extra
837argument. An example of a built-in method is \verb\list.append\ if
838\verb\list\ is a list object.
839\indexii{built-in}{method}
840
841\item[Classes]
842Class objects are described below. When a class object is called as a
843parameterless function, a new class instance (also described below) is
844created and returned. The class's initialization function is not
845called --- this is the responsibility of the caller. It is illegal to
846call a class object with one or more arguments.
847\index{class}
848
849\end{description}
850
851\item[Modules]
852Modules are imported by the \verb\import\ statement (see section
853\ref{import}). A module object is a container for a module's name
854space, which is a dictionary (the same dictionary as referenced by the
855\verb\func_globals\ attribute of functions defined in the module).
856Module attribute references are translated to lookups in this
857dictionary. A module object does not contain the code object used to
858initialize the module (since it isn't needed once the initialization
859is done).
860\stindex{import}
861\index{module}
862
863Attribute assignment update the module's name space dictionary.
864
865Special read-only attributes: \verb\__dict__\ yields the module's name
866space as a dictionary object; \verb\__name__\ yields the module's name
867as a string object.
868\ttindex{__dict__}
869\ttindex{__name__}
870
871\item[Classes]
872Class objects are created by class definitions (see section
873\ref{class}). A class is a container for a dictionary containing the
874class's name space. Class attribute references are translated to
875lookups in this dictionary. When an attribute name is not found
876there, the attribute search continues in the base classes. The search
877is depth-first, left-to-right in the order of their occurrence in the
878base class list.
879\index{class}
880\index{container}
881\index{dictionary}
882\indexii{class}{attribute}
883
884Class attribute assignments update the class's dictionary, never the
885dictionary of a base class.
886\indexiii{class}{attribute}{assignment}
887
888A class can be called as a parameterless function to yield a class
889instance (see above).
890
891Special read-only attributes: \verb\__dict__\ yields te dictionary
892containing the class's name space; \verb\__bases__\ yields a tuple
893(possibly empty or a singleton) containing the base classes, in the
894order of their occurrence in the base class list.
895\ttindex{__dict__}
896\ttindex{__bases__}
897
898\item[Class instances]
899A class instance is created by calling a class object as a
900parameterless function. A class instance has a dictionary in which
901attribute references are searched. When an attribute is not found
902there, and the instance's class has an attribute by that name, and
903that class attribute is a user-defined function (and in no other
904cases), the instance attribute reference yields a user-defined method
905object (see above) constructed from the instance and the function.
906\indexii{class}{instance}
907\indexii{class instance}{attribute}
908
909Attribute assignments update the instance's dictionary.
910\indexiii{class instance}{attribute}{assignment}
911
912Special read-only attributes: \verb\__dict__\ yields the attribute
913dictionary; \verb\__class__\ yields the instance's class.
914\ttindex{__dict__}
915\ttindex{__class__}
916
917\item[Files]
918A file object represents an open file. (It is a wrapper around a C
919{\tt stdio} file pointer.) File objects are created by the
920\verb\open()\ built-in function, and also by \verb\posix.popen()\ and
921the \verb\makefile\ method of socket objects. \verb\sys.stdin\,
922\verb\sys.stdout\ and \verb\sys.stderr\ are file objects corresponding
923the the interpreter's standard input, output and error streams.
924See the Python Library Reference for methods of file objects and other
925details.
926\index{file}
927\index{C}
928\index{stdio}
929\bifuncindex{open}
930\bifuncindex{popen}
931\bifuncindex{makefile}
932\ttindex{stdin}
933\ttindex{stdout}
934\ttindex{stderr}
935
936\item[Internal types]
937A few types used internally by the interpreter are exposed to the user.
938Their definition may change with future versions of the interpreter,
939but they are mentioned here for completeness.
940\index{internal type}
941
942\begin{description}
943
944\item[Code objects]
945Code objects represent executable code. The difference between a code
946object and a function object is that the function object contains an
947explicit reference to the function's context (the module in which it
948was defined) which a code object contains no context. There is no way
949to execute a bare code object.
950\index{code object}
951
952Special read-only attributes: \verb\co_code\ is a string representing
953the sequence of instructions; \verb\co_consts\ is a list of literals
954used by the code; \verb\co_names\ is a list of names (strings) used by
955the code; \verb\co_filename\ is the filename from which the code was
956compiled. (To find out the line numbers, you would have to decode the
957instructions; the standard library module \verb\dis\ contains an
958example of how to do this.)
959\ttindex{co_code}
960\ttindex{co_consts}
961\ttindex{co_names}
962\ttindex{co_filename}
963
964\item[Frame objects]
965Frame objects represent execution frames. They may occur in traceback
966objects (see below).
967\index{frame object}
968
969Special read-only attributes: \verb\f_back\ is to the previous
970stack frame (towards the caller), or \verb\None\ if this is the bottom
971stack frame; \verb\f_code\ is the code object being executed in this
972frame; \verb\f_globals\ is the dictionary used to look up global
973variables; \verb\f_locals\ is used for local variables;
974\verb\f_lineno\ gives the line number and \verb\f_lasti\ gives the
975precise instruction (this is an index into the instruction string of
976the code object).
977\ttindex{f_back}
978\ttindex{f_code}
979\ttindex{f_globals}
980\ttindex{f_locals}
981\ttindex{f_lineno}
982\ttindex{f_lasti}
983
984\item[Traceback objects]
985Traceback objects represent a stack trace of an exception. A
986traceback object is created when an exception occurs. When the search
987for an exception handler unwinds the execution stack, at each unwound
988level a traceback object is inserted in front of the current
989traceback. When an exception handler is entered, the stack trace is
990made available to the program as \verb\sys.exc_traceback\. When the
991program contains no suitable handler, the stack trace is written
992(nicely formatted) to the standard error stream; if the interpreter is
993interactive, it is also made available to the user as
994\verb\sys.last_traceback\.
995\index{traceback object}
996\indexii{stack}{trace}
997\index{exception handler}
998\index{execution stack}
999\ttindex{exc_traceback}
1000\ttindex{last_traceback}
1001
1002Special read-only attributes: \verb\tb_next\ is the next level in the
1003stack trace (towards the frame where the exception occurred), or
1004\verb\None\ if there is no next level; \verb\tb_frame\ points to the
1005execution frame of the current level; \verb\tb_lineno\ gives the line
1006number where the exception occurred; \verb\tb_lasti\ indicates the
1007precise instruction. The line number and last instruction in the
1008traceback may differ from the line number of its frame object if the
1009exception occurred in a \verb\try\ statement with no matching
1010\verb\except\ clause or with a \verb\finally\ clause.
1011\ttindex{tb_next}
1012\ttindex{tb_frame}
1013\ttindex{tb_lineno}
1014\ttindex{tb_lasti}
1015\stindex{try}
1016
1017\end{description} % Internal types
1018
1019\end{description} % Types
1020
1021\chapter{Execution model}
1022
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001023\section{Code blocks, execution frames, and name spaces}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001024
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001025A {\em code block} is a piece of Python program text that can be
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001026executed as a unit, such as a module, a class definition or a function
1027body. Some code blocks (like modules) are executed only once, others
1028(like function bodies) may be executed many times. Code block may
1029textually contain other code blocks. Code blocks may invoke other
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001030code blocks (that may or may not be textually contained in them) as
1031part of their execution, e.g., by invoking (calling) a function.
1032\index{code block}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001033
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001034The following are code blocks: A module is a code block. A function
1035body is a code block. A class definition is a code block. Each
1036command typed interactively is a separate code block; a script file is
1037a code block. The string argument passed to the built-in functions
1038\verb\eval\ and \verb\exec\ are code blocks. And finally, the
1039expression read and evaluated by the built-in function \verb\input\ is
1040a code block.
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001041
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001042A code block is executed in an execution frame. An {\em execution
1043frame} contains some administrative information (used for debugging),
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001044determines where and how execution continues after the code block's
1045execution has completed, and (perhaps most importantly) defines two
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001046name spaces, the local and the global name space, that affect
1047execution of the code block.
1048\index{execution frame}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001049
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001050A {\em name space} is a mapping from names (identifiers) to objects.
1051A particular name space may be referenced by more than one execution
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001052frame, and from other places as well. Adding a name to a name space
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001053is called {\em binding} a name (to an object); changing the mapping of
1054a name is called {\em rebinding}; removing a name is {\em unbinding}.
1055Name spaces are functionally equivalent to dictionaries.
1056\index{name space}
1057\indexii{binding}{name}
1058\indexii{rebinding}{name}
1059\indexii{unbinding}{name}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001060
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001061The {\em local name space} of an execution frame determines the default
1062place where names are defined and searched. The {\em global name
1063space} determines the place where names listed in \verb\global\
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001064statements are defined and searched, and where names that are not
1065explicitly bound in the current code block are searched.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001066\indexii{local}{name space}
1067\indexii{global}{name space}
1068\stindex{global}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001069
1070Whether a name is local or global in a code block is determined by
1071static inspection of the source text for the code block: in the
1072absence of \verb\global\ statements, a name that is bound anywhere in
1073the code block is local in the entire code block; all other names are
1074considered global. The \verb\global\ statement forces global
1075interpretation of selected names throughout the code block. The
1076following constructs bind names: formal parameters, \verb\import\
1077statements, class and function definitions (these bind the class or
1078function name), and targets that are identifiers if occurring in an
1079assignment, \verb\for\ loop header, or \verb\except\ clause header.
1080(A target occurring in a \verb\del\ statement does not bind a name.)
1081
1082When a global name is not found in the global name space, it is
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001083searched in the list of ``built-in'' names (which is actually the
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001084global name space of the module \verb\builtin\). When a name is not
1085found at all, the \verb\NameError\ exception is raised.
1086
1087The following table lists the meaning of the local and global name
1088space for various types of code blocks. The name space for a
1089particular module is automatically created when the module is first
1090referenced.
1091
1092\begin{center}
1093\begin{tabular}{|l|l|l|l|}
1094\hline
1095Code block type & Global name space & Local name space & Notes \\
1096\hline
1097Module & n.s. for this module & same as global & \\
1098Script & n.s. for \verb\__main__\ & same as global & \\
1099Interactive command & n.s. for \verb\__main__\ & same as global & \\
1100Class definition & global n.s. of containing block & new n.s. & \\
1101Function body & global n.s. of containing block & new n.s. & \\
1102String passed to \verb\exec\ or \verb\eval\
1103 & global n.s. of caller & local n.s. of caller & (1) \\
1104File read by \verb\execfile\
1105 & global n.s. of caller & local n.s. of caller & (1) \\
1106Expression read by \verb\input\
1107 & global n.s. of caller & local n.s. of caller & \\
1108\hline
1109\end{tabular}
1110\end{center}
1111
1112Notes:
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001113
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001114\begin{description}
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001115
1116\item[n.s.] means {\em name space}
1117
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001118\item[(1)] The global and local name space for these functions can be
1119overridden with optional extra arguments.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001120
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001121\end{description}
1122
1123\section{Exceptions}
1124
1125Exceptions are a means of breaking out of the normal flow of control
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001126of a code block in order to handle errors or other exceptional
1127conditions. An exception is {\em raised} at the point where the error
1128is detected; it may be {\em handled} by the surrounding code block or
1129by any code block that directly or indirectly invoked the code block
1130where the error occurred.
1131\index{exception}
1132\index{raise an exception}
1133\index{handle an exception}
1134\index{exception handler}
1135\index{errors}
1136\index{error handling}
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001137
1138The Python interpreter raises an exception when it detects an run-time
1139error (such as division by zero). A Python program can also
1140explicitly raise an exception with the \verb\raise\ statement.
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001141Exception handlers are specified with the \verb\try...except\
1142statement.
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001143
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001144Python uses the ``termination'' model of error handling: an exception
1145handler can find out what happened and continue execution at an outer
1146level, but it cannot repair the cause of the error and retry the
1147failing operation (except by re-entering the the offending piece of
1148code from the top).
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001149
1150When an exception is not handled at all, the interpreter terminates
1151execution of the program, or returns to its interactive main loop.
1152
1153Exceptions are identified by string objects. Two different string
1154objects with the same value identify different exceptions.
1155
1156When an exception is raised, an object (maybe \verb\None\) is passed
1157as the exception's ``parameter''; this object does not affect the
1158selection of an exception handler, but is passed to the selected
1159exception handler as additional information.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001160
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001161See also the description of the \verb\try\ and \verb\raise\
1162statements.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001163
Guido van Rossumf2612d11991-11-21 13:53:03 +00001164\chapter{Expressions and conditions}
1165
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001166In this and the following chapters, extended BNF notation will be used
1167to describe syntax, not lexical analysis.
1168\index{BNF}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001169
1170This chapter explains the meaning of the elements of expressions and
1171conditions. Conditions are a superset of expressions, and a condition
Guido van Rossum670e5a01992-01-17 14:03:20 +00001172may be used wherever an expression is required by enclosing it in
1173parentheses. The only places where expressions are used in the syntax
1174instead of conditions is in expression statements and on the
1175right-hand side of assignments; this catches some nasty bugs like
1176accedentally writing \verb\x == 1\ instead of \verb\x = 1\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001177
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001178The comma plays several roles in Python's syntax. It is usually an
Guido van Rossum743d1e71992-01-07 16:43:53 +00001179operator with a lower precedence than all others, but occasionally
Guido van Rossum670e5a01992-01-17 14:03:20 +00001180serves other purposes as well; e.g., it separates function arguments,
1181is used in list and dictionary constructors, and has special semantics
1182in \verb\print\ statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001183
1184When (one alternative of) a syntax rule has the form
1185
1186\begin{verbatim}
1187name: othername
1188\end{verbatim}
1189
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001190and no semantics are given, the semantics of this form of \verb\name\
1191are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001192
1193\section{Arithmetic conversions}
1194
1195When a description of an arithmetic operator below uses the phrase
1196``the numeric arguments are converted to a common type'',
1197this both means that if either argument is not a number, a
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001198\verb\TypeError\ exception is raised, and that otherwise
Guido van Rossumf2612d11991-11-21 13:53:03 +00001199the following conversions are applied:
1200
1201\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001202\item first, if either argument is a floating point number,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001203 the other is converted to floating point;
1204\item else, if either argument is a long integer,
1205 the other is converted to long integer;
Guido van Rossum670e5a01992-01-17 14:03:20 +00001206\item otherwise, both must be plain integers and no conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +00001207 is necessary.
1208\end{itemize}
1209
Guido van Rossumf2612d11991-11-21 13:53:03 +00001210\section{Atoms}
1211
Guido van Rossum670e5a01992-01-17 14:03:20 +00001212Atoms are the most basic elements of expressions. Forms enclosed in
1213reverse quotes or in parentheses, brackets or braces are also
1214categorized syntactically as atoms. The syntax for atoms is:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001215
1216\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001217atom: identifier | literal | enclosure
1218enclosure: parenth_form | list_display | dict_display | string_conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +00001219\end{verbatim}
1220
1221\subsection{Identifiers (Names)}
1222
1223An identifier occurring as an atom is a reference to a local, global
Guido van Rossum670e5a01992-01-17 14:03:20 +00001224or built-in name binding. If a name can be assigned to anywhere in a
1225code block, and is not mentioned in a \verb\global\ statement in that
1226code block, it refers to a local name throughout that code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001227Otherwise, it refers to a global name if one exists, else to a
1228built-in name.
1229
Guido van Rossum670e5a01992-01-17 14:03:20 +00001230When the name is bound to an object, evaluation of the atom yields
1231that object. When a name is not bound, an attempt to evaluate it
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001232raises a \verb\NameError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001233
1234\subsection{Literals}
1235
Guido van Rossum670e5a01992-01-17 14:03:20 +00001236Python knows string and numeric literals:
1237
1238\begin{verbatim}
1239literal: stringliteral | integer | longinteger | floatnumber
1240\end{verbatim}
1241
Guido van Rossumf2612d11991-11-21 13:53:03 +00001242Evaluation of a literal yields an object of the given type
1243(string, integer, long integer, floating point number)
1244with the given value.
1245The value may be approximated in the case of floating point literals.
1246
Guido van Rossum670e5a01992-01-17 14:03:20 +00001247All literals correspond to immutable data types, and hence the
1248object's identity is less important than its value. Multiple
1249evaluations of literals with the same value (either the same
1250occurrence in the program text or a different occurrence) may obtain
1251the same object or a different object with the same value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001252
1253(In the original implementation, all literals in the same code block
1254with the same type and value yield the same object.)
1255
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001256\subsection{Parenthesized forms}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001257
Guido van Rossum670e5a01992-01-17 14:03:20 +00001258A parenthesized form is an optional condition list enclosed in
1259parentheses:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001260
Guido van Rossum670e5a01992-01-17 14:03:20 +00001261\begin{verbatim}
1262parenth_form: "(" [condition_list] ")"
1263\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001264
Guido van Rossum670e5a01992-01-17 14:03:20 +00001265A parenthesized condition list yields whatever that condition list
1266yields.
1267
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001268An empty pair of parentheses yields an empty tuple object. Since
1269tuples are immutable, the rules for literals apply here.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001270
1271(Note that tuples are not formed by the parentheses, but rather by use
1272of the comma operator. The exception is the empty tuple, for which
Guido van Rossumb5e1c181992-03-06 10:52:59 +00001273parentheses {\em are} required --- allowing unparenthesized ``nothing''
Guido van Rossum670e5a01992-01-17 14:03:20 +00001274in expressions would causes ambiguities and allow common typos to
1275pass uncaught.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001276
1277\subsection{List displays}
1278
Guido van Rossum670e5a01992-01-17 14:03:20 +00001279A list display is a possibly empty series of conditions enclosed in
1280square brackets:
1281
1282\begin{verbatim}
1283list_display: "[" [condition_list] "]"
1284\end{verbatim}
1285
Guido van Rossumf2612d11991-11-21 13:53:03 +00001286A list display yields a new list object.
1287
1288If it has no condition list, the list object has no items.
1289Otherwise, the elements of the condition list are evaluated
1290from left to right and inserted in the list object in that order.
1291
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001292\subsection{Dictionary displays} \label{dict}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001293
Guido van Rossum670e5a01992-01-17 14:03:20 +00001294A dictionary display is a possibly empty series of key/datum pairs
1295enclosed in curly braces:
1296
1297\begin{verbatim}
1298dict_display: "{" [key_datum_list] "}"
1299key_datum_list: [key_datum ("," key_datum)* [","]
1300key_datum: condition ":" condition
1301\end{verbatim}
1302
Guido van Rossumf2612d11991-11-21 13:53:03 +00001303A dictionary display yields a new dictionary object.
1304
Guido van Rossum670e5a01992-01-17 14:03:20 +00001305The key/datum pairs are evaluated from left to right to define the
1306entries of the dictionary: each key object is used as a key into the
1307dictionary to store the corresponding datum.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001308
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001309Keys must be strings, otherwise a \verb\TypeError\ exception is raised.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001310Clashes between duplicate keys are not detected; the last datum
1311(textually rightmost in the display) stored for a given key value
1312prevails.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001313
1314\subsection{String conversions}
1315
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001316A string conversion is a condition list enclosed in reverse (or
Guido van Rossum670e5a01992-01-17 14:03:20 +00001317backward) quotes:
1318
1319\begin{verbatim}
1320string_conversion: "`" condition_list "`"
1321\end{verbatim}
1322
Guido van Rossumf2612d11991-11-21 13:53:03 +00001323A string conversion evaluates the contained condition list and converts the
1324resulting object into a string according to rules specific to its type.
1325
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001326If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossum670e5a01992-01-17 14:03:20 +00001327dictionary containing only objects whose type is one of these, the
1328resulting string is a valid Python expression which can be passed to
1329the built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +00001330same value (or an approximation, if floating point numbers are
1331involved).
1332
1333(In particular, converting a string adds quotes around it and converts
1334``funny'' characters to escape sequences that are safe to print.)
1335
Guido van Rossum670e5a01992-01-17 14:03:20 +00001336It is illegal to attempt to convert recursive objects (e.g., lists or
1337dictionaries that contain a reference to themselves, directly or
1338indirectly.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001339
1340\section{Primaries}
1341
1342Primaries represent the most tightly bound operations of the language.
1343Their syntax is:
1344
1345\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001346primary: atom | attributeref | subscription | slicing | call
Guido van Rossumf2612d11991-11-21 13:53:03 +00001347\end{verbatim}
1348
1349\subsection{Attribute references}
1350
Guido van Rossum670e5a01992-01-17 14:03:20 +00001351An attribute reference is a primary followed by a period and a name:
1352
1353\begin{verbatim}
1354attributeref: primary "." identifier
1355\end{verbatim}
1356
1357The primary must evaluate to an object of a type that supports
1358attribute references, e.g., a module or a list. This object is then
1359asked to produce the attribute whose name is the identifier. If this
1360attribute is not available, the exception \verb\AttributeError\ is
1361raised. Otherwise, the type and value of the object produced is
1362determined by the object. Multiple evaluations of the same attribute
1363reference may yield different objects.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001364
1365\subsection{Subscriptions}
1366
Guido van Rossum670e5a01992-01-17 14:03:20 +00001367A subscription selects an item of a sequence or mapping object:
1368
1369\begin{verbatim}
1370subscription: primary "[" condition "]"
1371\end{verbatim}
1372
1373The primary must evaluate to an object of a sequence or mapping type.
1374
1375If it is a mapping, the condition must evaluate to an object whose
1376value is one of the keys of the mapping, and the subscription selects
1377the value in the mapping that corresponds to that key.
1378
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001379If it is a sequence, the condition must evaluate to a plain integer.
1380If this value is negative, the length of the sequence is added to it
1381(so that, e.g., \verb\x[-1]\ selects the last item of \verb\x\.)
1382The resulting value must be a nonnegative integer smaller than the
1383number of items in the sequence, and the subscription selects the item
1384whose index is that value (counting from zero).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001385
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001386A string's items are characters. A character is not a separate data
Guido van Rossum670e5a01992-01-17 14:03:20 +00001387type but a string of exactly one character.
1388
Guido van Rossumf2612d11991-11-21 13:53:03 +00001389\subsection{Slicings}
1390
Guido van Rossum670e5a01992-01-17 14:03:20 +00001391A slicing selects a range of items in a sequence object:
1392
1393\begin{verbatim}
1394slicing: primary "[" [condition] ":" [condition] "]"
1395\end{verbatim}
1396
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001397The primary must evaluate to a sequence object. The lower and upper
1398bound expressions, if present, must evaluate to plain integers;
1399defaults are zero and the sequence's length, respectively. If either
1400bound is negative, the sequence's length is added to it. The slicing
1401now selects all items with index $k$ such that $i <= k < j$ where $i$
1402and $j$ are the specified lower and upper bounds. This may be an
1403empty sequence. It is not an error if $i$ or $j$ lie outside the
1404range of valid indexes (such items don't exist so they aren't
1405selected).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001406
1407\subsection{Calls}
1408
1409A call calls a function with a possibly empty series of arguments:
1410
1411\begin{verbatim}
1412call: primary "(" [condition_list] ")"
1413\end{verbatim}
1414
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001415The primary must evaluate to a callable object (user-defined
1416functions, built-in functions, methods of built-in objects, class
1417objects, and methods of class instances are callable). If it is a
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001418class, the argument list must be empty; otherwise, the arguments are
1419evaluated.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001420
Guido van Rossumcf8148b1992-03-02 16:13:50 +00001421A call always returns some value, possibly \verb\None\, unless it
1422raises an exception. How this value is computed depends on the type
1423of the callable object. If it is:
1424
1425\begin{description}
1426
1427\item[a user-defined function:] the code block for the function is
1428executed, passing it the argument list. The first thing the code
1429block will do is bind the formal parameters to the arguments. When
1430the code block executes a \verb\return\ statement, this specifies the
1431return value of the function call.
1432
1433\item[a built-in function or method:] the result is up to the
1434interpreter; see the library reference manual for the descriptions of
1435built-in functions and methods.
1436
1437\item[a class object:] a new instance of that class is returned.
1438
1439\item[a class instance method:] the corresponding user-defined
1440function is called, with an argument list that is one longer than the
1441argument list of the call: the instance becomes the first argument.
1442
1443\end{description}
Guido van Rossum670e5a01992-01-17 14:03:20 +00001444
Guido van Rossumf2612d11991-11-21 13:53:03 +00001445\section{Factors}
1446
1447Factors represent the unary numeric operators.
1448Their syntax is:
1449
1450\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001451factor: primary | "-" factor | "+" factor | "~" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001452\end{verbatim}
1453
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001454The unary \verb\"-"\ operator yields the negative of its
1455numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001456
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001457The unary \verb\"+"\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001458
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001459The unary \verb\"~"\ operator yields the bit-wise negation of its
1460plain or long integer argument. The bit-wise negation negation of
1461\verb\x\ is defined as \verb\-(x+1)\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001462
1463In all three cases, if the argument does not have the proper type,
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001464a \verb\TypeError\ exception is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001465
1466\section{Terms}
1467
1468Terms represent the most tightly binding binary operators:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001469%
Guido van Rossumf2612d11991-11-21 13:53:03 +00001470\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001471term: factor | term "*" factor | term "/" factor | term "%" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001472\end{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001473%
1474The \verb\"*"\ (multiplication) operator yields the product of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001475arguments. The arguments must either both be numbers, or one argument
1476must be a plain integer and the other must be a sequence. In the
1477former case, the numbers are converted to a common type and then
1478multiplied together. In the latter case, sequence repetition is
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001479performed; a negative repetition factor yields an empty sequence.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001480
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001481The \verb\"/"\ (division) operator yields the quotient of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001482arguments. The numeric arguments are first converted to a common
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001483type. Plain or long integer division yields an integer of the same
1484type; the result is that of mathematical division with the `floor'
1485function applied to the result. Division by zero raises the
1486\verb\ZeroDivisionError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001487
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001488The \verb\"%"\ (modulo) operator yields the remainder from the
Guido van Rossum670e5a01992-01-17 14:03:20 +00001489division of the first argument by the second. The numeric arguments
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001490are first converted to a common type. A zero right argument raises the
1491\verb\ZeroDivisionError\ exception. The arguments may be floating point
1492numbers, e.g., \verb\3.14 % 0.7\ equals \verb\0.34\. The modulo operator
Guido van Rossum670e5a01992-01-17 14:03:20 +00001493always yields a result with the same sign as its second operand (or
1494zero); the absolute value of the result is strictly smaller than the
1495second operand.
1496
1497The integer division and modulo operators are connected by the
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001498following identity: \verb\x == (x/y)*y + (x%y)\.
1499Integer division and modulo are also connected with the built-in
1500function \verb\divmod()\: \verb\divmod(x, y) == (x/y, x%y)\.
1501These identities don't hold for floating point numbers; there a
1502similar identity holds where \verb\x/y\ is replaced by
1503\verb\floor(x/y)\).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001504
1505\section{Arithmetic expressions}
1506
1507\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001508arith_expr: term | arith_expr "+" term | arith_expr "-" term
Guido van Rossumf2612d11991-11-21 13:53:03 +00001509\end{verbatim}
1510
Guido van Rossum670e5a01992-01-17 14:03:20 +00001511The \verb|"+"| operator yields the sum of its arguments. The
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001512arguments must either both be numbers, or both sequences of the same
1513type. In the former case, the numbers are converted to a common type
1514and then added together. In the latter case, the sequences are
1515concatenated.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001516
Guido van Rossum743d1e71992-01-07 16:43:53 +00001517The \verb|"-"| operator yields the difference of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001518The numeric arguments are first converted to a common type.
1519
1520\section{Shift expressions}
1521
1522\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001523shift_expr: arith_expr | shift_expr ( "<<" | ">>" ) arith_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001524\end{verbatim}
1525
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001526These operators accept plain or long integers as arguments. The
1527arguments are converted to a common type. They shift the first
1528argument to the left or right by the number of bits given by the
1529second argument.
1530
1531A right shift by $n$ bits is defined as division by $2^n$. A left
1532shift by $n$ bits is defined as multiplication with $2^n$ without
1533overflow check; for plain integers this drops bits if the result is
1534not less than $2^{31} - 1$ in absolute value.
1535
1536Negative shift counts raise a \verb\ValueError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001537
1538\section{Bitwise AND expressions}
1539
1540\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001541and_expr: shift_expr | and_expr "&" shift_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001542\end{verbatim}
1543
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001544This operator yields the bitwise AND of its arguments, which must be
1545plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001546
1547\section{Bitwise XOR expressions}
1548
1549\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001550xor_expr: and_expr | xor_expr "^" and_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001551\end{verbatim}
1552
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001553This operator yields the bitwise exclusive OR of its arguments, which
1554must be plain or long integers. The arguments are converted to a
1555common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001556
1557\section{Bitwise OR expressions}
1558
1559\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001560or_expr: xor_expr | or_expr "|" xor_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001561\end{verbatim}
1562
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001563This operator yields the bitwise OR of its arguments, which must be
1564plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001565
1566\section{Comparisons}
1567
1568\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001569comparison: or_expr (comp_operator or_expr)*
Guido van Rossum743d1e71992-01-07 16:43:53 +00001570comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001571\end{verbatim}
1572
1573Comparisons yield integer value: 1 for true, 0 for false.
1574
1575Comparisons can be chained arbitrarily,
1576e.g., $x < y <= z$ is equivalent to
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001577$x < y$ \verb\and\ $y <= z$, except that $y$ is evaluated only once
Guido van Rossumf2612d11991-11-21 13:53:03 +00001578(but in both cases $z$ is not evaluated at all when $x < y$ is
1579found to be false).
1580
1581Formally, $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 +00001582$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 +00001583$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
1584
1585Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
1586between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
1587
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001588The forms \verb\<>\ and \verb\!=\ are equivalent; for consistency with
1589C, \verb\!=\ is preferred; where \verb\!=\ is mentioned below
1590\verb\<>\ is also implied.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001591
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001592The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare
Guido van Rossumf2612d11991-11-21 13:53:03 +00001593the values of two objects. The objects needn't have the same type.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001594If both are numbers, they are coverted to a common type. Otherwise,
1595objects of different types {\em always} compare unequal, and are
1596ordered consistently but arbitrarily.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001597
1598(This unusual
1599definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001600operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001601
1602Comparison of objects of the same type depends on the type:
1603
1604\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001605
1606\item
1607Numbers are compared arithmetically.
1608
1609\item
1610Strings are compared lexicographically using the numeric equivalents
1611(the result of the built-in function \verb\ord\) of their characters.
1612
1613\item
1614Tuples and lists are compared lexicographically using comparison of
1615corresponding items.
1616
1617\item
1618Mappings (dictionaries) are compared through lexicographic
1619comparison of their sorted (key, value) lists.%
1620\footnote{This is expensive since it requires sorting the keys first,
1621but about the only sensible definition. It was tried to compare
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001622dictionaries using the rule below for most other types, but this gave
1623surprises in cases like \verb|if d == {}: ...|.}
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 Rossumf2612d11991-11-21 13:53:03 +00002316XXX Syntax for scripts, modules
Guido van Rossumcf8148b1992-03-02 16:13:50 +00002317XXX Syntax for interactive input, eval, exec, execfile, input
Guido van Rossum743d1e71992-01-07 16:43:53 +00002318XXX New definition of expressions (as conditions)
Guido van Rossumf2612d11991-11-21 13:53:03 +00002319
Guido van Rossumb5e1c181992-03-06 10:52:59 +00002320\input{ref.ind} % The index
2321
Guido van Rossumf2612d11991-11-21 13:53:03 +00002322\end{document}