blob: ed55884f7b5e17ee0c88bd953cbc4cff4d566856 [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 Rossumf2612d11991-11-21 13:53:03 +00003\documentstyle[myformat]{report}
4
5\title{\bf
6 Python Reference Manual \\
7 {\em Incomplete Draft}
8}
9
10\author{
11 Guido van Rossum \\
12 Dept. CST, CWI, Kruislaan 413 \\
13 1098 SJ Amsterdam, The Netherlands \\
14 E-mail: {\tt guido@cwi.nl}
15}
16
17\begin{document}
18
19\pagenumbering{roman}
20
21\maketitle
22
23\begin{abstract}
24
25\noindent
26Python is a simple, yet powerful programming language that bridges the
27gap between C and shell programming, and is thus ideally suited for
28``throw-away programming''
29and rapid prototyping. Its syntax is put
30together from constructs borrowed from a variety of other languages;
31most prominent are influences from ABC, C, Modula-3 and Icon.
32
33The Python interpreter is easily extended with new functions and data
34types implemented in C. Python is also suitable as an extension
35language for highly customizable C applications such as editors or
36window managers.
37
38Python is available for various operating systems, amongst which
39several flavors of {\UNIX}, Amoeba, the Apple Macintosh O.S.,
40and MS-DOS.
41
42This reference manual describes the syntax and ``core semantics'' of
43the language. It is terse, but exact and complete. The semantics of
44non-essential built-in object types and of the built-in functions and
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000045modules are described in the {\em Python Library Reference}. For an
46informal introduction to the language, see the {\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
Guido van Rossum670e5a01992-01-17 14:03:20 +000090\section{Warning}
91
92This version of the manual is incomplete. Sections that still need to
93be written or need considerable work are marked with ``XXX''.
94
Guido van Rossum743d1e71992-01-07 16:43:53 +000095\section{Notation}
96
97The descriptions of lexical analysis and syntax use a modified BNF
98grammar notation. This uses the following style of definition:
99
100\begin{verbatim}
101name: lcletter (lcletter | "_")*
102lcletter: "a"..."z"
103\end{verbatim}
104
Guido van Rossum7b632a61992-01-16 17:49:21 +0000105The first line says that a \verb\name\ is an \verb\lcletter\ followed by
106a sequence of zero or more \verb\lcletter\s and underscores. An
Guido van Rossum743d1e71992-01-07 16:43:53 +0000107\verb\lcletter\ in turn is any of the single characters `a' through `z'.
108(This rule is actually adhered to for the names defined in syntax and
109grammar rules in this document.)
110
111Each rule begins with a name (which is the name defined by the rule)
Guido van Rossum7b632a61992-01-16 17:49:21 +0000112and a colon, and is wholly contained on one line. A vertical bar
113(\verb\|\) is used to separate alternatives; it is the least binding
114operator in this notation. A star (\verb\*\) means zero or more
115repetitions of the preceding item; likewise, a plus (\verb\+\) means
116one or more repetitions, and a question mark (\verb\?\) zero or one
117(in other words, the preceding item is optional). These three
118operators bind as tightly as possible; parentheses are used for
Guido van Rossum743d1e71992-01-07 16:43:53 +0000119grouping. Literal strings are enclosed in double quotes. White space
120is only meaningful to separate tokens.
121
122In lexical definitions (as the example above), two more conventions
123are used: Two literal characters separated by three dots mean a choice
124of any single character in the given (inclusive) range of ASCII
125characters. A phrase between angular brackets (\verb\<...>\) gives an
126informal description of the symbol defined; e.g., this could be used
127to describe the notion of `control character' if needed.
128
Guido van Rossum7b632a61992-01-16 17:49:21 +0000129Even though the notation used is almost the same, there is a big
Guido van Rossum743d1e71992-01-07 16:43:53 +0000130difference between the meaning of lexical and syntactic definitions:
131a lexical definition operates on the individual characters of the
132input source, while a syntax definition operates on the stream of
133tokens generated by the lexical analysis.
134
Guido van Rossumf2612d11991-11-21 13:53:03 +0000135\chapter{Lexical analysis}
136
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000137A Python program is read by a {\em parser}. Input to the parser is a
138stream of {\em tokens}, generated by the {\em lexical analyzer}. This
139chapter describes how the lexical analyzer breaks a file into tokens.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000140
141\section{Line structure}
142
Guido van Rossum7b632a61992-01-16 17:49:21 +0000143A Python program is divided in a number of logical lines. The end of
144a logical line is represented by the token NEWLINE. Statements cannot
145cross logical line boundaries except where NEWLINE is allowed by the
146syntax (e.g., between statements in compound statements).
Guido van Rossumf2612d11991-11-21 13:53:03 +0000147
148\subsection{Comments}
149
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000150A comment starts with a hash character (\verb\#\) that is not part of
Guido van Rossum7b632a61992-01-16 17:49:21 +0000151a string literal, and ends at the end of the physical line. A comment
152always signifies the end of the logical line. Comments are ignored by
153the syntax.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000154
155\subsection{Line joining}
156
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000157Two or more physical lines may be joined into logical lines using
Guido van Rossum7b632a61992-01-16 17:49:21 +0000158backslash characters (\verb/\/), as follows: when a physical line ends
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000159in a backslash that is not part of a string literal or comment, it is
160joined with the following forming a single logical line, deleting the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000161backslash and the following end-of-line character. For example:
162%
163\begin{verbatim}
164samplingrates = (48000, AL.RATE_48000), \
165 (44100, AL.RATE_44100), \
166 (32000, AL.RATE_32000), \
167 (22050, AL.RATE_22050), \
168 (16000, AL.RATE_16000), \
169 (11025, AL.RATE_11025), \
170 ( 8000, AL.RATE_8000)
171\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000172
173\subsection{Blank lines}
174
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000175A logical line that contains only spaces, tabs, and possibly a
176comment, is ignored (i.e., no NEWLINE token is generated), except that
177during interactive input of statements, an entirely blank logical line
178terminates a multi-line statement.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000179
180\subsection{Indentation}
181
Guido van Rossum7b632a61992-01-16 17:49:21 +0000182Leading whitespace (spaces and tabs) at the beginning of a logical
183line is used to compute the indentation level of the line, which in
184turn is used to determine the grouping of statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000185
Guido van Rossum7b632a61992-01-16 17:49:21 +0000186First, tabs are replaced (from left to right) by one to eight spaces
187such that the total number of characters up to there is a multiple of
188eight (this is intended to be the same rule as used by UNIX). The
189total number of spaces preceding the first non-blank character then
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000190determines the line's indentation. Indentation cannot be split over
191multiple physical lines using backslashes.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000192
193The indentation levels of consecutive lines are used to generate
194INDENT and DEDENT tokens, using a stack, as follows.
195
196Before the first line of the file is read, a single zero is pushed on
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000197the stack; this will never be popped off again. The numbers pushed on
198the stack will always be strictly increasing from bottom to top. At
199the beginning of each logical line, the line's indentation level is
200compared to the top of the stack. If it is equal, nothing happens.
201If it larger, it is pushed on the stack, and one INDENT token is
202generated. If it is smaller, it {\em must} be one of the numbers
203occurring on the stack; all numbers on the stack that are larger are
204popped off, and for each number popped off a DEDENT token is
205generated. At the end of the file, a DEDENT token is generated for
206each number remaining on the stack that is larger than zero.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000207
Guido van Rossum7b632a61992-01-16 17:49:21 +0000208Here is an example of a correctly (though confusingly) indented piece
209of Python code:
210
211\begin{verbatim}
212def perm(l):
Guido van Rossum670e5a01992-01-17 14:03:20 +0000213
214 # Compute the list of all permutations of l
215
Guido van Rossum7b632a61992-01-16 17:49:21 +0000216 if len(l) <= 1:
217 return [l]
218 r = []
219 for i in range(len(l)):
220 s = l[:i] + l[i+1:]
221 p = perm(s)
222 for x in p:
223 r.append(l[i:i+1] + x)
224 return r
225\end{verbatim}
226
227The following example shows various indentation errors:
228
229\begin{verbatim}
230 def perm(l): # error: first line indented
231 for i in range(len(l)): # error: not indented
232 s = l[:i] + l[i+1:]
233 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
234 for x in p:
235 r.append(l[i:i+1] + x)
236 return r # error: inconsistent indent
237\end{verbatim}
238
239(Actually, the first three errors are detected by the parser; only the
240last error is found by the lexical analyzer -- the indentation of
241\verb\return r\ does not match a level popped off the stack.)
242
Guido van Rossumf2612d11991-11-21 13:53:03 +0000243\section{Other tokens}
244
245Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
246exist: identifiers, keywords, literals, operators, and delimiters.
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000247Spaces and tabs are not tokens, but serve to delimit tokens. Where
248ambiguity exists, a token comprises the longest possible string that
249forms a legal token, when read from left to right.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000250
Guido van Rossumf2612d11991-11-21 13:53:03 +0000251\section{Identifiers}
252
253Identifiers are described by the following regular expressions:
254
255\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000256identifier: (letter|"_") (letter|digit|"_")*
Guido van Rossumf2612d11991-11-21 13:53:03 +0000257letter: lowercase | uppercase
Guido van Rossum743d1e71992-01-07 16:43:53 +0000258lowercase: "a"..."z"
259uppercase: "A"..."Z"
260digit: "0"..."9"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000261\end{verbatim}
262
Guido van Rossum670e5a01992-01-17 14:03:20 +0000263Identifiers are unlimited in length. Case is significant.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000264
Guido van Rossum670e5a01992-01-17 14:03:20 +0000265\subsection{Keywords}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000266
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000267The following identifiers are used as reserved words, or {\em
Guido van Rossum7b632a61992-01-16 17:49:21 +0000268keywords} of the language, and cannot be used as ordinary
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000269identifiers. They must be spelled exactly as written here:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000270
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000271\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000272and del for in print
273break elif from is raise
274class else global not return
275continue except if or try
276def finally import pass while
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000277\end{verbatim}
278
Guido van Rossum743d1e71992-01-07 16:43:53 +0000279% # This Python program sorts and formats the above table
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000280% import string
281% l = []
282% try:
283% while 1:
284% l = l + string.split(raw_input())
285% except EOFError:
286% pass
287% l.sort()
288% for i in range((len(l)+4)/5):
289% for j in range(i, len(l), 5):
290% print string.ljust(l[j], 10),
291% print
Guido van Rossumf2612d11991-11-21 13:53:03 +0000292
293\section{Literals}
294
295\subsection{String literals}
296
297String literals are described by the following regular expressions:
298
299\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000300stringliteral: "'" stringitem* "'"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000301stringitem: stringchar | escapeseq
Guido van Rossum743d1e71992-01-07 16:43:53 +0000302stringchar: <any ASCII character except newline or "\" or "'">
303escapeseq: "'" <any ASCII character except newline>
Guido van Rossumf2612d11991-11-21 13:53:03 +0000304\end{verbatim}
305
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000306String literals cannot span physical line boundaries. Escape
307sequences in strings are actually interpreted according to rules
308simular to those used by Standard C. The recognized escape sequences
309are:
310
311\begin{center}
312\begin{tabular}{|l|l|}
313\hline
314\verb/\\/ & Backslash (\verb/\/) \\
315\verb/\'/ & Single quote (\verb/'/) \\
316\verb/\a/ & ASCII Bell (BEL) \\
317\verb/\b/ & ASCII Backspace (BS) \\
Guido van Rossum7b632a61992-01-16 17:49:21 +0000318%\verb/\E/ & ASCII Escape (ESC) \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000319\verb/\f/ & ASCII Formfeed (FF) \\
320\verb/\n/ & ASCII Linefeed (LF) \\
321\verb/\r/ & ASCII Carriage Return (CR) \\
322\verb/\t/ & ASCII Horizontal Tab (TAB) \\
323\verb/\v/ & ASCII Vertical Tab (VT) \\
324\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
Guido van Rossum743d1e71992-01-07 16:43:53 +0000325\verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000326\hline
327\end{tabular}
328\end{center}
329
Guido van Rossum7b632a61992-01-16 17:49:21 +0000330In strict compatibility with in Standard C, up to three octal digits are
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000331accepted, but an unlimited number of hex digits is taken to be part of
332the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum7b632a61992-01-16 17:49:21 +0000333are used in all current implementations...).
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000334
Guido van Rossum7b632a61992-01-16 17:49:21 +0000335All unrecognized escape sequences are left in the string unchanged,
336i.e., {\em the backslash is left in the string.} (This rule is
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000337useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum743d1e71992-01-07 16:43:53 +0000338resulting output is more easily recognized as broken. It also helps a
339great deal for string literals used as regular expressions or
340otherwise passed to other modules that do their own escape handling --
341but you may end up quadrupling backslashes that must appear literally.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000342
343\subsection{Numeric literals}
344
Guido van Rossum670e5a01992-01-17 14:03:20 +0000345There are three types of numeric literals: plain integers, long
346integers, and floating point numbers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000347
348Integers and long integers are described by the following regular expressions:
349
350\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000351longinteger: integer ("l"|"L")
Guido van Rossumf2612d11991-11-21 13:53:03 +0000352integer: decimalinteger | octinteger | hexinteger
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000353decimalinteger: nonzerodigit digit* | "0"
354octinteger: "0" octdigit+
355hexinteger: "0" ("x"|"X") hexdigit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000356
Guido van Rossum743d1e71992-01-07 16:43:53 +0000357nonzerodigit: "1"..."9"
358octdigit: "0"..."7"
359hexdigit: digit|"a"..."f"|"A"..."F"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000360\end{verbatim}
361
Guido van Rossum670e5a01992-01-17 14:03:20 +0000362Although both lower case `l'and upper case `L' are allowed as suffix
363for long integers, it is strongly recommended to always use `L', since
364the letter `l' looks too much like the digit `1'.
365
366(Plain) integer decimal literals must be at most $2^{31} - 1$ (i.e., the
367largest positive integer, assuming 32-bit arithmetic); octal and
368hexadecimal literals may be as large as $2^{32} - 1$. There is no limit
369for long integer literals.
370
371Some examples of (plain and long) integer literals:
372
373\begin{verbatim}
3747 2147483647 0177 0x80000000
3753L 79228162514264337593543950336L 0377L 0100000000L
376\end{verbatim}
377
Guido van Rossumf2612d11991-11-21 13:53:03 +0000378Floating point numbers are described by the following regular expressions:
379
380\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000381floatnumber: pointfloat | exponentfloat
382pointfloat: [intpart] fraction | intpart "."
383exponentfloat: (intpart | pointfloat) exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000384intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000385fraction: "." digit+
386exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000387\end{verbatim}
388
Guido van Rossum670e5a01992-01-17 14:03:20 +0000389The range of floating point literals is implementation-dependent.
390
391Some examples of floating point literals:
Guido van Rossum7b632a61992-01-16 17:49:21 +0000392
393\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +00003943.14 10. .001 1e100 3.14e-10
Guido van Rossum7b632a61992-01-16 17:49:21 +0000395\end{verbatim}
396
Guido van Rossum670e5a01992-01-17 14:03:20 +0000397Note that numeric literals do not include a sign; a phrase like
398\verb\-1\ is actually an expression composed of the operator
Guido van Rossum7b632a61992-01-16 17:49:21 +0000399\verb\-\ and the literal \verb\1\.
400
Guido van Rossumf2612d11991-11-21 13:53:03 +0000401\section{Operators}
402
403The following tokens are operators:
404
405\begin{verbatim}
406+ - * / %
407<< >> & | ^ ~
Guido van Rossum743d1e71992-01-07 16:43:53 +0000408< == > <= <> != >=
Guido van Rossumf2612d11991-11-21 13:53:03 +0000409\end{verbatim}
410
Guido van Rossum743d1e71992-01-07 16:43:53 +0000411The comparison operators \verb\<>\ and \verb\!=\ are alternate
412spellings of the same operator.
413
Guido van Rossumf2612d11991-11-21 13:53:03 +0000414\section{Delimiters}
415
Guido van Rossum743d1e71992-01-07 16:43:53 +0000416The following tokens serve as delimiters or otherwise have a special
417meaning:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000418
419\begin{verbatim}
420( ) [ ] { }
Guido van Rossum743d1e71992-01-07 16:43:53 +0000421; , : . ` =
Guido van Rossumf2612d11991-11-21 13:53:03 +0000422\end{verbatim}
423
Guido van Rossum7b632a61992-01-16 17:49:21 +0000424The following printing ASCII characters are not used in Python (except
425in string literals and in comments). Their occurrence is an
426unconditional error:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000427
428\begin{verbatim}
429! @ $ " ?
430\end{verbatim}
431
Guido van Rossum7b632a61992-01-16 17:49:21 +0000432They may be used by future versions of the language though!
433
Guido van Rossumf2612d11991-11-21 13:53:03 +0000434\chapter{Execution model}
435
Guido van Rossum743d1e71992-01-07 16:43:53 +0000436\section{Objects, values and types}
437
438I won't try to define rigorously here what an object is, but I'll give
439some properties of objects that are important to know about.
440
441Every object has an identity, a type and a value. An object's {\em
442identity} never changes once it has been created; think of it as the
443object's (permanent) address. An object's {\em type} determines the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000444operations that an object supports (e.g., does it have a length?) and
445also defines the ``meaning'' of the object's value. The type also
446never changes. The {\em value} of some objects can change; whether
447this is possible is a property of its type.
Guido van Rossum743d1e71992-01-07 16:43:53 +0000448
449Objects are never explicitly destroyed; however, when they become
Guido van Rossum670e5a01992-01-17 14:03:20 +0000450unreachable they may be garbage-collected. An implementation is
451allowed to delay garbage collection or omit it altogether -- it is a
452matter of implementation quality how garbage collection is
453implemented, as long as no objects are collected that are still
454reachable. (Implementation note: the current implementation uses a
Guido van Rossum743d1e71992-01-07 16:43:53 +0000455reference-counting scheme which collects most objects as soon as they
Guido van Rossum670e5a01992-01-17 14:03:20 +0000456become onreachable, but never collects garbage containing circular
Guido van Rossum743d1e71992-01-07 16:43:53 +0000457references.)
458
Guido van Rossum670e5a01992-01-17 14:03:20 +0000459Note that the use of the implementation's tracing or debugging
460facilities may keep objects alive that would normally be collectable.
461
Guido van Rossum743d1e71992-01-07 16:43:53 +0000462(Some objects contain references to ``external'' resources such as
463open files. It is understood that these resources are freed when the
464object is garbage-collected, but since garbage collection is not
Guido van Rossum670e5a01992-01-17 14:03:20 +0000465guaranteed, such objects also provide an explicit way to release the
466external resource (e.g., a \verb\close\ method). Programs are strongly
Guido van Rossum743d1e71992-01-07 16:43:53 +0000467recommended to use this.)
468
469Some objects contain references to other objects. These references
470are part of the object's value; in most cases, when such a
471``container'' object is compared to another (of the same type), the
Guido van Rossum670e5a01992-01-17 14:03:20 +0000472comparison applies to the {\em values} of the referenced objects (not
473their identities).
Guido van Rossum743d1e71992-01-07 16:43:53 +0000474
Guido van Rossum670e5a01992-01-17 14:03:20 +0000475Types affect almost all aspects of objects.
476Even object identity is affected in some sense: for immutable
Guido van Rossum743d1e71992-01-07 16:43:53 +0000477types, operations that compute new values may actually return a
Guido van Rossum670e5a01992-01-17 14:03:20 +0000478reference to any existing object with the same type and value, while
Guido van Rossum743d1e71992-01-07 16:43:53 +0000479for mutable objects this is not allowed. E.g., after
480
481\begin{verbatim}
482a = 1; b = 1; c = []; d = []
483\end{verbatim}
484
485\verb\a\ and \verb\b\ may or may not refer to the same object, but
486\verb\c\ and \verb\d\ are guaranteed to refer to two different, unique,
487newly created lists.
488
Guido van Rossum670e5a01992-01-17 14:03:20 +0000489\section{The standard type hierarchy}
490
491XXX None, sequences, numbers, mappings, ...
492
Guido van Rossum743d1e71992-01-07 16:43:53 +0000493\section{Execution frames, name spaces, and scopes}
494
Guido van Rossum670e5a01992-01-17 14:03:20 +0000495XXX code blocks, scopes, name spaces, name binding, exceptions
Guido van Rossumf2612d11991-11-21 13:53:03 +0000496
497\chapter{Expressions and conditions}
498
Guido van Rossum743d1e71992-01-07 16:43:53 +0000499From now on, extended BNF notation will be used to describe syntax,
500not lexical analysis.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000501
502This chapter explains the meaning of the elements of expressions and
503conditions. Conditions are a superset of expressions, and a condition
Guido van Rossum670e5a01992-01-17 14:03:20 +0000504may be used wherever an expression is required by enclosing it in
505parentheses. The only places where expressions are used in the syntax
506instead of conditions is in expression statements and on the
507right-hand side of assignments; this catches some nasty bugs like
508accedentally writing \verb\x == 1\ instead of \verb\x = 1\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000509
Guido van Rossum670e5a01992-01-17 14:03:20 +0000510The comma has several roles in Python's syntax. It is usually an
Guido van Rossum743d1e71992-01-07 16:43:53 +0000511operator with a lower precedence than all others, but occasionally
Guido van Rossum670e5a01992-01-17 14:03:20 +0000512serves other purposes as well; e.g., it separates function arguments,
513is used in list and dictionary constructors, and has special semantics
514in \verb\print\ statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000515
516When (one alternative of) a syntax rule has the form
517
518\begin{verbatim}
519name: othername
520\end{verbatim}
521
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000522and no semantics are given, the semantics of this form of \verb\name\
523are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000524
525\section{Arithmetic conversions}
526
527When a description of an arithmetic operator below uses the phrase
528``the numeric arguments are converted to a common type'',
529this both means that if either argument is not a number, a
530{\tt TypeError} exception is raised, and that otherwise
531the following conversions are applied:
532
533\begin{itemize}
534\item First, if either argument is a floating point number,
535 the other is converted to floating point;
536\item else, if either argument is a long integer,
537 the other is converted to long integer;
Guido van Rossum670e5a01992-01-17 14:03:20 +0000538\item otherwise, both must be plain integers and no conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +0000539 is necessary.
540\end{itemize}
541
Guido van Rossum670e5a01992-01-17 14:03:20 +0000542(Note: ``plain integers'' in Python are at least 32 bits in size;
Guido van Rossumf2612d11991-11-21 13:53:03 +0000543``long integers'' are arbitrary precision integers.)
544
545\section{Atoms}
546
Guido van Rossum670e5a01992-01-17 14:03:20 +0000547Atoms are the most basic elements of expressions. Forms enclosed in
548reverse quotes or in parentheses, brackets or braces are also
549categorized syntactically as atoms. The syntax for atoms is:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000550
551\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000552atom: identifier | literal | enclosure
553enclosure: parenth_form | list_display | dict_display | string_conversion
Guido van Rossumf2612d11991-11-21 13:53:03 +0000554\end{verbatim}
555
556\subsection{Identifiers (Names)}
557
558An identifier occurring as an atom is a reference to a local, global
Guido van Rossum670e5a01992-01-17 14:03:20 +0000559or built-in name binding. If a name can be assigned to anywhere in a
560code block, and is not mentioned in a \verb\global\ statement in that
561code block, it refers to a local name throughout that code block.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000562Otherwise, it refers to a global name if one exists, else to a
563built-in name.
564
Guido van Rossum670e5a01992-01-17 14:03:20 +0000565When the name is bound to an object, evaluation of the atom yields
566that object. When a name is not bound, an attempt to evaluate it
567raises a {\tt NameError} exception.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000568
569\subsection{Literals}
570
Guido van Rossum670e5a01992-01-17 14:03:20 +0000571Python knows string and numeric literals:
572
573\begin{verbatim}
574literal: stringliteral | integer | longinteger | floatnumber
575\end{verbatim}
576
Guido van Rossumf2612d11991-11-21 13:53:03 +0000577Evaluation of a literal yields an object of the given type
578(string, integer, long integer, floating point number)
579with the given value.
580The value may be approximated in the case of floating point literals.
581
Guido van Rossum670e5a01992-01-17 14:03:20 +0000582All literals correspond to immutable data types, and hence the
583object's identity is less important than its value. Multiple
584evaluations of literals with the same value (either the same
585occurrence in the program text or a different occurrence) may obtain
586the same object or a different object with the same value.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000587
588(In the original implementation, all literals in the same code block
589with the same type and value yield the same object.)
590
Guido van Rossum670e5a01992-01-17 14:03:20 +0000591\subsection{Parenthesized form}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000592
Guido van Rossum670e5a01992-01-17 14:03:20 +0000593A parenthesized form is an optional condition list enclosed in
594parentheses:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000595
Guido van Rossum670e5a01992-01-17 14:03:20 +0000596\begin{verbatim}
597parenth_form: "(" [condition_list] ")"
598\end{verbatim}
Guido van Rossumf2612d11991-11-21 13:53:03 +0000599
Guido van Rossum670e5a01992-01-17 14:03:20 +0000600A parenthesized condition list yields whatever that condition list
601yields.
602
603An empty pair of parentheses yields an empty tuple object (since
604tuples are immutable, the rules for literals apply here).
605
606(Note that tuples are not formed by the parentheses, but rather by use
607of the comma operator. The exception is the empty tuple, for which
608parentheses {\em are} required -- allowing unparenthesized ``nothing''
609in expressions would causes ambiguities and allow common typos to
610pass uncaught.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000611
612\subsection{List displays}
613
Guido van Rossum670e5a01992-01-17 14:03:20 +0000614A list display is a possibly empty series of conditions enclosed in
615square brackets:
616
617\begin{verbatim}
618list_display: "[" [condition_list] "]"
619\end{verbatim}
620
Guido van Rossumf2612d11991-11-21 13:53:03 +0000621A list display yields a new list object.
622
623If it has no condition list, the list object has no items.
624Otherwise, the elements of the condition list are evaluated
625from left to right and inserted in the list object in that order.
626
627\subsection{Dictionary displays}
628
Guido van Rossum670e5a01992-01-17 14:03:20 +0000629A dictionary display is a possibly empty series of key/datum pairs
630enclosed in curly braces:
631
632\begin{verbatim}
633dict_display: "{" [key_datum_list] "}"
634key_datum_list: [key_datum ("," key_datum)* [","]
635key_datum: condition ":" condition
636\end{verbatim}
637
Guido van Rossumf2612d11991-11-21 13:53:03 +0000638A dictionary display yields a new dictionary object.
639
Guido van Rossum670e5a01992-01-17 14:03:20 +0000640The key/datum pairs are evaluated from left to right to define the
641entries of the dictionary: each key object is used as a key into the
642dictionary to store the corresponding datum.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000643
Guido van Rossum670e5a01992-01-17 14:03:20 +0000644Keys must be strings, otherwise a {\tt TypeError} exception is raised.%
645\footnote{
646This restriction may be lifted in a future version of the language.
647}
648Clashes between duplicate keys are not detected; the last datum
649(textually rightmost in the display) stored for a given key value
650prevails.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000651
652\subsection{String conversions}
653
Guido van Rossum670e5a01992-01-17 14:03:20 +0000654A string conversion is a condition list enclosed in {\em reverse} (or
655backward) quotes:
656
657\begin{verbatim}
658string_conversion: "`" condition_list "`"
659\end{verbatim}
660
Guido van Rossumf2612d11991-11-21 13:53:03 +0000661A string conversion evaluates the contained condition list and converts the
662resulting object into a string according to rules specific to its type.
663
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000664If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossum670e5a01992-01-17 14:03:20 +0000665dictionary containing only objects whose type is one of these, the
666resulting string is a valid Python expression which can be passed to
667the built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +0000668same value (or an approximation, if floating point numbers are
669involved).
670
671(In particular, converting a string adds quotes around it and converts
672``funny'' characters to escape sequences that are safe to print.)
673
Guido van Rossum670e5a01992-01-17 14:03:20 +0000674It is illegal to attempt to convert recursive objects (e.g., lists or
675dictionaries that contain a reference to themselves, directly or
676indirectly.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000677
678\section{Primaries}
679
680Primaries represent the most tightly bound operations of the language.
681Their syntax is:
682
683\begin{verbatim}
Guido van Rossum670e5a01992-01-17 14:03:20 +0000684primary: atom | attributeref | subscription | slicing | call
Guido van Rossumf2612d11991-11-21 13:53:03 +0000685\end{verbatim}
686
687\subsection{Attribute references}
688
Guido van Rossum670e5a01992-01-17 14:03:20 +0000689An attribute reference is a primary followed by a period and a name:
690
691\begin{verbatim}
692attributeref: primary "." identifier
693\end{verbatim}
694
695The primary must evaluate to an object of a type that supports
696attribute references, e.g., a module or a list. This object is then
697asked to produce the attribute whose name is the identifier. If this
698attribute is not available, the exception \verb\AttributeError\ is
699raised. Otherwise, the type and value of the object produced is
700determined by the object. Multiple evaluations of the same attribute
701reference may yield different objects.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000702
703\subsection{Subscriptions}
704
Guido van Rossum670e5a01992-01-17 14:03:20 +0000705A subscription selects an item of a sequence or mapping object:
706
707\begin{verbatim}
708subscription: primary "[" condition "]"
709\end{verbatim}
710
711The primary must evaluate to an object of a sequence or mapping type.
712
713If it is a mapping, the condition must evaluate to an object whose
714value is one of the keys of the mapping, and the subscription selects
715the value in the mapping that corresponds to that key.
716
717If it is a sequence, the condition must evaluate to a nonnegative
718plain integer smaller than the number of items in the sequence, and
719the subscription selects the item whose index is that value (counting
720from zero).
721
722A string's items are characters. A character is not a separate data
723type but a string of exactly one character.
724
Guido van Rossumf2612d11991-11-21 13:53:03 +0000725\subsection{Slicings}
726
Guido van Rossum670e5a01992-01-17 14:03:20 +0000727A slicing selects a range of items in a sequence object:
728
729\begin{verbatim}
730slicing: primary "[" [condition] ":" [condition] "]"
731\end{verbatim}
732
733XXX
734
735\subsection{Calls}
736
737A call calls a function with a possibly empty series of arguments:
738
739\begin{verbatim}
740call: primary "(" [condition_list] ")"
741\end{verbatim}
742
743The primary must evaluate to a callable object. Callable objects are
744user-defined functions, built-in functions, methods of built-in
745objects (``built-in methods''), class objects, and methods of class
746instances (``user-defined methods''). If it is a class, the argument
747list must be empty.
748
749XXX explain what happens on function call
750
Guido van Rossumf2612d11991-11-21 13:53:03 +0000751\section{Factors}
752
753Factors represent the unary numeric operators.
754Their syntax is:
755
756\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000757factor: primary | "-" factor | "+" factor | "~" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +0000758\end{verbatim}
759
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000760The unary \verb\-\ operator yields the negative of its numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000761
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000762The unary \verb\+\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000763
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000764The unary \verb\~\ operator yields the bit-wise negation of its
Guido van Rossum670e5a01992-01-17 14:03:20 +0000765(plain or long) integral numerical argument, using 2's complement.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000766
767In all three cases, if the argument does not have the proper type,
768a {\tt TypeError} exception is raised.
769
770\section{Terms}
771
772Terms represent the most tightly binding binary operators:
773
774\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000775term: factor | term "*" factor | term "/" factor | term "%" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +0000776\end{verbatim}
777
Guido van Rossum670e5a01992-01-17 14:03:20 +0000778The \verb\*\ (multiplication) operator yields the product of its
779arguments. The arguments must either both be numbers, or one argument
780must be a plain integer and the other must be a sequence. In the
781former case, the numbers are converted to a common type and then
782multiplied together. In the latter case, sequence repetition is
783performed; a negative repetition factor yields the empty string.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000784
Guido van Rossum670e5a01992-01-17 14:03:20 +0000785The \verb|"/"| (division) operator yields the quotient of its
786arguments. The numeric arguments are first converted to a common
787type. (Plain or long) integer division yields an integer of the same
788type; the result is that of mathematical division with the {\em floor}
789operator applied to the result, to match the modulo operator.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000790Division by zero raises a {\tt RuntimeError} exception.
791
Guido van Rossum670e5a01992-01-17 14:03:20 +0000792The \verb|"%"| (modulo) operator yields the remainder from the
793division of the first argument by the second. The numeric arguments
794are first converted to a common type. A zero right argument raises a
795{\tt RuntimeError} exception. The arguments may be floating point
796numbers, e.g., $3.14 \% 0.7$ equals $0.34$. The modulo operator
797always yields a result with the same sign as its second operand (or
798zero); the absolute value of the result is strictly smaller than the
799second operand.
800
801The integer division and modulo operators are connected by the
802following identity: $x = (x/y)*y + (x\%y)$.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000803
804\section{Arithmetic expressions}
805
806\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000807arith_expr: term | arith_expr "+" term | arith_expr "-" term
Guido van Rossumf2612d11991-11-21 13:53:03 +0000808\end{verbatim}
809
Guido van Rossum670e5a01992-01-17 14:03:20 +0000810HIRO
811
812The \verb|"+"| operator yields the sum of its arguments. The
813arguments must either both be numbers, or both sequences. In the
814former case, the numbers are converted to a common type and then added
815together. In the latter case, the sequences are concatenated
816directly.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000817
Guido van Rossum743d1e71992-01-07 16:43:53 +0000818The \verb|"-"| operator yields the difference of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000819The numeric arguments are first converted to a common type.
820
821\section{Shift expressions}
822
823\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000824shift_expr: arith_expr | shift_expr "<<" arith_expr | shift_expr ">>" arith_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000825\end{verbatim}
826
Guido van Rossum670e5a01992-01-17 14:03:20 +0000827These operators accept (plain) integers as arguments only.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000828They shift their left argument to the left or right by the number of bits
Guido van Rossum743d1e71992-01-07 16:43:53 +0000829given by the right argument. Shifts are ``logical"", e.g., bits shifted
Guido van Rossumf2612d11991-11-21 13:53:03 +0000830out on one end are lost, and bits shifted in are zero;
831negative numbers are shifted as if they were unsigned in C.
832Negative shift counts and shift counts greater than {\em or equal to}
833the word size yield undefined results.
834
835\section{Bitwise AND expressions}
836
837\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000838and_expr: shift_expr | and_expr "&" shift_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000839\end{verbatim}
840
841This operator yields the bitwise AND of its arguments,
Guido van Rossum670e5a01992-01-17 14:03:20 +0000842which must be (plain) integers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000843
844\section{Bitwise XOR expressions}
845
846\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000847xor_expr: and_expr | xor_expr "^" and_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000848\end{verbatim}
849
850This operator yields the bitwise exclusive OR of its arguments,
Guido van Rossum670e5a01992-01-17 14:03:20 +0000851which must be (plain) integers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000852
853\section{Bitwise OR expressions}
854
855\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000856or_expr: xor_expr | or_expr "|" xor_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000857\end{verbatim}
858
859This operator yields the bitwise OR of its arguments,
Guido van Rossum670e5a01992-01-17 14:03:20 +0000860which must be (plain) integers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000861
862\section{Expressions and expression lists}
863
864\begin{verbatim}
865expression: or_expression
Guido van Rossum743d1e71992-01-07 16:43:53 +0000866expr_list: expression ("," expression)* [","]
Guido van Rossumf2612d11991-11-21 13:53:03 +0000867\end{verbatim}
868
869An expression list containing at least one comma yields a new tuple.
870The length of the tuple is the number of expressions in the list.
871The expressions are evaluated from left to right.
872
873The trailing comma is required only to create a single tuple;
874it is optional in all other cases (a single expression without
875a trailing comma doesn't create a tuple, but rather yields the
876value of that expression).
877
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000878To create an empty tuple, use an empty pair of parentheses: \verb\()\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000879
880\section{Comparisons}
881
882\begin{verbatim}
883comparison: expression (comp_operator expression)*
Guido van Rossum743d1e71992-01-07 16:43:53 +0000884comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000885\end{verbatim}
886
887Comparisons yield integer value: 1 for true, 0 for false.
888
889Comparisons can be chained arbitrarily,
890e.g., $x < y <= z$ is equivalent to
891$x < y$ {\tt and} $y <= z$, except that $y$ is evaluated only once
892(but in both cases $z$ is not evaluated at all when $x < y$ is
893found to be false).
894
895Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to
896$e_0 op_1 e_1$ {\tt and} $e_1 op_2 e_2$ {\tt and} ... {\tt and}
897$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
898
899Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
900between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
901
Guido van Rossum743d1e71992-01-07 16:43:53 +0000902The forms \verb\<>\ and \verb\!=\ are equivalent.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000903
Guido van Rossum743d1e71992-01-07 16:43:53 +0000904The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "<>"} compare
Guido van Rossumf2612d11991-11-21 13:53:03 +0000905the values of two objects. The objects needn't have the same type.
906If both are numbers, they are compared to a common type.
907Otherwise, objects of different types {\em always} compare unequal,
908and are ordered consistently but arbitrarily, except that
909the value \verb\None\ compares smaller than the values of any other type.
910
911(This unusual
912definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000913operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000914
915Comparison of objects of the same type depends on the type:
916
917\begin{itemize}
918\item Numbers are compared arithmetically.
919\item Strings are compared lexicographically using the numeric
920 equivalents (the result of the built-in function ord())
921 of their characters.
922\item Tuples and lists are compared lexicographically
923 using comparison of corresponding items.
924\item Dictionaries compare unequal unless they are the same object;
925 the choice whether one dictionary object is considered smaller
926 or larger than another one is made arbitrarily but
927 consistently within one execution of a program.
928\item The latter rule is also used for most other built-in types.
929\end{itemize}
930
931The operators \verb\in\ and \verb\not in\ test for sequence membership:
932if $y$ is a sequence, $x {\tt in} y$ is true if and only if there exists
933an index $i$ such that $x = y_i$.
934$x {\tt not in} y$ yields the inverse truth value.
935The exception {\tt TypeError} is raised when $y$ is not a sequence,
936or when $y$ is a string and $x$ is not a string of length one.
937
938The operators \verb\is\ and \verb\is not\ compare object identity:
939$x {\tt is} y$ is true if and only if $x$ and $y$ are the same object.
940$x {\tt is not} y$ yields the inverse truth value.
941
942\section{Boolean operators}
943
944\begin{verbatim}
945condition: or_test
Guido van Rossum743d1e71992-01-07 16:43:53 +0000946or_test: and_test | or_test "or" and_test
947and_test: not_test | and_test "and" not_test
948not_test: comparison | "not" not_test
Guido van Rossumf2612d11991-11-21 13:53:03 +0000949\end{verbatim}
950
951In the context of Boolean operators, and also when conditions are
952used by control flow statements, the following values are interpreted
953as false: None, numeric zero of all types, empty sequences (strings,
954tuples and lists), and empty mappings (dictionaries).
955All other values are interpreted as true.
956
957The operator \verb\not\ yields 1 if its argument is false, 0 otherwise.
958
959The condition $x {\tt and} y$ first evaluates $x$; if $x$ is false,
960$x$ is returned; otherwise, $y$ is evaluated and returned.
961
962The condition $x {\tt or} y$ first evaluates $x$; if $x$ is true,
963$x$ is returned; otherwise, $y$ is evaluated and returned.
964
965(Note that \verb\and\ and \verb\or\ do not restrict the value and type
966they return to 0 and 1, but rather return the last evaluated argument.
967This is sometimes useful, e.g., if $s$ is a string, which should be
968replaced by a default value if it is empty, $s {\tt or} 'foo'$
969returns the desired value. Because \verb\not\ has to invent a value
970anyway, it does not bother to return a value of the same type as its
971argument, so \verb\not 'foo'\ yields $0$, not $''$.)
972
973\chapter{Simple statements}
974
975Simple statements are comprised within a single logical line.
976Several simple statements may occor on a single line separated
977by semicolons. The syntax for simple statements is:
978
979\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000980stmt_list: simple_stmt (";" simple_stmt)* [";"]
Guido van Rossumf2612d11991-11-21 13:53:03 +0000981simple_stmt: expression_stmt
982 | assignment
983 | pass_stmt
984 | del_stmt
985 | print_stmt
986 | return_stmt
987 | raise_stmt
988 | break_stmt
989 | continue_stmt
990 | import_stmt
Guido van Rossum743d1e71992-01-07 16:43:53 +0000991 | global_stmt
Guido van Rossumf2612d11991-11-21 13:53:03 +0000992\end{verbatim}
993
994\section{Expression statements}
995
996\begin{verbatim}
997expression_stmt: expression_list
998\end{verbatim}
999
1000An expression statement evaluates the expression list (which may
1001be a single expression).
1002If the value is not \verb\None\, it is converted to a string
1003using the rules for string conversions, and the resulting string
1004is written to standard output on a line by itself.
1005
1006(The exception for \verb\None\ is made so that procedure calls,
1007which are syntactically equivalent to expressions,
1008do not cause any output.)
1009
1010\section{Assignments}
1011
1012\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001013assignment: target_list ("=" target_list)* "=" expression_list
1014target_list: target ("," target)* [","]
1015target: identifier | "(" target_list ")" | "[" target_list "]"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001016 | attributeref | subscription | slicing
1017\end{verbatim}
1018
1019(See the section on primaries for the definition of the last
1020three symbols.)
1021
1022An assignment evaluates the expression list (remember that this can
1023be a single expression or a comma-separated list,
1024the latter yielding a tuple)
1025and assigns the single resulting object to each of the target lists,
1026from left to right.
1027
1028Assignment is defined recursively depending on the type of the
1029target. Where assignment is to part of a mutable object
1030(through an attribute reference, subscription or slicing),
1031the mutable object must ultimately perform the
1032assignment and decide about its validity, raising an exception
1033if the assignment is unacceptable. The rules observed by
1034various types and the exceptions raised are given with the
1035definition of the object types (some of which are defined
1036in the library reference).
1037
1038Assignment of an object to a target list is recursively
1039defined as follows.
1040
1041\begin{itemize}
1042\item
1043If the target list contains no commas (except in nested constructs):
1044the object is assigned to the single target contained in the list.
1045
1046\item
1047If the target list contains commas (that are not in nested constructs):
1048the object must be a tuple with as many items
1049as the list contains targets, and the items are assigned, from left
1050to right, to the corresponding targets.
1051
1052\end{itemize}
1053
1054Assignment of an object to a (non-list)
1055target is recursively defined as follows.
1056
1057\begin{itemize}
1058
1059\item
1060If the target is an identifier (name):
1061the object is bound to that name
1062in the current local scope. Any previous binding of the same name
1063is undone.
1064
1065\item
1066If the target is a target list enclosed in parentheses:
1067the object is assigned to that target list.
1068
1069\item
1070If the target is a target list enclosed in square brackets:
1071the object must be a list with as many items
1072as the target list contains targets,
1073and the list's items are assigned, from left to right,
1074to the corresponding targets.
1075
1076\item
1077If the target is an attribute reference:
1078The primary expression in the reference is evaluated.
1079It should yield an object with assignable attributes;
1080if this is not the case, a {\tt TypeError} exception is raised.
1081That object is then asked to assign the assigned object
1082to the given attribute; if it cannot perform the assignment,
1083it raises an exception.
1084
1085\item
1086If the target is a subscription:
1087The primary expression in the reference is evaluated.
1088It should yield either a mutable sequence object or a mapping
1089(dictionary) object.
1090Next, the subscript expression is evaluated.
1091
1092If the primary is a sequence object, the subscript must yield a
1093nonnegative integer smaller than the sequence's length,
1094and the sequence is asked to assign the assigned object
1095to its item with that index.
1096
1097If the primary is a mapping object, the subscript must have a
1098type compatible with the mapping's key type,
1099and the mapping is then asked to to create a key/datum pair
1100which maps the subscript to the assigned object.
1101
1102Various exceptions can be raised.
1103
1104\item
1105If the target is a slicing:
1106The primary expression in the reference is evaluated.
1107It should yield a mutable sequence object (currently only lists).
1108The assigned object should be a sequence object of the same type.
1109Next, the lower and upper bound expressions are evaluated,
1110insofar they are present; defaults are zero and the sequence's length.
1111The bounds should evaluate to (small) integers.
1112If either bound is negative, the sequence's length is added to it (once).
1113The resulting bounds are clipped to lie between zero
1114and the sequence's length, inclusive.
1115(XXX Shouldn't this description be with expressions?)
1116Finally, the sequence object is asked to replace the items
1117indicated by the slice with the items of the assigned sequence.
1118This may change the sequence's length, if it allows it.
1119
1120\end{itemize}
1121
1122(In the original implementation, the syntax for targets is taken
1123to be the same as for expressions, and invalid syntax is rejected
1124during the code generation phase, causing less detailed error
1125messages.)
1126
1127\section{The {\tt pass} statement}
1128
1129\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001130pass_stmt: "pass"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001131\end{verbatim}
1132
1133{\tt pass} is a null operation -- when it is executed,
1134nothing happens.
1135
1136\section{The {\tt del} statement}
1137
1138\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001139del_stmt: "del" target_list
Guido van Rossumf2612d11991-11-21 13:53:03 +00001140\end{verbatim}
1141
1142Deletion is recursively defined similar to assignment.
1143
1144(XXX Rather that spelling it out in full details,
1145here are some hints.)
1146
1147Deletion of a target list recursively deletes each target,
1148from left to right.
1149
1150Deletion of a name removes the binding of that name (which must exist)
1151from the local scope.
1152
1153Deletion of attribute references, subscriptions and slicings
1154is passed to the primary object involved; deletion of a slicing
1155is in general equivalent to assignment of an empty slice of the
1156right type (but even this is determined by the sliced object).
1157
1158\section{The {\tt print} statement}
1159
1160\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001161print_stmt: "print" [ condition ("," condition)* [","] ]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001162\end{verbatim}
1163
1164{\tt print} evaluates each condition in turn and writes the resulting
1165object to standard output (see below).
1166If an object is not a string, it is first converted to
1167a string using the rules for string conversions.
1168The (resulting or original) string is then written.
1169A space is written before each object is (converted and) written,
1170unless the output system believes it is positioned at the beginning
1171of a line. This is the case: (1) when no characters have been written
1172to standard output; or (2) when the last character written to
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001173standard output is \verb/\n/;
Guido van Rossumf2612d11991-11-21 13:53:03 +00001174or (3) when the last I/O operation
1175on standard output was not a \verb\print\ statement.
1176
1177Finally,
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001178a \verb/\n/ character is written at the end,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001179unless the \verb\print\ statement ends with a comma.
1180This is the only action if the statement contains just the keyword
1181\verb\print\.
1182
1183Standard output is defined as the file object named \verb\stdout\
1184in the built-in module \verb\sys\. If no such object exists,
1185or if it is not a writable file, a {\tt RuntimeError} exception is raised.
1186(The original implementation attempts to write to the system's original
1187standard output instead, but this is not safe, and should be fixed.)
1188
1189\section{The {\tt return} statement}
1190
1191\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001192return_stmt: "return" [condition_list]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001193\end{verbatim}
1194
1195\verb\return\ may only occur syntactically nested in a function
1196definition, not within a nested class definition.
1197
1198If a condition list is present, it is evaluated, else \verb\None\
1199is substituted.
1200
1201\verb\return\ leaves the current function call with the condition
1202list (or \verb\None\) as return value.
1203
1204When \verb\return\ passes control out of a \verb\try\ statement
1205with a \verb\finally\ clause, that finally clause is executed
1206before really leaving the function.
1207(XXX This should be made more exact, a la Modula-3.)
1208
1209\section{The {\tt raise} statement}
1210
1211\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001212raise_stmt: "raise" condition ["," condition]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001213\end{verbatim}
1214
1215\verb\raise\ evaluates its first condition, which must yield
1216a string object. If there is a second condition, this is evaluated,
1217else \verb\None\ is substituted.
1218
1219It then raises the exception identified by the first object,
1220with the second one (or \verb\None\) as its parameter.
1221
1222\section{The {\tt break} statement}
1223
1224\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001225break_stmt: "break"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001226\end{verbatim}
1227
1228\verb\break\ may only occur syntactically nested in a \verb\for\
1229or \verb\while\ loop, not nested in a function or class definition.
1230
1231It terminates the neares enclosing loop, skipping the optional
1232\verb\else\ clause if the loop has one.
1233
1234If a \verb\for\ loop is terminated by \verb\break\, the loop control
1235target (list) keeps its current value.
1236
1237When \verb\break\ passes control out of a \verb\try\ statement
1238with a \verb\finally\ clause, that finally clause is executed
1239before really leaving the loop.
1240
1241\section{The {\tt continue} statement}
1242
1243\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001244continue_stmt: "continue"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001245\end{verbatim}
1246
1247\verb\continue\ may only occur syntactically nested in a \verb\for\
1248or \verb\while\ loop, not nested in a function or class definition,
1249and {\em not nested in a \verb\try\ statement with a \verb\finally\
1250clause}.
1251
1252It continues with the next cycle of the nearest enclosing loop.
1253
1254\section{The {\tt import} statement}
1255
1256\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001257import_stmt: "import" identifier ("," identifier)*
1258 | "from" identifier "import" identifier ("," identifier)*
1259 | "from" identifier "import" "*"
1260\end{verbatim}
1261
1262(XXX To be done.)
1263
1264\section{The {\tt global} statement}
1265
1266\begin{verbatim}
1267global_stmt: "global" identifier ("," identifier)*
Guido van Rossumf2612d11991-11-21 13:53:03 +00001268\end{verbatim}
1269
1270(XXX To be done.)
1271
1272\chapter{Compound statements}
1273
1274(XXX The semantic definitions of this chapter are still to be done.)
1275
1276\begin{verbatim}
1277statement: stmt_list NEWLINE | compound_stmt
1278compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1279suite: statement | NEWLINE INDENT statement+ DEDENT
1280\end{verbatim}
1281
1282\section{The {\tt if} statement}
1283
1284\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001285if_stmt: "if" condition ":" suite
1286 ("elif" condition ":" suite)*
1287 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001288\end{verbatim}
1289
1290\section{The {\tt while} statement}
1291
1292\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001293while_stmt: "while" condition ":" suite ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001294\end{verbatim}
1295
1296\section{The {\tt for} statement}
1297
1298\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001299for_stmt: "for" target_list "in" condition_list ":" suite
1300 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001301\end{verbatim}
1302
1303\section{The {\tt try} statement}
1304
1305\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001306try_stmt: "try" ":" suite
1307 ("except" condition ["," condition] ":" suite)*
1308 ["finally" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001309\end{verbatim}
1310
1311\section{Function definitions}
1312
1313\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001314funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
1315parameter_list: parameter ("," parameter)*
1316parameter: identifier | "(" parameter_list ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001317\end{verbatim}
1318
1319\section{Class definitions}
1320
1321\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001322classdef: "class" identifier [inheritance] ":" suite
1323inheritance: "(" expression ("," expression)* ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001324\end{verbatim}
1325
1326XXX Syntax for scripts, modules
1327XXX Syntax for interactive input, eval, exec, input
Guido van Rossum743d1e71992-01-07 16:43:53 +00001328XXX New definition of expressions (as conditions)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001329
1330\end{document}