blob: 61fe0db1660301070b5c3b65c2288fa8b0e0c76a [file] [log] [blame]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001% Format this file with latex.
Guido van Rossum7b632a61992-01-16 17:49:21 +00002
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00003\documentstyle[11pt,myformat]{report}
Guido van Rossumf2612d11991-11-21 13:53:03 +00004
5\title{\bf
Guido van Rossum255ad6e1992-01-28 18:10:46 +00006 Python Reference Manual
Guido van Rossumf2612d11991-11-21 13:53:03 +00007}
8
9\author{
10 Guido van Rossum \\
11 Dept. CST, CWI, Kruislaan 413 \\
12 1098 SJ Amsterdam, The Netherlands \\
13 E-mail: {\tt guido@cwi.nl}
14}
15
16\begin{document}
17
18\pagenumbering{roman}
19
20\maketitle
21
22\begin{abstract}
23
24\noindent
Guido van Rossum0f1f9da1992-01-20 17:10:21 +000025Python is a simple, yet powerful, interpreted programming language
26that bridges the gap between C and shell programming, and is thus
27ideally suited for ``throw-away programming'' and rapid prototyping.
28Its syntax is put together from constructs borrowed from a variety of
29other languages; most prominent are influences from ABC, C, Modula-3
30and Icon.
Guido van Rossumf2612d11991-11-21 13:53:03 +000031
32The Python interpreter is easily extended with new functions and data
33types implemented in C. Python is also suitable as an extension
34language for highly customizable C applications such as editors or
35window managers.
36
37Python is available for various operating systems, amongst which
38several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
39and MS-DOS.
40
41This reference manual describes the syntax and ``core semantics'' of
Guido van Rossum0f1f9da1992-01-20 17:10:21 +000042the language. It is terse, but attempts to be exact and complete.
43The semantics of non-essential built-in object types and of the
44built-in functions and modules are described in the {\em Python
45Library Reference}. For an informal introduction to the language, see
46the {\em Python Tutorial}.
Guido van Rossumf2612d11991-11-21 13:53:03 +000047
48\end{abstract}
49
50\pagebreak
51
Guido van Rossum670e5a01992-01-17 14:03:20 +000052{
53\parskip = 0mm
Guido van Rossumf2612d11991-11-21 13:53:03 +000054\tableofcontents
Guido van Rossum670e5a01992-01-17 14:03:20 +000055}
Guido van Rossumf2612d11991-11-21 13:53:03 +000056
57\pagebreak
58
59\pagenumbering{arabic}
60
61\chapter{Introduction}
62
63This reference manual describes the Python programming language.
64It is not intended as a tutorial.
65
Guido van Rossum743d1e71992-01-07 16:43:53 +000066While I am trying to be as precise as possible, I chose to use English
67rather than formal specifications for everything except syntax and
68lexical analysis. This should make the document better understandable
69to the average reader, but will leave room for ambiguities.
70Consequently, if you were coming from Mars and tried to re-implement
Guido van Rossum7b632a61992-01-16 17:49:21 +000071Python from this document alone, you might have to guess things and in
72fact you would be implementing quite a different language.
73On the other hand, if you are using
Guido van Rossum743d1e71992-01-07 16:43:53 +000074Python and wonder what the precise rules about a particular area of
Guido van Rossum7b632a61992-01-16 17:49:21 +000075the language are, you should definitely be able to find it here.
Guido van Rossum743d1e71992-01-07 16:43:53 +000076
77It is dangerous to add too many implementation details to a language
78reference document -- the implementation may change, and other
79implementations of the same language may work differently. On the
80other hand, there is currently only one Python implementation, and
Guido van Rossum7b632a61992-01-16 17:49:21 +000081its particular quirks are sometimes worth being mentioned, especially
82where the implementation imposes additional limitations.
Guido van Rossum743d1e71992-01-07 16:43:53 +000083
84Every Python implementation comes with a number of built-in and
85standard modules. These are not documented here, but in the separate
86{\em Python Library Reference} document. A few built-in modules are
87mentioned when they interact in a significant way with the language
88definition.
89
90\section{Notation}
91
92The descriptions of lexical analysis and syntax use a modified BNF
93grammar notation. This uses the following style of definition:
94
95\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +000096name: lc_letter (lc_letter | "_")*
97lc_letter: "a"..."z"
Guido van Rossum743d1e71992-01-07 16:43:53 +000098\end{verbatim}
99
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000100The first line says that a \verb\name\ is an \verb\lc_letter\ followed by
101a sequence of zero or more \verb\lc_letter\s and underscores. An
102\verb\lc_letter\ in turn is any of the single characters `a' through `z'.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000103(This rule is actually adhered to for the names defined in syntax and
104grammar rules in this document.)
105
106Each rule begins with a name (which is the name defined by the rule)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000107and a colon. A vertical bar
Guido van Rossum7b632a61992-01-16 17:49:21 +0000108(\verb\|\) is used to separate alternatives; it is the least binding
109operator in this notation. A star (\verb\*\) means zero or more
110repetitions of the preceding item; likewise, a plus (\verb\+\) means
111one or more repetitions, and a question mark (\verb\?\) zero or one
112(in other words, the preceding item is optional). These three
113operators bind as tightly as possible; parentheses are used for
Guido van Rossum743d1e71992-01-07 16:43:53 +0000114grouping. Literal strings are enclosed in double quotes. White space
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000115is only meaningful to separate tokens. Rules are normally contained
116on a single line; rules with many alternatives may be formatted
117alternatively with each line after the first beginning with a
118vertical bar.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000119
120In lexical definitions (as the example above), two more conventions
121are used: Two literal characters separated by three dots mean a choice
122of any single character in the given (inclusive) range of ASCII
123characters. A phrase between angular brackets (\verb\<...>\) gives an
124informal description of the symbol defined; e.g., this could be used
125to describe the notion of `control character' if needed.
126
Guido van Rossum7b632a61992-01-16 17:49:21 +0000127Even though the notation used is almost the same, there is a big
Guido van Rossum743d1e71992-01-07 16:43:53 +0000128difference between the meaning of lexical and syntactic definitions:
129a lexical definition operates on the individual characters of the
130input source, while a syntax definition operates on the stream of
131tokens generated by the lexical analysis.
132
Guido van Rossumf2612d11991-11-21 13:53:03 +0000133\chapter{Lexical analysis}
134
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000135A Python program is read by a {\em parser}. Input to the parser is a
136stream of {\em tokens}, generated by the {\em lexical analyzer}. This
137chapter describes how the lexical analyzer breaks a file into tokens.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000138
139\section{Line structure}
140
Guido van Rossum7b632a61992-01-16 17:49:21 +0000141A Python program is divided in a number of logical lines. The end of
142a logical line is represented by the token NEWLINE. Statements cannot
143cross logical line boundaries except where NEWLINE is allowed by the
144syntax (e.g., between statements in compound statements).
Guido van Rossumf2612d11991-11-21 13:53:03 +0000145
146\subsection{Comments}
147
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000148A comment starts with a hash character (\verb\#\) that is not part of
Guido van Rossum7b632a61992-01-16 17:49:21 +0000149a string literal, and ends at the end of the physical line. A comment
150always signifies the end of the logical line. Comments are ignored by
151the syntax.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000152
153\subsection{Line joining}
154
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000155Two or more physical lines may be joined into logical lines using
Guido van Rossum7b632a61992-01-16 17:49:21 +0000156backslash characters (\verb/\/), as follows: when a physical line ends
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000157in a backslash that is not part of a string literal or comment, it is
158joined with the following forming a single logical line, deleting the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000159backslash and the following end-of-line character. For example:
160%
161\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000162moth_names = ['Januari', 'Februari', 'Maart', \
163 'April', 'Mei', 'Juni', \
164 'Juli', 'Augustus', 'September', \
165 'Oktober', 'November', 'December']
Guido van Rossum670e5a01992-01-17 14:03:20 +0000166\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000167
168\subsection{Blank lines}
169
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000170A logical line that contains only spaces, tabs, and possibly a
171comment, is ignored (i.e., no NEWLINE token is generated), except that
172during interactive input of statements, an entirely blank logical line
173terminates a multi-line statement.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000174
175\subsection{Indentation}
176
Guido van Rossum7b632a61992-01-16 17:49:21 +0000177Leading whitespace (spaces and tabs) at the beginning of a logical
178line is used to compute the indentation level of the line, which in
179turn is used to determine the grouping of statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000180
Guido van Rossum7b632a61992-01-16 17:49:21 +0000181First, tabs are replaced (from left to right) by one to eight spaces
182such that the total number of characters up to there is a multiple of
183eight (this is intended to be the same rule as used by UNIX). The
184total number of spaces preceding the first non-blank character then
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000185determines the line's indentation. Indentation cannot be split over
186multiple physical lines using backslashes.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000187
188The indentation levels of consecutive lines are used to generate
189INDENT and DEDENT tokens, using a stack, as follows.
190
191Before the first line of the file is read, a single zero is pushed on
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000192the stack; this will never be popped off again. The numbers pushed on
193the stack will always be strictly increasing from bottom to top. At
194the beginning of each logical line, the line's indentation level is
195compared to the top of the stack. If it is equal, nothing happens.
196If it larger, it is pushed on the stack, and one INDENT token is
197generated. If it is smaller, it {\em must} be one of the numbers
198occurring on the stack; all numbers on the stack that are larger are
199popped off, and for each number popped off a DEDENT token is
200generated. At the end of the file, a DEDENT token is generated for
201each number remaining on the stack that is larger than zero.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000202
Guido van Rossum7b632a61992-01-16 17:49:21 +0000203Here is an example of a correctly (though confusingly) indented piece
204of Python code:
205
206\begin{verbatim}
207def perm(l):
Guido van Rossum670e5a01992-01-17 14:03:20 +0000208 # Compute the list of all permutations of l
209
Guido van Rossum7b632a61992-01-16 17:49:21 +0000210 if len(l) <= 1:
211 return [l]
212 r = []
213 for i in range(len(l)):
214 s = l[:i] + l[i+1:]
215 p = perm(s)
216 for x in p:
217 r.append(l[i:i+1] + x)
218 return r
219\end{verbatim}
220
221The following example shows various indentation errors:
222
223\begin{verbatim}
224 def perm(l): # error: first line indented
225 for i in range(len(l)): # error: not indented
226 s = l[:i] + l[i+1:]
227 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
228 for x in p:
229 r.append(l[i:i+1] + x)
230 return r # error: inconsistent indent
231\end{verbatim}
232
233(Actually, the first three errors are detected by the parser; only the
234last error is found by the lexical analyzer -- the indentation of
235\verb\return r\ does not match a level popped off the stack.)
236
Guido van Rossumf2612d11991-11-21 13:53:03 +0000237\section{Other tokens}
238
239Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
240exist: identifiers, keywords, literals, operators, and delimiters.
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000241Spaces and tabs are not tokens, but serve to delimit tokens. Where
242ambiguity exists, a token comprises the longest possible string that
243forms a legal token, when read from left to right.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000244
Guido van Rossumf2612d11991-11-21 13:53:03 +0000245\section{Identifiers}
246
247Identifiers are described by the following regular expressions:
248
249\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000250identifier: (letter|"_") (letter|digit|"_")*
Guido van Rossumf2612d11991-11-21 13:53:03 +0000251letter: lowercase | uppercase
Guido van Rossum743d1e71992-01-07 16:43:53 +0000252lowercase: "a"..."z"
253uppercase: "A"..."Z"
254digit: "0"..."9"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000255\end{verbatim}
256
Guido van Rossum670e5a01992-01-17 14:03:20 +0000257Identifiers are unlimited in length. Case is significant.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000258
Guido van Rossum670e5a01992-01-17 14:03:20 +0000259\subsection{Keywords}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000260
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000261The following identifiers are used as reserved words, or {\em
Guido van Rossum7b632a61992-01-16 17:49:21 +0000262keywords} of the language, and cannot be used as ordinary
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000263identifiers. They must be spelled exactly as written here:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000264
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000265\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000266and del for in print
267break elif from is raise
268class else global not return
269continue except if or try
270def finally import pass while
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000271\end{verbatim}
272
Guido van Rossum743d1e71992-01-07 16:43:53 +0000273% # This Python program sorts and formats the above table
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000274% import string
275% l = []
276% try:
277% while 1:
278% l = l + string.split(raw_input())
279% except EOFError:
280% pass
281% l.sort()
282% for i in range((len(l)+4)/5):
283% for j in range(i, len(l), 5):
284% print string.ljust(l[j], 10),
285% print
Guido van Rossumf2612d11991-11-21 13:53:03 +0000286
287\section{Literals}
288
289\subsection{String literals}
290
291String literals are described by the following regular expressions:
292
293\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000294stringliteral: "'" stringitem* "'"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000295stringitem: stringchar | escapeseq
Guido van Rossum743d1e71992-01-07 16:43:53 +0000296stringchar: <any ASCII character except newline or "\" or "'">
297escapeseq: "'" <any ASCII character except newline>
Guido van Rossumf2612d11991-11-21 13:53:03 +0000298\end{verbatim}
299
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000300String literals cannot span physical line boundaries. Escape
301sequences in strings are actually interpreted according to rules
302simular to those used by Standard C. The recognized escape sequences
303are:
304
305\begin{center}
306\begin{tabular}{|l|l|}
307\hline
308\verb/\\/ & Backslash (\verb/\/) \\
309\verb/\'/ & Single quote (\verb/'/) \\
310\verb/\a/ & ASCII Bell (BEL) \\
311\verb/\b/ & ASCII Backspace (BS) \\
Guido van Rossum7b632a61992-01-16 17:49:21 +0000312%\verb/\E/ & ASCII Escape (ESC) \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000313\verb/\f/ & ASCII Formfeed (FF) \\
314\verb/\n/ & ASCII Linefeed (LF) \\
315\verb/\r/ & ASCII Carriage Return (CR) \\
316\verb/\t/ & ASCII Horizontal Tab (TAB) \\
317\verb/\v/ & ASCII Vertical Tab (VT) \\
318\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
Guido van Rossum743d1e71992-01-07 16:43:53 +0000319\verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000320\hline
321\end{tabular}
322\end{center}
323
Guido van Rossum7b632a61992-01-16 17:49:21 +0000324In strict compatibility with in Standard C, up to three octal digits are
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000325accepted, but an unlimited number of hex digits is taken to be part of
326the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum7b632a61992-01-16 17:49:21 +0000327are used in all current implementations...).
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000328
Guido van Rossum7b632a61992-01-16 17:49:21 +0000329All unrecognized escape sequences are left in the string unchanged,
330i.e., {\em the backslash is left in the string.} (This rule is
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000331useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum743d1e71992-01-07 16:43:53 +0000332resulting output is more easily recognized as broken. It also helps a
333great deal for string literals used as regular expressions or
334otherwise passed to other modules that do their own escape handling --
335but you may end up quadrupling backslashes that must appear literally.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000336
337\subsection{Numeric literals}
338
Guido van Rossum670e5a01992-01-17 14:03:20 +0000339There are three types of numeric literals: plain integers, long
340integers, and floating point numbers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000341
342Integers and long integers are described by the following regular expressions:
343
344\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000345longinteger: integer ("l"|"L")
Guido van Rossumf2612d11991-11-21 13:53:03 +0000346integer: decimalinteger | octinteger | hexinteger
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000347decimalinteger: nonzerodigit digit* | "0"
348octinteger: "0" octdigit+
349hexinteger: "0" ("x"|"X") hexdigit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000350
Guido van Rossum743d1e71992-01-07 16:43:53 +0000351nonzerodigit: "1"..."9"
352octdigit: "0"..."7"
353hexdigit: digit|"a"..."f"|"A"..."F"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000354\end{verbatim}
355
Guido van Rossum670e5a01992-01-17 14:03:20 +0000356Although both lower case `l'and upper case `L' are allowed as suffix
357for long integers, it is strongly recommended to always use `L', since
358the letter `l' looks too much like the digit `1'.
359
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000360Plain integer decimal literals must be at most $2^{31} - 1$ (i.e., the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000361largest positive integer, assuming 32-bit arithmetic); octal and
362hexadecimal literals may be as large as $2^{32} - 1$. There is no limit
363for long integer literals.
364
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000365Some examples of plain and long integer literals:
Guido van Rossum670e5a01992-01-17 14:03:20 +0000366
367\begin{verbatim}
3687 2147483647 0177 0x80000000
3693L 79228162514264337593543950336L 0377L 0100000000L
370\end{verbatim}
371
Guido van Rossumf2612d11991-11-21 13:53:03 +0000372Floating point numbers are described by the following regular expressions:
373
374\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000375floatnumber: pointfloat | exponentfloat
376pointfloat: [intpart] fraction | intpart "."
377exponentfloat: (intpart | pointfloat) exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000378intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000379fraction: "." digit+
380exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000381\end{verbatim}
382
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000383The allowed range of floating point literals is
384implementation-dependent.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000385
386Some examples of floating point literals:
Guido van Rossum7b632a61992-01-16 17:49:21 +0000387
388\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00003893.14 10. .001 1e100 3.14e-10
Guido van Rossum7b632a61992-01-16 17:49:21 +0000390\end{verbatim}
391
Guido van Rossum670e5a01992-01-17 14:03:20 +0000392Note that numeric literals do not include a sign; a phrase like
393\verb\-1\ is actually an expression composed of the operator
Guido van Rossum7b632a61992-01-16 17:49:21 +0000394\verb\-\ and the literal \verb\1\.
395
Guido van Rossumf2612d11991-11-21 13:53:03 +0000396\section{Operators}
397
398The following tokens are operators:
399
400\begin{verbatim}
401+ - * / %
402<< >> & | ^ ~
Guido van Rossum743d1e71992-01-07 16:43:53 +0000403< == > <= <> != >=
Guido van Rossumf2612d11991-11-21 13:53:03 +0000404\end{verbatim}
405
Guido van Rossum743d1e71992-01-07 16:43:53 +0000406The comparison operators \verb\<>\ and \verb\!=\ are alternate
407spellings of the same operator.
408
Guido van Rossumf2612d11991-11-21 13:53:03 +0000409\section{Delimiters}
410
Guido van Rossum743d1e71992-01-07 16:43:53 +0000411The following tokens serve as delimiters or otherwise have a special
412meaning:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000413
414\begin{verbatim}
415( ) [ ] { }
Guido van Rossum743d1e71992-01-07 16:43:53 +0000416; , : . ` =
Guido van Rossumf2612d11991-11-21 13:53:03 +0000417\end{verbatim}
418
Guido van Rossum7b632a61992-01-16 17:49:21 +0000419The following printing ASCII characters are not used in Python (except
420in string literals and in comments). Their occurrence is an
421unconditional error:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000422
423\begin{verbatim}
424! @ $ " ?
425\end{verbatim}
426
Guido van Rossum7b632a61992-01-16 17:49:21 +0000427They may be used by future versions of the language though!
428
Guido van Rossumf2612d11991-11-21 13:53:03 +0000429\chapter{Execution model}
430
Guido van Rossum743d1e71992-01-07 16:43:53 +0000431\section{Objects, values and types}
432
433I won't try to define rigorously here what an object is, but I'll give
434some properties of objects that are important to know about.
435
436Every object has an identity, a type and a value. An object's {\em
437identity} never changes once it has been created; think of it as the
438object's (permanent) address. An object's {\em type} determines the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000439operations that an object supports (e.g., does it have a length?) and
440also defines the ``meaning'' of the object's value. The type also
441never changes. The {\em value} of some objects can change; whether
442this is possible is a property of its type.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000443
444Objects are never explicitly destroyed; however, when they become
Guido van Rossum670e5a01992-01-17 14:03:20 +0000445unreachable they may be garbage-collected. An implementation is
446allowed to delay garbage collection or omit it altogether -- it is a
447matter of implementation quality how garbage collection is
448implemented, as long as no objects are collected that are still
449reachable. (Implementation note: the current implementation uses a
Guido van Rossum743d1e71992-01-07 16:43:53 +0000450reference-counting scheme which collects most objects as soon as they
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000451become unreachable, but never collects garbage containing circular
Guido van Rossum743d1e71992-01-07 16:43:53 +0000452references.)
453
Guido van Rossum670e5a01992-01-17 14:03:20 +0000454Note that the use of the implementation's tracing or debugging
455facilities may keep objects alive that would normally be collectable.
456
Guido van Rossum743d1e71992-01-07 16:43:53 +0000457(Some objects contain references to ``external'' resources such as
458open files. It is understood that these resources are freed when the
459object is garbage-collected, but since garbage collection is not
Guido van Rossum670e5a01992-01-17 14:03:20 +0000460guaranteed, such objects also provide an explicit way to release the
461external resource (e.g., a \verb\close\ method). Programs are strongly
Guido van Rossum743d1e71992-01-07 16:43:53 +0000462recommended to use this.)
463
464Some objects contain references to other objects. These references
465are part of the object's value; in most cases, when such a
466``container'' object is compared to another (of the same type), the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000467comparison applies to the {\em values} of the referenced objects (not
468their identities).
Guido van Rossum743d1e71992-01-07 16:43:53 +0000469
Guido van Rossum670e5a01992-01-17 14:03:20 +0000470Types affect almost all aspects of objects.
471Even object identity is affected in some sense: for immutable
Guido van Rossum743d1e71992-01-07 16:43:53 +0000472types, operations that compute new values may actually return a
Guido van Rossum670e5a01992-01-17 14:03:20 +0000473reference to any existing object with the same type and value, while
Guido van Rossum743d1e71992-01-07 16:43:53 +0000474for mutable objects this is not allowed. E.g., after
475
476\begin{verbatim}
477a = 1; b = 1; c = []; d = []
478\end{verbatim}
479
480\verb\a\ and \verb\b\ may or may not refer to the same object, but
481\verb\c\ and \verb\d\ are guaranteed to refer to two different, unique,
482newly created lists.
483
484\section{Execution frames, name spaces, and scopes}
485
Guido van Rossum670e5a01992-01-17 14:03:20 +0000486XXX code blocks, scopes, name spaces, name binding, exceptions
Guido van Rossumf2612d11991-11-21 13:53:03 +0000487
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000488\chapter{The standard type hierarchy}
489
490The following types are built into Python. Extension modules
491written in C can define additional types. Future versions of Python
492may also add types to the type hierarchy (e.g., rational or complex
493numbers, lists of efficiently stored integers, etc.).
494
495\begin{description}
496
497\item[None]
498This type has a single value. There is a single object with this value.
499This object is accessed through the built-in name \verb\None\.
500It is returned from functions that don't explicitly return an object.
501
502\item[Numbers]
503These are created by numeric literals and returned as results
504by arithmetic operators and arithmetic built-in functions.
505Numeric objects are immutable; once created their value never changes.
506Python numbers are of course strongly related to mathematical numbers,
507but subject to the limitations of numerical representation in computers.
508
509Python distinguishes between integers and floating point numbers:
510
511\begin{description}
512\item[Integers]
513These represent elements from the mathematical set of whole numbers.
514
515There are two types of integers:
516
517\begin{description}
518
519\item[Plain integers]
520These represent numbers in the range $-2^{31}$ through $2^{31}-1$.
521(The range may be larger on machines with a larger natural word
522size, but not smaller.)
523When the result of an operation falls outside this range, the
524exception \verb\OverflowError\ is raised.
525For the purpose of shift and mask operations, integers are assumed to
526have a binary, 2's complement notation using 32 or more bits, and
527hiding no bits from the user (i.e., all $2^{32}$ different bit
528patterns correspond to different values).
529
530\item[Long integers]
531These represent numbers in an unlimited range, subject to avaiable
532(virtual) memory only. For the purpose of shift and mask operations,
533a binary representation is assumed, and negative numbers are
534represented in a variant of 2's complement which gives the illusion of
535an infinite string of sign bits extending to the left.
536
537\end{description} % Integers
538
539The rules for integer representation are intended to give the most
540meaningful interpretation of shift and mask operations involving
541negative integers and the least surprises when switching between the
542plain and long integer domains. For any operation except left shift,
543if it yields a result in the plain integer domain without causing
544overflow, it will yield the same result in the long integer domain or
545when using mixed operands.
546
547\item[Floating point numbers]
548These represent machine-level double precision floating point numbers.
549You are at the mercy of the underlying machine architecture and
550C implementation for the accepted range and handling of overflow.
551
552\end{description} % Numbers
553
554\item[Sequences]
555These represent finite ordered sets indexed by natural numbers.
556The built-in function \verb\len()\ returns the number of elements
557of a sequence. When this number is $n$, the index set contains
558the numbers $0, 1, \ldots, n-1$. Element \verb\i\ of sequence
559\verb\a\ is selected by \verb\a[i]\.
560
561Sequences also support slicing: \verb\a[i:j]\ selects all elements
562with index $k$ such that $i < k < j$. When used as an expression,
563a slice is a sequence of the same type -- this implies that the
564index set is renumbered so that it starts at 0 again.
565
566Sequences are distinguished according to their mutability:
567
568\begin{description}
569%
570\item[Immutable sequences]
571An object of an immutable sequence type cannot change once it is
572created. (If the object contains references to other objects,
573these other objects may be mutable and may be changed; however
574the collection of objects directly referenced by an immutable object
575cannot change.)
576
577The following types are immutable sequences:
578
579\begin{description}
580
581\item[Strings]
582The elements of a string are characters. There is no separate
583character type; a character is represented by a string of one element.
584Characters represent (at least) 8-bit bytes. The built-in
585functions \verb\chr()\ and \verb\ord()\ convert between characters
586and nonnegative integers representing the byte values.
587Bytes with the values 0-127 represent the corresponding ASCII values.
588
589(On systems whose native character set is not ASCII, strings may use
590EBCDIC in their internal representation, provided the functions
591\verb\chr()\ and \verb\ord()\ implement a mapping between ASCII and
592EBCDIC, and string comparisons preserve the ASCII order.
593Or perhaps someone can propose a better rule?)
594
595\item[Tuples]
596The elements of a tuple are arbitrary Python objects.
597Tuples of two or more elements are formed by comma-separated lists
598of expressions. A tuple of one element can be formed by affixing
599a comma to an expression (an expression by itself of course does
600not create a tuple). An empty tuple can be formed by enclosing
601`nothing' in parentheses.
602
603\end{description} % Immutable sequences
604
605\item[Mutable sequences]
606Mutable sequences can be changed after they are created.
607The subscript and slice notations can be used as the target
608of assignment and \verb\del\ (delete) statements.
609
610There is currently a single mutable sequence type:
611
612\begin{description}
613
614\item[Lists]
615The elements of a list are arbitrary Python objects.
616Lists are formed by placing a comma-separated list of expressions
617in square brackets. (Note that there are no special cases for lists
618of length 0 or 1.)
619
620\end{description} % Mutable sequences
621
622\end{description} % Sequences
623
624\item[Mapping types]
625These represent finite sets of objects indexed by arbitrary index sets.
626The subscript notation \verb\a[k]\ selects the element indexed
627by \verb\k\ from the mapping \verb\a\; this can be used in
628expressions and as the target of assignments or \verb\del\ statements.
629The built-in function \verb\len()\ returns the number of elements
630in a mapping.
631
632There is currently a single mapping type:
633
634\begin{description}
635
636\item[Dictionaries]
637These represent finite sets of objects indexed by strings.
638Dictionaries are created by the \verb\{...}\ notation (see section
639\ref{dict}). (Implementation note: the strings used for indexing must
640not contain null bytes.)
641
642\end{description} % Mapping types
643
644\item[Callable types]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000645These are the types to which the function call operation (written as
646\verb\function(argument, argument, ...)\) can be applied:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000647
648\begin{description}
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000649
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000650\item[User-defined functions]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000651A user-defined function is created by a function definition (starting
652with the \verb\def\ keyword). It should be called with an argument
653list containing the same number of items as the function's
654formal parameter list.
655
656Special read-only attributes: \verb\func_code\ is the code object
657representing the compiled function body, and \verb\func_globals\ is (a
658reference to) the dictionary that holds the function's global
659variables -- it implements the global name space of the module in
660which the function was defined.
661
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000662\item[User-defined methods]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000663A user-defined method (a.k.a. {\tt object closure}) is a pair of a
664class instance object and a user-defined function. It should be
665called with an argument list containing one item less than the number
666of items in the function's formal parameter list. When called, the
667class instance becomes the first argument, and the call arguments are
668shifted one to the right.
669
670Special read-only attributes: \verb\im_self\ is the class instance
671object, \verb\im_func\ is the function object.
672
673\item[Built-in functions]
674A built-in function object is a wrapper around a C function. Examples
675of built-in functions are \verb\len\ and \verb\math.sin\. There
676are no special attributes. The number and type of the arguments are
677determined by the C function.
678
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000679\item[Built-in methods]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000680This is really a different disguise of a built-in function, this time
681containing an object passed to the C function as an implicit extra
682argument. An example of a built-in method is \verb\list.append\ if
683\verb\list\ is a list object.
684
685\item[Classes]
686Class objects are described below. When a class object is called as a
687parameterless function, a new class instance (also described below) is
688created and returned. The class's initialization function is not
689called -- this is the responsibility of the caller. It is illegal to
690call a class object with one or more arguments.
691
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000692\end{description}
693
694\item[Modules]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000695A module object is a container for a module's name space, which is a
696dictionary (the same dictionary as referenced by the
697\ver\func_globals\ attribute of functions defined in the module).
698Module attribute references are translated to lookups in this
699dictionary. A module object does not contain the code object used to
700initialize the module (since it isn't needed once the initialization
701is done).
702
703There are two special read-only attributes: \verb\__dict__\ yields the
704module's name space as a dictionary object; \verb\__name__\ yields the
705module's name.
706
707\item[Classes]
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000708XXX
709
710\item[Class instances]
711XXX
712
713\item[Files]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000714A file object represents an open file. (It is a wrapper around a C
715{\tt stdio} file pointer.) File objects are created by the
716\verb\open()\ built-in function, and also by \verb\posix.popen()\ and
717the \verb\makefile\ method of socket objects. \verb\sys.stdin\,
718\verb\sys.stdout\ and \verb\sys.stderr\ are file objects corresponding
719the the interpreter's standard input, output and error streams.
720See the Python Library Reference for methods of file objects and other
721details.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000722
723\item[Internal types]
724A few types used internally by the interpreter are exposed to the user.
725Their definition may change with future versions of the interpreter,
726but they are mentioned here for completeness.
727
728\begin{description}
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000729
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000730\item[Code objects]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000731Code objects represent executable code. The difference between a code
732object and a function object is that the function object contains an
733explicit reference to the function's context (the module in which it
734was defined) which a code object contains no context. There is no way
735to execute a bare code object.
736
737Special read-only attributes: \verb\co_code\ is a string representing
738the sequence of instructions; \verb\co_consts\ is a list of literals
739used by the code; \verb\co_names\ is a list of names (strings) used by
740the code; \verb\co_filename\ is the filename from which the code was
741compiled. (To find out the line numbers, you would have to decode the
742instructions; the standard library module \verb\dis\ contains an
743example of how to do this.)
744
745\item[Frame objects]
746Frame objects represent execution frames. They may occur in traceback
747objects (see below).
748
749Special read-only attributes: \verb\f_back\ is to the previous
750stack frame (towards the caller), or \verb\None\ if this is the bottom
751stack frame; \verb\f_code\ is the code object being executed in this
752frame; \verb\f_globals\ is the dictionary used to look up global
753variables; \verb\f_locals\ is used for local variables;
754\verb\f_lineno\ gives the line number and \verb\f_lasti\ gives the
755precise instruction (this is an index into the instruction string of
756the code object).
757
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000758\item[Traceback objects]
Guido van Rossum255ad6e1992-01-28 18:10:46 +0000759Traceback objects represent a stack trace of an exception. A
760traceback object is created when an exception occurs. When the search
761for an exception handler unwinds the execution stack, at each unwound
762level a traceback object is inserted in front of the current
763traceback. When an exception handler is entered, the stack trace is
764made available to the program as \verb\sys.exc_traceback\. When the
765program contains no suitable handler, the stack trace is written
766(nicely formatted) to the standard error stream; if the interpreter is
767interactive, it is made available to the user as
768\verb\sys.last_traceback\.
769
770Special read-only attributes: \verb\tb_next\ is the next level in the
771stack trace (towards the frame where the exception occurred), or
772\verb\None\ if there is no next level; \verb\tb_frame\ points to the
773execution frame of the current level; \verb\tb_lineno\ gives the line
774number where the exception occurred; \verb\tb_lasti\ indicates the
775precise instruction. The line number and last instruction in the
776traceback may differ from the line number of its frame object if the
777exception occurred in a \verb\try\ statement with no matching
778\verb\except\ clause or with a \verb\finally\ clause.
779
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000780\end{description} % Internal types
781
782\end{description} % Types
783
Guido van Rossumf2612d11991-11-21 13:53:03 +0000784\chapter{Expressions and conditions}
785
Guido van Rossum743d1e71992-01-07 16:43:53 +0000786From now on, extended BNF notation will be used to describe syntax,
787not lexical analysis.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000788
789This chapter explains the meaning of the elements of expressions and
790conditions. Conditions are a superset of expressions, and a condition
Guido van Rossum670e5a01992-01-17 14:03:20 +0000791may be used wherever an expression is required by enclosing it in
792parentheses. The only places where expressions are used in the syntax
793instead of conditions is in expression statements and on the
794right-hand side of assignments; this catches some nasty bugs like
795accedentally writing \verb\x == 1\ instead of \verb\x = 1\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000796
Guido van Rossum670e5a01992-01-17 14:03:20 +0000797The comma has several roles in Python's syntax. It is usually an
Guido van Rossum743d1e71992-01-07 16:43:53 +0000798operator with a lower precedence than all others, but occasionally
Guido van Rossum670e5a01992-01-17 14:03:20 +0000799serves other purposes as well; e.g., it separates function arguments,
800is used in list and dictionary constructors, and has special semantics
801in \verb\print\ statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000802
803When (one alternative of) a syntax rule has the form
804
805\begin{verbatim}
806name: othername
807\end{verbatim}
808
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000809and no semantics are given, the semantics of this form of \verb\name\
810are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000811
812\section{Arithmetic conversions}
813
814When a description of an arithmetic operator below uses the phrase
815``the numeric arguments are converted to a common type'',
816this both means that if either argument is not a number, a
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000817\verb\TypeError\ exception is raised, and that otherwise
Guido van Rossumf2612d11991-11-21 13:53:03 +0000818the following conversions are applied:
819
820\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000821\item first, if either argument is a floating point number,
Guido van Rossumf2612d11991-11-21 13:53:03 +0000822 the other is converted to floating point;
823\item else, if either argument is a long integer,
824 the other is converted to long integer;
Guido van Rossum670e5a01992-01-17 14:03:20 +0000825\item otherwise, both must be plain integers and no conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +0000826 is necessary.
827\end{itemize}
828
Guido van Rossumf2612d11991-11-21 13:53:03 +0000829\section{Atoms}
830
Guido van Rossum670e5a01992-01-17 14:03:20 +0000831Atoms are the most basic elements of expressions. Forms enclosed in
832reverse quotes or in parentheses, brackets or braces are also
833categorized syntactically as atoms. The syntax for atoms is:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000834
835\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000836atom: identifier | literal | enclosure
837enclosure: parenth_form | list_display | dict_display | string_conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +0000838\end{verbatim}
839
840\subsection{Identifiers (Names)}
841
842An identifier occurring as an atom is a reference to a local, global
Guido van Rossum670e5a01992-01-17 14:03:20 +0000843or built-in name binding. If a name can be assigned to anywhere in a
844code block, and is not mentioned in a \verb\global\ statement in that
845code block, it refers to a local name throughout that code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000846Otherwise, it refers to a global name if one exists, else to a
847built-in name.
848
Guido van Rossum670e5a01992-01-17 14:03:20 +0000849When the name is bound to an object, evaluation of the atom yields
850that object. When a name is not bound, an attempt to evaluate it
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000851raises a \verb\NameError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000852
853\subsection{Literals}
854
Guido van Rossum670e5a01992-01-17 14:03:20 +0000855Python knows string and numeric literals:
856
857\begin{verbatim}
858literal: stringliteral | integer | longinteger | floatnumber
859\end{verbatim}
860
Guido van Rossumf2612d11991-11-21 13:53:03 +0000861Evaluation of a literal yields an object of the given type
862(string, integer, long integer, floating point number)
863with the given value.
864The value may be approximated in the case of floating point literals.
865
Guido van Rossum670e5a01992-01-17 14:03:20 +0000866All literals correspond to immutable data types, and hence the
867object's identity is less important than its value. Multiple
868evaluations of literals with the same value (either the same
869occurrence in the program text or a different occurrence) may obtain
870the same object or a different object with the same value.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000871
872(In the original implementation, all literals in the same code block
873with the same type and value yield the same object.)
874
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000875\subsection{Parenthesized forms}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000876
Guido van Rossum670e5a01992-01-17 14:03:20 +0000877A parenthesized form is an optional condition list enclosed in
878parentheses:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000879
Guido van Rossum670e5a01992-01-17 14:03:20 +0000880\begin{verbatim}
881parenth_form: "(" [condition_list] ")"
882\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000883
Guido van Rossum670e5a01992-01-17 14:03:20 +0000884A parenthesized condition list yields whatever that condition list
885yields.
886
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000887An empty pair of parentheses yields an empty tuple object. Since
888tuples are immutable, the rules for literals apply here.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000889
890(Note that tuples are not formed by the parentheses, but rather by use
891of the comma operator. The exception is the empty tuple, for which
892parentheses {\em are} required -- allowing unparenthesized ``nothing''
893in expressions would causes ambiguities and allow common typos to
894pass uncaught.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000895
896\subsection{List displays}
897
Guido van Rossum670e5a01992-01-17 14:03:20 +0000898A list display is a possibly empty series of conditions enclosed in
899square brackets:
900
901\begin{verbatim}
902list_display: "[" [condition_list] "]"
903\end{verbatim}
904
Guido van Rossumf2612d11991-11-21 13:53:03 +0000905A list display yields a new list object.
906
907If it has no condition list, the list object has no items.
908Otherwise, the elements of the condition list are evaluated
909from left to right and inserted in the list object in that order.
910
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000911\subsection{Dictionary displays} \label{dict}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000912
Guido van Rossum670e5a01992-01-17 14:03:20 +0000913A dictionary display is a possibly empty series of key/datum pairs
914enclosed in curly braces:
915
916\begin{verbatim}
917dict_display: "{" [key_datum_list] "}"
918key_datum_list: [key_datum ("," key_datum)* [","]
919key_datum: condition ":" condition
920\end{verbatim}
921
Guido van Rossumf2612d11991-11-21 13:53:03 +0000922A dictionary display yields a new dictionary object.
923
Guido van Rossum670e5a01992-01-17 14:03:20 +0000924The key/datum pairs are evaluated from left to right to define the
925entries of the dictionary: each key object is used as a key into the
926dictionary to store the corresponding datum.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000927
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000928Keys must be strings, otherwise a \verb\TypeError\ exception is raised.
Guido van Rossum670e5a01992-01-17 14:03:20 +0000929Clashes between duplicate keys are not detected; the last datum
930(textually rightmost in the display) stored for a given key value
931prevails.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000932
933\subsection{String conversions}
934
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000935A string conversion is a condition list enclosed in reverse (or
Guido van Rossum670e5a01992-01-17 14:03:20 +0000936backward) quotes:
937
938\begin{verbatim}
939string_conversion: "`" condition_list "`"
940\end{verbatim}
941
Guido van Rossumf2612d11991-11-21 13:53:03 +0000942A string conversion evaluates the contained condition list and converts the
943resulting object into a string according to rules specific to its type.
944
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000945If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossum670e5a01992-01-17 14:03:20 +0000946dictionary containing only objects whose type is one of these, the
947resulting string is a valid Python expression which can be passed to
948the built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +0000949same value (or an approximation, if floating point numbers are
950involved).
951
952(In particular, converting a string adds quotes around it and converts
953``funny'' characters to escape sequences that are safe to print.)
954
Guido van Rossum670e5a01992-01-17 14:03:20 +0000955It is illegal to attempt to convert recursive objects (e.g., lists or
956dictionaries that contain a reference to themselves, directly or
957indirectly.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000958
959\section{Primaries}
960
961Primaries represent the most tightly bound operations of the language.
962Their syntax is:
963
964\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000965primary: atom | attributeref | subscription | slicing | call
Guido van Rossumf2612d11991-11-21 13:53:03 +0000966\end{verbatim}
967
968\subsection{Attribute references}
969
Guido van Rossum670e5a01992-01-17 14:03:20 +0000970An attribute reference is a primary followed by a period and a name:
971
972\begin{verbatim}
973attributeref: primary "." identifier
974\end{verbatim}
975
976The primary must evaluate to an object of a type that supports
977attribute references, e.g., a module or a list. This object is then
978asked to produce the attribute whose name is the identifier. If this
979attribute is not available, the exception \verb\AttributeError\ is
980raised. Otherwise, the type and value of the object produced is
981determined by the object. Multiple evaluations of the same attribute
982reference may yield different objects.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000983
984\subsection{Subscriptions}
985
Guido van Rossum670e5a01992-01-17 14:03:20 +0000986A subscription selects an item of a sequence or mapping object:
987
988\begin{verbatim}
989subscription: primary "[" condition "]"
990\end{verbatim}
991
992The primary must evaluate to an object of a sequence or mapping type.
993
994If it is a mapping, the condition must evaluate to an object whose
995value is one of the keys of the mapping, and the subscription selects
996the value in the mapping that corresponds to that key.
997
Guido van Rossum0f1f9da1992-01-20 17:10:21 +0000998If it is a sequence, the condition must evaluate to a plain integer.
999If this value is negative, the length of the sequence is added to it
1000(so that, e.g., \verb\x[-1]\ selects the last item of \verb\x\.)
1001The resulting value must be a nonnegative integer smaller than the
1002number of items in the sequence, and the subscription selects the item
1003whose index is that value (counting from zero).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001004
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001005A string's items are characters. A character is not a separate data
Guido van Rossum670e5a01992-01-17 14:03:20 +00001006type but a string of exactly one character.
1007
Guido van Rossumf2612d11991-11-21 13:53:03 +00001008\subsection{Slicings}
1009
Guido van Rossum670e5a01992-01-17 14:03:20 +00001010A slicing selects a range of items in a sequence object:
1011
1012\begin{verbatim}
1013slicing: primary "[" [condition] ":" [condition] "]"
1014\end{verbatim}
1015
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001016The primary must evaluate to a sequence object. The lower and upper
1017bound expressions, if present, must evaluate to plain integers;
1018defaults are zero and the sequence's length, respectively. If either
1019bound is negative, the sequence's length is added to it. The slicing
1020now selects all items with index $k$ such that $i <= k < j$ where $i$
1021and $j$ are the specified lower and upper bounds. This may be an
1022empty sequence. It is not an error if $i$ or $j$ lie outside the
1023range of valid indexes (such items don't exist so they aren't
1024selected).
Guido van Rossum670e5a01992-01-17 14:03:20 +00001025
1026\subsection{Calls}
1027
1028A call calls a function with a possibly empty series of arguments:
1029
1030\begin{verbatim}
1031call: primary "(" [condition_list] ")"
1032\end{verbatim}
1033
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001034The primary must evaluate to a callable object (user-defined
1035functions, built-in functions, methods of built-in objects, class
1036objects, and methods of class instances are callable). If it is a
1037class, the argument list must be empty.
Guido van Rossum670e5a01992-01-17 14:03:20 +00001038
1039XXX explain what happens on function call
1040
Guido van Rossumf2612d11991-11-21 13:53:03 +00001041\section{Factors}
1042
1043Factors represent the unary numeric operators.
1044Their syntax is:
1045
1046\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001047factor: primary | "-" factor | "+" factor | "~" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001048\end{verbatim}
1049
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001050The unary \verb\"-"\ operator yields the negative of its
1051numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001052
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001053The unary \verb\"+"\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001054
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001055The unary \verb\"~"\ operator yields the bit-wise negation of its
1056plain or long integer argument. The bit-wise negation negation of
1057\verb\x\ is defined as \verb\-(x+1)\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001058
1059In all three cases, if the argument does not have the proper type,
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001060a \verb\TypeError\ exception is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001061
1062\section{Terms}
1063
1064Terms represent the most tightly binding binary operators:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001065%
Guido van Rossumf2612d11991-11-21 13:53:03 +00001066\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001067term: factor | term "*" factor | term "/" factor | term "%" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +00001068\end{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001069%
1070The \verb\"*"\ (multiplication) operator yields the product of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001071arguments. The arguments must either both be numbers, or one argument
1072must be a plain integer and the other must be a sequence. In the
1073former case, the numbers are converted to a common type and then
1074multiplied together. In the latter case, sequence repetition is
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001075performed; a negative repetition factor yields an empty sequence.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001076
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001077The \verb\"/"\ (division) operator yields the quotient of its
Guido van Rossum670e5a01992-01-17 14:03:20 +00001078arguments. The numeric arguments are first converted to a common
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001079type. Plain or long integer division yields an integer of the same
1080type; the result is that of mathematical division with the `floor'
1081function applied to the result. Division by zero raises the
1082\verb\ZeroDivisionError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001083
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001084The \verb\"%"\ (modulo) operator yields the remainder from the
Guido van Rossum670e5a01992-01-17 14:03:20 +00001085division of the first argument by the second. The numeric arguments
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001086are first converted to a common type. A zero right argument raises the
1087\verb\ZeroDivisionError\ exception. The arguments may be floating point
1088numbers, e.g., \verb\3.14 % 0.7\ equals \verb\0.34\. The modulo operator
Guido van Rossum670e5a01992-01-17 14:03:20 +00001089always yields a result with the same sign as its second operand (or
1090zero); the absolute value of the result is strictly smaller than the
1091second operand.
1092
1093The integer division and modulo operators are connected by the
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001094following identity: \verb\x == (x/y)*y + (x%y)\.
1095Integer division and modulo are also connected with the built-in
1096function \verb\divmod()\: \verb\divmod(x, y) == (x/y, x%y)\.
1097These identities don't hold for floating point numbers; there a
1098similar identity holds where \verb\x/y\ is replaced by
1099\verb\floor(x/y)\).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001100
1101\section{Arithmetic expressions}
1102
1103\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001104arith_expr: term | arith_expr "+" term | arith_expr "-" term
Guido van Rossumf2612d11991-11-21 13:53:03 +00001105\end{verbatim}
1106
Guido van Rossum670e5a01992-01-17 14:03:20 +00001107The \verb|"+"| operator yields the sum of its arguments. The
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001108arguments must either both be numbers, or both sequences of the same
1109type. In the former case, the numbers are converted to a common type
1110and then added together. In the latter case, the sequences are
1111concatenated.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001112
Guido van Rossum743d1e71992-01-07 16:43:53 +00001113The \verb|"-"| operator yields the difference of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001114The numeric arguments are first converted to a common type.
1115
1116\section{Shift expressions}
1117
1118\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001119shift_expr: arith_expr | shift_expr ( "<<" | ">>" ) arith_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001120\end{verbatim}
1121
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001122These operators accept plain or long integers as arguments. The
1123arguments are converted to a common type. They shift the first
1124argument to the left or right by the number of bits given by the
1125second argument.
1126
1127A right shift by $n$ bits is defined as division by $2^n$. A left
1128shift by $n$ bits is defined as multiplication with $2^n$ without
1129overflow check; for plain integers this drops bits if the result is
1130not less than $2^{31} - 1$ in absolute value.
1131
1132Negative shift counts raise a \verb\ValueError\ exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001133
1134\section{Bitwise AND expressions}
1135
1136\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001137and_expr: shift_expr | and_expr "&" shift_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001138\end{verbatim}
1139
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001140This operator yields the bitwise AND of its arguments, which must be
1141plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001142
1143\section{Bitwise XOR expressions}
1144
1145\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001146xor_expr: and_expr | xor_expr "^" and_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001147\end{verbatim}
1148
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001149This operator yields the bitwise exclusive OR of its arguments, which
1150must be plain or long integers. The arguments are converted to a
1151common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001152
1153\section{Bitwise OR expressions}
1154
1155\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001156or_expr: xor_expr | or_expr "|" xor_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +00001157\end{verbatim}
1158
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001159This operator yields the bitwise OR of its arguments, which must be
1160plain or long integers. The arguments are converted to a common type.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001161
1162\section{Comparisons}
1163
1164\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001165comparison: or_expr (comp_operator or_expr)*
Guido van Rossum743d1e71992-01-07 16:43:53 +00001166comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001167\end{verbatim}
1168
1169Comparisons yield integer value: 1 for true, 0 for false.
1170
1171Comparisons can be chained arbitrarily,
1172e.g., $x < y <= z$ is equivalent to
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001173$x < y$ \verb\and\ $y <= z$, except that $y$ is evaluated only once
Guido van Rossumf2612d11991-11-21 13:53:03 +00001174(but in both cases $z$ is not evaluated at all when $x < y$ is
1175found to be false).
1176
1177Formally, $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 +00001178$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 +00001179$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
1180
1181Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
1182between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
1183
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001184The forms \verb\<>\ and \verb\!=\ are equivalent; for consistency with
1185C, \verb\!=\ is preferred; where \verb\!=\ is mentioned below
1186\verb\<>\ is also implied.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001187
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001188The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare
Guido van Rossumf2612d11991-11-21 13:53:03 +00001189the values of two objects. The objects needn't have the same type.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001190If both are numbers, they are coverted to a common type. Otherwise,
1191objects of different types {\em always} compare unequal, and are
1192ordered consistently but arbitrarily.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001193
1194(This unusual
1195definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001196operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001197
1198Comparison of objects of the same type depends on the type:
1199
1200\begin{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001201
1202\item
1203Numbers are compared arithmetically.
1204
1205\item
1206Strings are compared lexicographically using the numeric equivalents
1207(the result of the built-in function \verb\ord\) of their characters.
1208
1209\item
1210Tuples and lists are compared lexicographically using comparison of
1211corresponding items.
1212
1213\item
1214Mappings (dictionaries) are compared through lexicographic
1215comparison of their sorted (key, value) lists.%
1216\footnote{This is expensive since it requires sorting the keys first,
1217but about the only sensible definition. It was tried to compare
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001218dictionaries using the rule below for most other types, but this gave
1219surprises in cases like \verb|if d == {}: ...|.}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001220
1221\item
1222Most other types compare unequal unless they are the same object;
1223the choice whether one object is considered smaller or larger than
1224another one is made arbitrarily but consistently within one
1225execution of a program.
1226
Guido van Rossumf2612d11991-11-21 13:53:03 +00001227\end{itemize}
1228
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001229The operators \verb\in\ and \verb\not in\ test for sequence
1230membership: if $y$ is a sequence, $x ~\verb\in\~ y$ is true if and
1231only if there exists an index $i$ such that $x = y[i]$.
1232$x ~\verb\not in\~ y$ yields the inverse truth value. The exception
1233\verb\TypeError\ is raised when $y$ is not a sequence, or when $y$ is
1234a string and $x$ is not a string of length one.%
1235\footnote{The latter restriction is sometimes a nuisance.}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001236
1237The operators \verb\is\ and \verb\is not\ compare object identity:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001238$x ~\verb\is\~ y$ is true if and only if $x$ and $y$ are the same
1239object. $x ~\verb\is not\~ y$ yields the inverse truth value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001240
1241\section{Boolean operators}
1242
1243\begin{verbatim}
1244condition: or_test
Guido van Rossum743d1e71992-01-07 16:43:53 +00001245or_test: and_test | or_test "or" and_test
1246and_test: not_test | and_test "and" not_test
1247not_test: comparison | "not" not_test
Guido van Rossumf2612d11991-11-21 13:53:03 +00001248\end{verbatim}
1249
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001250In the context of Boolean operators, and also when conditions are used
1251by control flow statements, the following values are interpreted as
1252false: \verb\None\, numeric zero of all types, empty sequences
1253(strings, tuples and lists), and empty mappings (dictionaries). All
1254other values are interpreted as true.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001255
1256The operator \verb\not\ yields 1 if its argument is false, 0 otherwise.
1257
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001258The condition $x ~\verb\and\~ y$ first evaluates $x$; if $x$ is false,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001259$x$ is returned; otherwise, $y$ is evaluated and returned.
1260
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001261The condition $x ~\verb\or\~ y$ first evaluates $x$; if $x$ is true,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001262$x$ is returned; otherwise, $y$ is evaluated and returned.
1263
1264(Note that \verb\and\ and \verb\or\ do not restrict the value and type
1265they return to 0 and 1, but rather return the last evaluated argument.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001266This is sometimes useful, e.g., if \verb\s\ is a string, which should be
1267replaced by a default value if it is empty, \verb\s or 'foo'\
Guido van Rossumf2612d11991-11-21 13:53:03 +00001268returns the desired value. Because \verb\not\ has to invent a value
1269anyway, it does not bother to return a value of the same type as its
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001270argument, so \verb\not 'foo'\ yields \verb\0\, not \verb\''\.)
1271
1272\section{Expression lists and condition lists}
1273
1274\begin{verbatim}
1275expr_list: or_expr ("," or_expr)* [","]
1276cond_list: condition ("," condition)* [","]
1277\end{verbatim}
1278
1279The only difference between expression lists and condition lists is
1280the lowest priority of operators that can be used in them without
1281being enclosed in parentheses; condition lists allow all operators,
1282while expression lists don't allow comparisons and Boolean operators
1283(they do allow bitwise and shift operators though).
1284
1285Expression lists are used in expression statements and assignments;
1286condition lists are used everywhere else.
1287
1288An expression (condition) list containing at least one comma yields a
1289tuple. The length of the tuple is the number of expressions
1290(conditions) in the list. The expressions (conditions) are evaluated
1291from left to right.
1292
1293The trailing comma is required only to create a single tuple (a.k.a. a
1294{\em singleton}); it is optional in all other cases. A single
1295expression (condition) without a trailing comma doesn't create a
1296tuple, but rather yields the value of that expression (condition).
1297
1298To create an empty tuple, use an empty pair of parentheses: \verb\()\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001299
1300\chapter{Simple statements}
1301
1302Simple statements are comprised within a single logical line.
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001303Several simple statements may occur on a single line separated
Guido van Rossumf2612d11991-11-21 13:53:03 +00001304by semicolons. The syntax for simple statements is:
1305
1306\begin{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001307simple_stmt: expression_stmt
1308 | assignment
1309 | pass_stmt
1310 | del_stmt
1311 | print_stmt
1312 | return_stmt
1313 | raise_stmt
1314 | break_stmt
1315 | continue_stmt
1316 | import_stmt
Guido van Rossum743d1e71992-01-07 16:43:53 +00001317 | global_stmt
Guido van Rossumf2612d11991-11-21 13:53:03 +00001318\end{verbatim}
1319
1320\section{Expression statements}
1321
1322\begin{verbatim}
1323expression_stmt: expression_list
1324\end{verbatim}
1325
1326An expression statement evaluates the expression list (which may
1327be a single expression).
1328If the value is not \verb\None\, it is converted to a string
1329using the rules for string conversions, and the resulting string
1330is written to standard output on a line by itself.
1331
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001332(The exception for \verb\None\ is made so that procedure calls, which
1333are syntactically equivalent to expressions, do not cause any output.
1334A tuple with only \verb\None\ items is written normally.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001335
1336\section{Assignments}
1337
1338\begin{verbatim}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001339assignment: (target_list "=")+ expression_list
Guido van Rossum743d1e71992-01-07 16:43:53 +00001340target_list: target ("," target)* [","]
1341target: identifier | "(" target_list ")" | "[" target_list "]"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001342 | attributeref | subscription | slicing
1343\end{verbatim}
1344
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001345(See the section on primaries for the syntax definition of the last
Guido van Rossumf2612d11991-11-21 13:53:03 +00001346three symbols.)
1347
1348An assignment evaluates the expression list (remember that this can
1349be a single expression or a comma-separated list,
1350the latter yielding a tuple)
1351and assigns the single resulting object to each of the target lists,
1352from left to right.
1353
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001354Assignment is defined recursively depending on the form of the target.
1355When a target is part of a mutable object (an attribute reference,
1356subscription or slicing), the mutable object must ultimately perform
1357the assignment and decide about its validity, and may raise an
1358exception if the assignment is unacceptable. The rules observed by
1359various types and the exceptions raised are given with the definition
1360of the object types (some of which are defined in the library
1361reference).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001362
1363Assignment of an object to a target list is recursively
1364defined as follows.
1365
1366\begin{itemize}
1367\item
1368If the target list contains no commas (except in nested constructs):
1369the object is assigned to the single target contained in the list.
1370
1371\item
1372If the target list contains commas (that are not in nested constructs):
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001373the object must be a tuple with the same number of items
Guido van Rossumf2612d11991-11-21 13:53:03 +00001374as the list contains targets, and the items are assigned, from left
1375to right, to the corresponding targets.
1376
1377\end{itemize}
1378
1379Assignment of an object to a (non-list)
1380target is recursively defined as follows.
1381
1382\begin{itemize}
1383
1384\item
1385If the target is an identifier (name):
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001386\begin{itemize}
1387\item
1388If the name does not occur in a \verb\global\ statement in the current
1389code block: the object is bound to that name in the current local
1390name space.
1391\item
1392Otherwise: the object is bound to that name in the current global name
1393space.
1394\end{itemize}
1395A previous binding of the same name in the same name space is undone.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001396
1397\item
1398If the target is a target list enclosed in parentheses:
1399the object is assigned to that target list.
1400
1401\item
1402If the target is a target list enclosed in square brackets:
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001403the object must be a list with the same number of items
Guido van Rossumf2612d11991-11-21 13:53:03 +00001404as the target list contains targets,
1405and the list's items are assigned, from left to right,
1406to the corresponding targets.
1407
1408\item
1409If the target is an attribute reference:
1410The primary expression in the reference is evaluated.
1411It should yield an object with assignable attributes;
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001412if this is not the case, \verb\TypeError\ is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001413That object is then asked to assign the assigned object
1414to the given attribute; if it cannot perform the assignment,
1415it raises an exception.
1416
1417\item
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001418If the target is a subscription: The primary expression in the
1419reference is evaluated. It should yield either a mutable sequence
1420(list) object or a mapping (dictionary) object. Next, the subscript
1421expression is evaluated.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001422
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001423If the primary is a sequence object, the subscript must yield a plain
1424integer. If it is negative, the sequence's length is added to it.
1425The resulting value must be a nonnegative integer less than the
1426sequence's length, and the sequence is asked to assign the assigned
1427object to its item with that index. If the index is out of range,
1428\verb\IndexError\ is raised (assignment to a subscripted sequence
1429cannot add new items to a list).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001430
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001431If the primary is a mapping object, the subscript must have a type
1432compatible with the mapping's key type, and the mapping is then asked
1433to to create a key/datum pair which maps the subscript to the assigned
1434object. This can either replace an existing key/value pair with the
1435same key value, or insert a new key/value pair (if no key with the
1436same value existed).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001437
1438\item
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001439If the target is a slicing: The primary expression in the reference is
1440evaluated. It should yield a mutable sequence (list) object. The
1441assigned object should be a sequence object of the same type. Next,
1442the lower and upper bound expressions are evaluated, insofar they are
1443present; defaults are zero and the sequence's length. The bounds
1444should evaluate to (small) integers. If either bound is negative, the
1445sequence's length is added to it. The resulting bounds are clipped to
1446lie between zero and the sequence's length, inclusive. Finally, the
1447sequence object is asked to replace the items indicated by the slice
1448with the items of the assigned sequence. This may change the
1449sequence's length, if it allows it.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001450
1451\end{itemize}
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001452
Guido van Rossumf2612d11991-11-21 13:53:03 +00001453(In the original implementation, the syntax for targets is taken
1454to be the same as for expressions, and invalid syntax is rejected
1455during the code generation phase, causing less detailed error
1456messages.)
1457
Guido van Rossum68c172e1992-01-21 11:34:56 +00001458\section{The {\tt pass} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001459
1460\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001461pass_stmt: "pass"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001462\end{verbatim}
1463
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001464\verb\pass\ is a null operation -- when it is executed, nothing
1465happens. It is useful as a placeholder when a statement is
1466required syntactically, but no code needs to be executed, for example:
Guido van Rossumf2612d11991-11-21 13:53:03 +00001467
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001468\begin{verbatim}
1469def f(arg): pass # a no-op function
1470
1471class C: pass # an empty class
1472\end{verbatim}
1473
Guido van Rossum68c172e1992-01-21 11:34:56 +00001474\section{The {\tt del} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001475
1476\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001477del_stmt: "del" target_list
Guido van Rossumf2612d11991-11-21 13:53:03 +00001478\end{verbatim}
1479
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001480Deletion is recursively defined very similar to the way assignment is
1481defined. Rather that spelling it out in full details, here are some
1482hints.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001483
1484Deletion of a target list recursively deletes each target,
1485from left to right.
1486
1487Deletion of a name removes the binding of that name (which must exist)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001488from the local or global name space, depending on whether the name
1489occurs in a \verb\global\ statement in the same code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001490
1491Deletion of attribute references, subscriptions and slicings
1492is passed to the primary object involved; deletion of a slicing
1493is in general equivalent to assignment of an empty slice of the
1494right type (but even this is determined by the sliced object).
1495
Guido van Rossum68c172e1992-01-21 11:34:56 +00001496\section{The {\tt print} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001497
1498\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001499print_stmt: "print" [ condition ("," condition)* [","] ]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001500\end{verbatim}
1501
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001502\verb\print\ evaluates each condition in turn and writes the resulting
1503object to standard output (see below). If an object is not a string,
1504it is first converted to a string using the rules for string
1505conversions. The (resulting or original) string is then written. A
1506space is written before each object is (converted and) written, unless
1507the output system believes it is positioned at the beginning of a
1508line. This is the case: (1) when no characters have yet been written
1509to standard output; or (2) when the last character written to standard
1510output is \verb/\n/; or (3) when the last write operation on standard
1511output was not a \verb\print\ statement. (In some cases it may be
1512functional to write an empty string to standard output for this
1513reason.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001514
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001515A \verb/"\n"/ character is written at the end, unless the \verb\print\
1516statement ends with a comma. This is the only action if the statement
1517contains just the keyword \verb\print\.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001518
1519Standard output is defined as the file object named \verb\stdout\
1520in the built-in module \verb\sys\. If no such object exists,
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001521or if it is not a writable file, a \verb\RuntimeError\ exception is raised.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001522(The original implementation attempts to write to the system's original
1523standard output instead, but this is not safe, and should be fixed.)
1524
Guido van Rossum68c172e1992-01-21 11:34:56 +00001525\section{The {\tt return} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001526
1527\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001528return_stmt: "return" [condition_list]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001529\end{verbatim}
1530
1531\verb\return\ may only occur syntactically nested in a function
1532definition, not within a nested class definition.
1533
1534If a condition list is present, it is evaluated, else \verb\None\
1535is substituted.
1536
1537\verb\return\ leaves the current function call with the condition
1538list (or \verb\None\) as return value.
1539
1540When \verb\return\ passes control out of a \verb\try\ statement
1541with a \verb\finally\ clause, that finally clause is executed
1542before really leaving the function.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001543
Guido van Rossum68c172e1992-01-21 11:34:56 +00001544\section{The {\tt raise} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001545
1546\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001547raise_stmt: "raise" condition ["," condition]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001548\end{verbatim}
1549
1550\verb\raise\ evaluates its first condition, which must yield
1551a string object. If there is a second condition, this is evaluated,
1552else \verb\None\ is substituted.
1553
1554It then raises the exception identified by the first object,
1555with the second one (or \verb\None\) as its parameter.
1556
Guido van Rossum68c172e1992-01-21 11:34:56 +00001557\section{The {\tt break} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001558
1559\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001560break_stmt: "break"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001561\end{verbatim}
1562
1563\verb\break\ may only occur syntactically nested in a \verb\for\
1564or \verb\while\ loop, not nested in a function or class definition.
1565
1566It terminates the neares enclosing loop, skipping the optional
1567\verb\else\ clause if the loop has one.
1568
1569If a \verb\for\ loop is terminated by \verb\break\, the loop control
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001570target keeps its current value.
Guido van Rossumf2612d11991-11-21 13:53:03 +00001571
1572When \verb\break\ passes control out of a \verb\try\ statement
1573with a \verb\finally\ clause, that finally clause is executed
1574before really leaving the loop.
1575
Guido van Rossum68c172e1992-01-21 11:34:56 +00001576\section{The {\tt continue} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001577
1578\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001579continue_stmt: "continue"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001580\end{verbatim}
1581
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001582\verb\continue\ may only occur syntactically nested in a \verb\for\ or
1583\verb\while\ loop, not nested in a function or class definition, and
1584not nested in the \verb\try\ clause of a \verb\try\ statement with a
1585\verb\finally\ clause (it may occur nested in a \verb\except\ or
1586\verb\finally\ clause of a \verb\try\ statement though).
Guido van Rossumf2612d11991-11-21 13:53:03 +00001587
1588It continues with the next cycle of the nearest enclosing loop.
1589
Guido van Rossum68c172e1992-01-21 11:34:56 +00001590\section{The {\tt import} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001591
1592\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001593import_stmt: "import" identifier ("," identifier)*
1594 | "from" identifier "import" identifier ("," identifier)*
1595 | "from" identifier "import" "*"
1596\end{verbatim}
1597
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001598Import statements are executed in two steps: (1) find a module, and
1599initialize it if necessary; (2) define a name or names in the local
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001600name space (of the scope where the \verb\import\ statement occurs).
1601The first form (without \verb\from\) repeats these steps for each
1602identifier in the list, the \verb\from\ form performs them once, with
1603the first identifier specifying the module name.
Guido van Rossum743d1e71992-01-07 16:43:53 +00001604
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001605The system maintains a table of modules that have been initialized,
1606indexed by module name. (The current implementation makes this table
1607accessible as \verb\sys.modules\.) When a module name is found in
1608this table, step (1) is finished. If not, a search for a module
1609definition is started. This first looks for a built-in module
1610definition, and if no built-in module if the given name is found, it
1611searches a user-specified list of directories for a file whose name is
1612the module name with extension \verb\".py"\. (The current
1613implementation uses the list of strings \verb\sys.path\ as the search
1614path; it is initialized from the shell environment variable
1615\verb\$PYTHONPATH\, with an installation-dependent default.)
1616
1617If a built-in module is found, its built-in initialization code is
1618executed and step (1) is finished. If no matching file is found,
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001619\verb\ImportError\ is raised. If a file is found, it is parsed,
1620yielding an executable code block. If a syntax error occurs,
1621\verb\SyntaxError\ is raised. Otherwise, an empty module of the given
1622name is created and inserted in the module table, and then the code
1623block is executed in the context of this module. Exceptions during
1624this execution terminate step (1).
1625
1626When step (1) finishes without raising an exception, step (2) can
1627begin.
1628
1629The first form of \verb\import\ statement binds the module name in the
1630local name space to the module object, and then goes on to import the
1631next identifier, if any. The \verb\from\ from does not bind the
1632module name: it goes through the list of identifiers, looks each one
1633of them up in the module found in step (1), and binds the name in the
1634local name space to the object thus found. If a name is not found,
1635\verb\ImportError\ is raised. If the list of identifiers is replaced
1636by a star (\verb\*\), all names defined in the module are bound,
1637except those beginning with an underscore(\verb\_\).
1638
1639Names bound by import statements may not occur in \verb\global\
1640statements in the same scope.
1641
1642The \verb\from\ form with \verb\*\ may only occur in a module scope.
1643
1644(The current implementation does not enforce the latter two
1645restrictions, but programs should not abuse this freedom, as future
1646implementations may enforce them or silently change the meaning of the
1647program.)
Guido van Rossum0f1f9da1992-01-20 17:10:21 +00001648
Guido van Rossum68c172e1992-01-21 11:34:56 +00001649\section{The {\tt global} statement}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001650
1651\begin{verbatim}
1652global_stmt: "global" identifier ("," identifier)*
Guido van Rossumf2612d11991-11-21 13:53:03 +00001653\end{verbatim}
1654
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001655The \verb\global\ statement is a declaration which holds for the
1656entire current scope. It means that the listed identifiers are to be
1657interpreted as globals. While {\em using} global names is automatic
1658if they are not defined in the local scope, {\em assigning} to global
1659names would be impossible without \verb\global\.
1660
1661Names listed in a \verb\global\ statement must not be used in the same
1662scope before that \verb\global\ statement is executed.
1663
1664Name listed in a \verb\global\ statement must not be defined as formal
1665parameters or in a \verb\for\ loop control target, \verb\class\
1666definition, function definition, or \verb\import\ statement.
1667
1668(The current implementation does not enforce the latter two
1669restrictions, but programs should not abuse this freedom, as future
1670implementations may enforce them or silently change the meaning of the
1671program.)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001672
1673\chapter{Compound statements}
1674
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001675Compound statements contain (groups of) other statements; they affect
1676or control the execution of those other statements in some way.
1677
1678The \verb\if\, \verb\while\ and \verb\for\ statements implement
1679traditional control flow constructs. \verb\try\ specifies exception
1680handlers and/or cleanup code for a group of statements. Function and
1681class definitions are also syntactically compound statements.
1682
1683Compound statements consist of one or more `clauses'. A clause
1684consists of a header and a `suite'. The clause headers of a
1685particular compound statement are all at the same indentation level;
1686all clauses begin with a uniquely identifying keyword and end with a
1687colon. A suite is a group of statements controlled by a clause. A
1688suite can be a bunch of semicolon-separated simple statements on the
1689same line as the header, following the colon, or it can be a list of
1690indented statements. Only the latter form of suite can contain nested
1691compound statements; the following is illegal (mostly because it
1692wouldn't be clear what to do with \verb\else\):
Guido van Rossumf2612d11991-11-21 13:53:03 +00001693
1694\begin{verbatim}
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001695if test1: if test2: print x
Guido van Rossumf2612d11991-11-21 13:53:03 +00001696\end{verbatim}
1697
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001698Also note that the semicolon binds tighter that the colon in this
1699context (so to speak), so that in the following example, either all or
1700none of the \verb\print\ statements are executed:
1701
1702\begin{verbatim}
1703if some_test: print x; print y; print z
1704\end{verbatim}
1705
1706Summarizing:
1707
1708\begin{verbatim}
1709compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1710suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
1711statement: stmt_list NEWLINE | compound_stmt
1712stmt_list: simple_stmt (";" simple_stmt)* [";"]
1713\end{verbatim}
1714
1715Note that statements always ends in a \verb\NEWLINE\ possibly followed
1716by a \verb\DEDENT\.
1717
1718Also note that optional continuation clauses always begin with a
1719keyword that cannot start a statement, thus there are no ambiguities
1720(the `dangling \verb\else\' problem is solved in Python by requiring
1721nested \verb\if\ statements to be indented).
1722
1723The formatting of the grammar rules in the following section places
1724each clause on a separate line for clarity.
1725
Guido van Rossum68c172e1992-01-21 11:34:56 +00001726\section{The {\tt if} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001727
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001728The \verb\if\ statement is used for conditional execution:
1729
Guido van Rossumf2612d11991-11-21 13:53:03 +00001730\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001731if_stmt: "if" condition ":" suite
1732 ("elif" condition ":" suite)*
1733 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001734\end{verbatim}
1735
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001736It selects exactly one of the suites, by testing the conditions one by
1737one until one is true; then that suite is executed. If all conditions
1738are false, the suite of the \verb\else\ clause is executed, if present.
1739
Guido van Rossum68c172e1992-01-21 11:34:56 +00001740\section{The {\tt while} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001741
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001742The \verb\while\ statement is used for repeated execution as long as a
1743condition is true:
1744
Guido van Rossumf2612d11991-11-21 13:53:03 +00001745\begin{verbatim}
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001746while_stmt: "while" condition ":" suite
1747 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001748\end{verbatim}
1749
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001750This repeatedly tests the condition and, if it is true, executes the
1751first suite; if the condition is false (which may be the first time it
1752is tested) the suite of the \verb\else\ clause is executed, if
1753present, and the loop terminates.
1754
1755A \verb\break\ statement executed in the first suite terminates the
1756loop without executing the \verb\else\ clause's suite. A
1757\verb\continue\ statement executed in the first suited skips the rest
1758of the suite and goes back to testing the condition.
1759
Guido van Rossum68c172e1992-01-21 11:34:56 +00001760\section{The {\tt for} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001761
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001762The \verb\for\ statement is used to iterate over the elements of a
1763sequence (string, tuple or list):
1764
Guido van Rossumf2612d11991-11-21 13:53:03 +00001765\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001766for_stmt: "for" target_list "in" condition_list ":" suite
1767 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001768\end{verbatim}
1769
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001770The suite is executed once for each item in the condition list, in the
1771order of ascending indices. Each item in turn is assigned to the
1772target list using the standard rules for assignments, and then the
1773suite is executed. When the list is exhausted (which is immediately
1774when the sequence is empty), the suite in the \verb\else\ clause is
1775executed, if present.
1776
1777A \verb\break\ statement executed in the first suite terminates the
1778loop without executing the \verb\else\ clause's suite. A
1779\verb\continue\ statement executed in the first suited skips the rest
1780of the suite and continues with the next item or with the \verb\else\
1781clause.
1782
1783The suite may assign to the variable(s) in the target list; this does
1784not affect the next item assigned to it.
1785
1786The target list are not deleted when the loop is finished (but if the
1787loop has executed 0 times it will not have been assigned to at all by
1788the loop).
1789
1790The built-in function \verb\range()\ returns a sequence of integers
1791suitable to emulate the effect of Pascal's \verb\for i := 1 to n do\.
1792
1793{\bf Warning:} There is a subtlety when the sequence is being modified
1794by the loop (this can only occur for lists). An internal counter is
1795used to keep track of which item is used next, and this is incremented
1796on each iteration. When this counter has reached the end of the
1797sequence the loop terminates. This means that if the suite deletes
1798the current (or a previous) item from the sequence, the next item will
1799be skipped (since it gets the index of the current item and this has
1800already been treated). Likewise, if the suite inserts an item in the
1801sequence before the current item, the current item will be treated
1802again the next time through the loop. This can lead to nasty bugs
1803that can be avoided by making a temporary copy using the \
1804
Guido van Rossum68c172e1992-01-21 11:34:56 +00001805\section{The {\tt try} statement}
Guido van Rossumf2612d11991-11-21 13:53:03 +00001806
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001807The \verb\try\ statement specifies exception handlers and/or cleanup
1808code for a group of statements:
1809
Guido van Rossumf2612d11991-11-21 13:53:03 +00001810\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001811try_stmt: "try" ":" suite
1812 ("except" condition ["," condition] ":" suite)*
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001813 ["except" ":" suite]
Guido van Rossum743d1e71992-01-07 16:43:53 +00001814 ["finally" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001815\end{verbatim}
1816
Guido van Rossum255ad6e1992-01-28 18:10:46 +00001817There are really two forms: \verb\try...except\ and
1818\verb\try...finally\. A \verb\try\ statement with both types of
1819clauses is equivalent to a \verb\try...finally\ statement with a
1820\verb\try...except\ statement in its \verb\try\ clause. A \verb\try\
1821statement with neither a \verb\except\ clause nor a \verb\finally\
1822clause just executes the suite of statements in its \verb\try\ clause.
1823
1824The \verb\try...except\ form specifies one or more exception handlers.
1825When no exception occurs in the \verb\try\ clause, no exception
1826handler is executed. When an exception occurs in the \verb\try\
1827suite, a search for an exception handler HIRO
1828
1829The \verb\try...finally\ form specifies a `cleanup' handler. The
1830\verb\try\ clause is executed. When no exception occurs, the
1831\verb\finally\ clause is executed. When an exception occurs on the
1832\verb\try\ clause, the exception is temporarily saved, the
1833\verb\finally\ clause is executed, and then the saved exception is
1834re-raised. If the \verb\finally\ clause raises another exception or
1835executes a \verb\return\, \verb\break\ or \verb\continue\ statement,
1836the saved exception is lost.
1837
1838When a \verb\return\ or \verb\break\ statement is executed in the
1839\verb\try suite of a \verb\try...finally\ statement, the
1840\verb\finally\ clause is also executed `on the way out'. A
1841\verb\continue\ statement is illegal in the \verb\try\ clause (the
1842reason is a problem with the current implementation -- this
1843restriction may be lifted in the future).
1844
1845
1846
Guido van Rossumf2612d11991-11-21 13:53:03 +00001847\section{Function definitions}
1848
1849\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001850funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
1851parameter_list: parameter ("," parameter)*
1852parameter: identifier | "(" parameter_list ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001853\end{verbatim}
1854
1855\section{Class definitions}
1856
1857\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001858classdef: "class" identifier [inheritance] ":" suite
1859inheritance: "(" expression ("," expression)* ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001860\end{verbatim}
1861
1862XXX Syntax for scripts, modules
1863XXX Syntax for interactive input, eval, exec, input
Guido van Rossum743d1e71992-01-07 16:43:53 +00001864XXX New definition of expressions (as conditions)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001865
1866\end{document}