blob: f659569c962c3dab31911abb6e9f88ea7e543efc [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
52\tableofcontents
53
54\pagebreak
55
56\pagenumbering{arabic}
57
58\chapter{Introduction}
59
60This reference manual describes the Python programming language.
61It is not intended as a tutorial.
62
Guido van Rossum743d1e71992-01-07 16:43:53 +000063While I am trying to be as precise as possible, I chose to use English
64rather than formal specifications for everything except syntax and
65lexical analysis. This should make the document better understandable
66to the average reader, but will leave room for ambiguities.
67Consequently, if you were coming from Mars and tried to re-implement
Guido van Rossum7b632a61992-01-16 17:49:21 +000068Python from this document alone, you might have to guess things and in
69fact you would be implementing quite a different language.
70On the other hand, if you are using
Guido van Rossum743d1e71992-01-07 16:43:53 +000071Python and wonder what the precise rules about a particular area of
Guido van Rossum7b632a61992-01-16 17:49:21 +000072the language are, you should definitely be able to find it here.
Guido van Rossum743d1e71992-01-07 16:43:53 +000073
74It is dangerous to add too many implementation details to a language
75reference document -- the implementation may change, and other
76implementations of the same language may work differently. On the
77other hand, there is currently only one Python implementation, and
Guido van Rossum7b632a61992-01-16 17:49:21 +000078its particular quirks are sometimes worth being mentioned, especially
79where the implementation imposes additional limitations.
Guido van Rossum743d1e71992-01-07 16:43:53 +000080
81Every Python implementation comes with a number of built-in and
82standard modules. These are not documented here, but in the separate
83{\em Python Library Reference} document. A few built-in modules are
84mentioned when they interact in a significant way with the language
85definition.
86
87\section{Notation}
88
89The descriptions of lexical analysis and syntax use a modified BNF
90grammar notation. This uses the following style of definition:
91
92\begin{verbatim}
93name: lcletter (lcletter | "_")*
94lcletter: "a"..."z"
95\end{verbatim}
96
Guido van Rossum7b632a61992-01-16 17:49:21 +000097The first line says that a \verb\name\ is an \verb\lcletter\ followed by
98a sequence of zero or more \verb\lcletter\s and underscores. An
Guido van Rossum743d1e71992-01-07 16:43:53 +000099\verb\lcletter\ in turn is any of the single characters `a' through `z'.
100(This rule is actually adhered to for the names defined in syntax and
101grammar rules in this document.)
102
103Each rule begins with a name (which is the name defined by the rule)
Guido van Rossum7b632a61992-01-16 17:49:21 +0000104and a colon, and is wholly contained on one line. A vertical bar
105(\verb\|\) is used to separate alternatives; it is the least binding
106operator in this notation. A star (\verb\*\) means zero or more
107repetitions of the preceding item; likewise, a plus (\verb\+\) means
108one or more repetitions, and a question mark (\verb\?\) zero or one
109(in other words, the preceding item is optional). These three
110operators bind as tightly as possible; parentheses are used for
Guido van Rossum743d1e71992-01-07 16:43:53 +0000111grouping. Literal strings are enclosed in double quotes. White space
112is only meaningful to separate tokens.
113
114In lexical definitions (as the example above), two more conventions
115are used: Two literal characters separated by three dots mean a choice
116of any single character in the given (inclusive) range of ASCII
117characters. A phrase between angular brackets (\verb\<...>\) gives an
118informal description of the symbol defined; e.g., this could be used
119to describe the notion of `control character' if needed.
120
Guido van Rossum7b632a61992-01-16 17:49:21 +0000121Even though the notation used is almost the same, there is a big
Guido van Rossum743d1e71992-01-07 16:43:53 +0000122difference between the meaning of lexical and syntactic definitions:
123a lexical definition operates on the individual characters of the
124input source, while a syntax definition operates on the stream of
125tokens generated by the lexical analysis.
126
Guido van Rossumf2612d11991-11-21 13:53:03 +0000127\chapter{Lexical analysis}
128
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000129A Python program is read by a {\em parser}. Input to the parser is a
130stream of {\em tokens}, generated by the {\em lexical analyzer}. This
131chapter describes how the lexical analyzer breaks a file into tokens.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000132
133\section{Line structure}
134
Guido van Rossum7b632a61992-01-16 17:49:21 +0000135A Python program is divided in a number of logical lines. The end of
136a logical line is represented by the token NEWLINE. Statements cannot
137cross logical line boundaries except where NEWLINE is allowed by the
138syntax (e.g., between statements in compound statements).
Guido van Rossumf2612d11991-11-21 13:53:03 +0000139
140\subsection{Comments}
141
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000142A comment starts with a hash character (\verb\#\) that is not part of
Guido van Rossum7b632a61992-01-16 17:49:21 +0000143a string literal, and ends at the end of the physical line. A comment
144always signifies the end of the logical line. Comments are ignored by
145the syntax.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000146
147\subsection{Line joining}
148
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000149Two or more physical lines may be joined into logical lines using
Guido van Rossum7b632a61992-01-16 17:49:21 +0000150backslash characters (\verb/\/), as follows: when a physical line ends
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000151in a backslash that is not part of a string literal or comment, it is
152joined with the following forming a single logical line, deleting the
153backslash and the following end-of-line character.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000154
155\subsection{Blank lines}
156
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000157A logical line that contains only spaces, tabs, and possibly a
158comment, is ignored (i.e., no NEWLINE token is generated), except that
159during interactive input of statements, an entirely blank logical line
160terminates a multi-line statement.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000161
162\subsection{Indentation}
163
Guido van Rossum7b632a61992-01-16 17:49:21 +0000164Leading whitespace (spaces and tabs) at the beginning of a logical
165line is used to compute the indentation level of the line, which in
166turn is used to determine the grouping of statements.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000167
Guido van Rossum7b632a61992-01-16 17:49:21 +0000168First, tabs are replaced (from left to right) by one to eight spaces
169such that the total number of characters up to there is a multiple of
170eight (this is intended to be the same rule as used by UNIX). The
171total number of spaces preceding the first non-blank character then
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000172determines the line's indentation. Indentation cannot be split over
173multiple physical lines using backslashes.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000174
175The indentation levels of consecutive lines are used to generate
176INDENT and DEDENT tokens, using a stack, as follows.
177
178Before the first line of the file is read, a single zero is pushed on
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000179the stack; this will never be popped off again. The numbers pushed on
180the stack will always be strictly increasing from bottom to top. At
181the beginning of each logical line, the line's indentation level is
182compared to the top of the stack. If it is equal, nothing happens.
183If it larger, it is pushed on the stack, and one INDENT token is
184generated. If it is smaller, it {\em must} be one of the numbers
185occurring on the stack; all numbers on the stack that are larger are
186popped off, and for each number popped off a DEDENT token is
187generated. At the end of the file, a DEDENT token is generated for
188each number remaining on the stack that is larger than zero.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000189
Guido van Rossum7b632a61992-01-16 17:49:21 +0000190Here is an example of a correctly (though confusingly) indented piece
191of Python code:
192
193\begin{verbatim}
194def perm(l):
195 if len(l) <= 1:
196 return [l]
197 r = []
198 for i in range(len(l)):
199 s = l[:i] + l[i+1:]
200 p = perm(s)
201 for x in p:
202 r.append(l[i:i+1] + x)
203 return r
204\end{verbatim}
205
206The following example shows various indentation errors:
207
208\begin{verbatim}
209 def perm(l): # error: first line indented
210 for i in range(len(l)): # error: not indented
211 s = l[:i] + l[i+1:]
212 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
213 for x in p:
214 r.append(l[i:i+1] + x)
215 return r # error: inconsistent indent
216\end{verbatim}
217
218(Actually, the first three errors are detected by the parser; only the
219last error is found by the lexical analyzer -- the indentation of
220\verb\return r\ does not match a level popped off the stack.)
221
Guido van Rossumf2612d11991-11-21 13:53:03 +0000222\section{Other tokens}
223
224Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
225exist: identifiers, keywords, literals, operators, and delimiters.
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000226Spaces and tabs are not tokens, but serve to delimit tokens. Where
227ambiguity exists, a token comprises the longest possible string that
228forms a legal token, when read from left to right.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000229
Guido van Rossumf2612d11991-11-21 13:53:03 +0000230\section{Identifiers}
231
232Identifiers are described by the following regular expressions:
233
234\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000235identifier: (letter|"_") (letter|digit|"_")*
Guido van Rossumf2612d11991-11-21 13:53:03 +0000236letter: lowercase | uppercase
Guido van Rossum743d1e71992-01-07 16:43:53 +0000237lowercase: "a"..."z"
238uppercase: "A"..."Z"
239digit: "0"..."9"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000240\end{verbatim}
241
Guido van Rossum7b632a61992-01-16 17:49:21 +0000242Identifiers are unlimited in length. Case is significant. Keywords
243are not identifiers.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000244
245\section{Keywords}
246
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000247The following identifiers are used as reserved words, or {\em
Guido van Rossum7b632a61992-01-16 17:49:21 +0000248keywords} of the language, and cannot be used as ordinary
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000249identifiers. They must be spelled exactly as written here:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000250
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000251\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000252and del for in print
253break elif from is raise
254class else global not return
255continue except if or try
256def finally import pass while
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000257\end{verbatim}
258
Guido van Rossum743d1e71992-01-07 16:43:53 +0000259% # This Python program sorts and formats the above table
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000260% import string
261% l = []
262% try:
263% while 1:
264% l = l + string.split(raw_input())
265% except EOFError:
266% pass
267% l.sort()
268% for i in range((len(l)+4)/5):
269% for j in range(i, len(l), 5):
270% print string.ljust(l[j], 10),
271% print
Guido van Rossumf2612d11991-11-21 13:53:03 +0000272
273\section{Literals}
274
275\subsection{String literals}
276
277String literals are described by the following regular expressions:
278
279\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000280stringliteral: "'" stringitem* "'"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000281stringitem: stringchar | escapeseq
Guido van Rossum743d1e71992-01-07 16:43:53 +0000282stringchar: <any ASCII character except newline or "\" or "'">
283escapeseq: "'" <any ASCII character except newline>
Guido van Rossumf2612d11991-11-21 13:53:03 +0000284\end{verbatim}
285
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000286String literals cannot span physical line boundaries. Escape
287sequences in strings are actually interpreted according to rules
288simular to those used by Standard C. The recognized escape sequences
289are:
290
291\begin{center}
292\begin{tabular}{|l|l|}
293\hline
294\verb/\\/ & Backslash (\verb/\/) \\
295\verb/\'/ & Single quote (\verb/'/) \\
296\verb/\a/ & ASCII Bell (BEL) \\
297\verb/\b/ & ASCII Backspace (BS) \\
Guido van Rossum7b632a61992-01-16 17:49:21 +0000298%\verb/\E/ & ASCII Escape (ESC) \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000299\verb/\f/ & ASCII Formfeed (FF) \\
300\verb/\n/ & ASCII Linefeed (LF) \\
301\verb/\r/ & ASCII Carriage Return (CR) \\
302\verb/\t/ & ASCII Horizontal Tab (TAB) \\
303\verb/\v/ & ASCII Vertical Tab (VT) \\
304\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
Guido van Rossum743d1e71992-01-07 16:43:53 +0000305\verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000306\hline
307\end{tabular}
308\end{center}
309
Guido van Rossum7b632a61992-01-16 17:49:21 +0000310In strict compatibility with in Standard C, up to three octal digits are
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000311accepted, but an unlimited number of hex digits is taken to be part of
312the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum7b632a61992-01-16 17:49:21 +0000313are used in all current implementations...).
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000314
Guido van Rossum7b632a61992-01-16 17:49:21 +0000315All unrecognized escape sequences are left in the string unchanged,
316i.e., {\em the backslash is left in the string.} (This rule is
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000317useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum743d1e71992-01-07 16:43:53 +0000318resulting output is more easily recognized as broken. It also helps a
319great deal for string literals used as regular expressions or
320otherwise passed to other modules that do their own escape handling --
321but you may end up quadrupling backslashes that must appear literally.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000322
323\subsection{Numeric literals}
324
325There are three types of numeric literals: integers, long integers,
326and floating point numbers.
327
328Integers and long integers are described by the following regular expressions:
329
330\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000331longinteger: integer ("l"|"L")
Guido van Rossumf2612d11991-11-21 13:53:03 +0000332integer: decimalinteger | octinteger | hexinteger
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000333decimalinteger: nonzerodigit digit* | "0"
334octinteger: "0" octdigit+
335hexinteger: "0" ("x"|"X") hexdigit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000336
Guido van Rossum743d1e71992-01-07 16:43:53 +0000337nonzerodigit: "1"..."9"
338octdigit: "0"..."7"
339hexdigit: digit|"a"..."f"|"A"..."F"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000340\end{verbatim}
341
342Floating point numbers are described by the following regular expressions:
343
344\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000345floatnumber: [intpart] fraction [exponent] | intpart ["."] exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000346intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000347fraction: "." digit+
348exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000349\end{verbatim}
350
Guido van Rossum7b632a61992-01-16 17:49:21 +0000351Some examples of numeric literals:
352
353\begin{verbatim}
3541 1234567890 0177777 0x80000
355
356
357\end{verbatim}
358
359Note that the definitions for literals do not include a sign; a phrase
360like \verb\-1\ is actually an expression composed of the operator
361\verb\-\ and the literal \verb\1\.
362
Guido van Rossumf2612d11991-11-21 13:53:03 +0000363\section{Operators}
364
365The following tokens are operators:
366
367\begin{verbatim}
368+ - * / %
369<< >> & | ^ ~
Guido van Rossum743d1e71992-01-07 16:43:53 +0000370< == > <= <> != >=
Guido van Rossumf2612d11991-11-21 13:53:03 +0000371\end{verbatim}
372
Guido van Rossum743d1e71992-01-07 16:43:53 +0000373The comparison operators \verb\<>\ and \verb\!=\ are alternate
374spellings of the same operator.
375
Guido van Rossumf2612d11991-11-21 13:53:03 +0000376\section{Delimiters}
377
Guido van Rossum743d1e71992-01-07 16:43:53 +0000378The following tokens serve as delimiters or otherwise have a special
379meaning:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000380
381\begin{verbatim}
382( ) [ ] { }
Guido van Rossum743d1e71992-01-07 16:43:53 +0000383; , : . ` =
Guido van Rossumf2612d11991-11-21 13:53:03 +0000384\end{verbatim}
385
Guido van Rossum7b632a61992-01-16 17:49:21 +0000386The following printing ASCII characters are not used in Python (except
387in string literals and in comments). Their occurrence is an
388unconditional error:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000389
390\begin{verbatim}
391! @ $ " ?
392\end{verbatim}
393
Guido van Rossum7b632a61992-01-16 17:49:21 +0000394They may be used by future versions of the language though!
395
Guido van Rossumf2612d11991-11-21 13:53:03 +0000396\chapter{Execution model}
397
Guido van Rossum743d1e71992-01-07 16:43:53 +0000398(XXX This chapter should explain the general model of the execution of
399Python code and the evaluation of expressions. It should introduce
400objects, values, code blocks, scopes, name spaces, name binding,
401types, sequences, numbers, mappings, exceptions, and other technical
402terms needed to make the following chapters concise and exact.)
403
404\section{Objects, values and types}
405
406I won't try to define rigorously here what an object is, but I'll give
407some properties of objects that are important to know about.
408
409Every object has an identity, a type and a value. An object's {\em
410identity} never changes once it has been created; think of it as the
411object's (permanent) address. An object's {\em type} determines the
412operations that an object supports (e.g., can its length be taken?)
413and also defines the ``meaning'' of the object's value; it also never
414changes. The {\em value} of some objects can change; whether an
415object's value can change is a property of its type.
416
417Objects are never explicitly destroyed; however, when they become
418unreachable they may be garbage-collected. An implementation,
419however, is allowed to delay garbage collection or omit it altogether
420-- it is a matter of implementation quality how garbage collection is
421implemented. (Implementation note: the current implementation uses a
422reference-counting scheme which collects most objects as soon as they
423become onreachable, but does not detect garbage containing circular
424references.)
425
426(Some objects contain references to ``external'' resources such as
427open files. It is understood that these resources are freed when the
428object is garbage-collected, but since garbage collection is not
429guaranteed such objects also provide an explicit way to release the
430external resource (e.g., a \verb\close\ method) and programs are
431recommended to use this.)
432
433Some objects contain references to other objects. These references
434are part of the object's value; in most cases, when such a
435``container'' object is compared to another (of the same type), the
436comparison takes the {\em values} of the referenced objects into
437account (not their identities).
438
439Except for their identity, types affect almost any aspect of objects.
440Even object identities are affected in some sense: for immutable
441types, operations that compute new values may actually return a
442reference to an existing object with the same type and value, while
443for mutable objects this is not allowed. E.g., after
444
445\begin{verbatim}
446a = 1; b = 1; c = []; d = []
447\end{verbatim}
448
449\verb\a\ and \verb\b\ may or may not refer to the same object, but
450\verb\c\ and \verb\d\ are guaranteed to refer to two different, unique,
451newly created lists.
452
453\section{Execution frames, name spaces, and scopes}
454
455XXX
Guido van Rossumf2612d11991-11-21 13:53:03 +0000456
457\chapter{Expressions and conditions}
458
Guido van Rossum743d1e71992-01-07 16:43:53 +0000459From now on, extended BNF notation will be used to describe syntax,
460not lexical analysis.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000461
462This chapter explains the meaning of the elements of expressions and
463conditions. Conditions are a superset of expressions, and a condition
464may be used where an expression is required by enclosing it in
Guido van Rossum743d1e71992-01-07 16:43:53 +0000465parentheses. The only place where an unparenthesized condition is not
466allowed is on the right-hand side of the assignment operator, because
467this operator is the same token (\verb\=\) as used for compasisons.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000468
Guido van Rossum743d1e71992-01-07 16:43:53 +0000469The comma plays a somewhat special role in Python's syntax. It is an
470operator with a lower precedence than all others, but occasionally
471serves other purposes as well (e.g., it has special semantics in print
472statements). When a comma is accepted by the syntax, one of the
473syntactic categories \verb\expression_list\ or \verb\condition_list\
474is always used.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000475
476When (one alternative of) a syntax rule has the form
477
478\begin{verbatim}
479name: othername
480\end{verbatim}
481
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000482and no semantics are given, the semantics of this form of \verb\name\
483are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000484
485\section{Arithmetic conversions}
486
487When a description of an arithmetic operator below uses the phrase
488``the numeric arguments are converted to a common type'',
489this both means that if either argument is not a number, a
490{\tt TypeError} exception is raised, and that otherwise
491the following conversions are applied:
492
493\begin{itemize}
494\item First, if either argument is a floating point number,
495 the other is converted to floating point;
496\item else, if either argument is a long integer,
497 the other is converted to long integer;
498\item otherwise, both must be short integers and no conversion
499 is necessary.
500\end{itemize}
501
502(Note: ``short integers'' in Python are at least 32 bits in size;
503``long integers'' are arbitrary precision integers.)
504
505\section{Atoms}
506
507Atoms are the most basic elements of expressions.
508Forms enclosed in reverse quotes or various types of parentheses
509or braces are also categorized syntactically as atoms.
510Syntax rules for atoms:
511
512\begin{verbatim}
513atom: identifier | literal | parenth_form | string_conversion
514literal: stringliteral | integer | longinteger | floatnumber
515parenth_form: enclosure | list_display | dict_display
Guido van Rossum743d1e71992-01-07 16:43:53 +0000516enclosure: "(" [condition_list] ")"
517list_display: "[" [condition_list] "]"
518dict_display: "{" [key_datum ("," key_datum)* [","] "}"
519key_datum: condition ":" condition
520string_conversion:"`" condition_list "`"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000521\end{verbatim}
522
523\subsection{Identifiers (Names)}
524
525An identifier occurring as an atom is a reference to a local, global
526or built-in name binding. If a name can be assigned to anywhere in a code
527block, it refers to a local name throughout that code block.
528Otherwise, it refers to a global name if one exists, else to a
529built-in name.
530
531When the name is bound to an object, evaluation of the atom
532yields that object.
533When it is not bound, a {\tt NameError} exception
534is raised, with the identifier as string parameter.
535
536\subsection{Literals}
537
538Evaluation of a literal yields an object of the given type
539(string, integer, long integer, floating point number)
540with the given value.
541The value may be approximated in the case of floating point literals.
542
543All literals correspond to immutable data types, and hence the object's
544identity is less important than its value.
545Multiple evaluations of the same literal (either the same occurrence
546in the program text or a different occurrence) may
547obtain the same object or a different object with the same value.
548
549(In the original implementation, all literals in the same code block
550with the same type and value yield the same object.)
551
552\subsection{Enclosures}
553
554An empty enclosure yields an empty tuple object.
555
556An enclosed condition list yields whatever that condition list yields.
557
558(Note that, except for empty tuples, tuples are not formed by
559enclosure in parentheses, but rather by use of the comma operator.)
560
561\subsection{List displays}
562
563A list display yields a new list object.
564
565If it has no condition list, the list object has no items.
566Otherwise, the elements of the condition list are evaluated
567from left to right and inserted in the list object in that order.
568
569\subsection{Dictionary displays}
570
571A dictionary display yields a new dictionary object.
572
573The key/datum pairs are evaluated from left to right to
574define the entries of the dictionary:
575each key object is used as a key into the dictionary to store
576the corresponding datum pair.
577
Guido van Rossum743d1e71992-01-07 16:43:53 +0000578Keys must be strings, otherwise a {\tt TypeError} exception is raised.
579Clashes between keys are not detected; the last datum (textually
580rightmost in the display) stored for a given key value prevails.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000581
582\subsection{String conversions}
583
584A string conversion evaluates the contained condition list and converts the
585resulting object into a string according to rules specific to its type.
586
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000587If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossumf2612d11991-11-21 13:53:03 +0000588dictionary containing only objects whose type is in this list,
589the resulting
590string is a valid Python expression which can be passed to the
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000591built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +0000592same value (or an approximation, if floating point numbers are
593involved).
594
595(In particular, converting a string adds quotes around it and converts
596``funny'' characters to escape sequences that are safe to print.)
597
598It is illegal to attempt to convert recursive objects (e.g.,
599lists or dictionaries that -- directly or indirectly -- contain a reference
600to themselves.)
601
602\section{Primaries}
603
604Primaries represent the most tightly bound operations of the language.
605Their syntax is:
606
607\begin{verbatim}
608primary: atom | attributeref | call | subscription | slicing
Guido van Rossum743d1e71992-01-07 16:43:53 +0000609attributeref: primary "." identifier
610call: primary "(" [condition_list] ")"
611subscription: primary "[" condition "]"
612slicing: primary "[" [condition] ":" [condition] "]"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000613\end{verbatim}
614
615\subsection{Attribute references}
616
617\subsection{Calls}
618
619\subsection{Subscriptions}
620
621\subsection{Slicings}
622
623\section{Factors}
624
625Factors represent the unary numeric operators.
626Their syntax is:
627
628\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000629factor: primary | "-" factor | "+" factor | "~" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +0000630\end{verbatim}
631
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000632The unary \verb\-\ operator yields the negative of its numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000633
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000634The unary \verb\+\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000635
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000636The unary \verb\~\ operator yields the bit-wise negation of its
Guido van Rossumf2612d11991-11-21 13:53:03 +0000637integral numerical argument.
638
639In all three cases, if the argument does not have the proper type,
640a {\tt TypeError} exception is raised.
641
642\section{Terms}
643
644Terms represent the most tightly binding binary operators:
645
646\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000647term: factor | term "*" factor | term "/" factor | term "%" factor
Guido van Rossumf2612d11991-11-21 13:53:03 +0000648\end{verbatim}
649
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000650The \verb\*\ operator yields the product of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000651The arguments must either both be numbers, or one argument must be
652a (short) integer and the other must be a string.
653In the former case, the numbers are converted to a common type
654and then multiplied together.
655In the latter case, string repetition is performed; a negative
656repetition factor yields the empty string.
657
Guido van Rossum743d1e71992-01-07 16:43:53 +0000658The \verb|"/"| operator yields the quotient of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000659The numeric arguments are first converted to a common type.
660(Short or long) integer division yields an integer of the same type,
661truncating towards zero.
662Division by zero raises a {\tt RuntimeError} exception.
663
Guido van Rossum743d1e71992-01-07 16:43:53 +0000664The \verb|"%"| operator yields the remainder from the division
Guido van Rossumf2612d11991-11-21 13:53:03 +0000665of the first argument by the second.
666The numeric arguments are first converted to a common type.
Guido van Rossum47f23331991-12-06 17:21:05 +0000667The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000668A zero right argument raises a {\tt RuntimeError} exception.
669The arguments may be floating point numbers, e.g.,
Guido van Rossum47f23331991-12-06 17:21:05 +0000670$3.14 \% 0.7$ equals $0.34$.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000671
672\section{Arithmetic expressions}
673
674\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000675arith_expr: term | arith_expr "+" term | arith_expr "-" term
Guido van Rossumf2612d11991-11-21 13:53:03 +0000676\end{verbatim}
677
Guido van Rossum743d1e71992-01-07 16:43:53 +0000678The \verb|"+"| operator yields the sum of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000679The arguments must either both be numbers, or both strings.
680In the former case, the numbers are converted to a common type
681and then added together.
682In the latter case, the strings are concatenated directly,
683without inserting a space.
684
Guido van Rossum743d1e71992-01-07 16:43:53 +0000685The \verb|"-"| operator yields the difference of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000686The numeric arguments are first converted to a common type.
687
688\section{Shift expressions}
689
690\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000691shift_expr: arith_expr | shift_expr "<<" arith_expr | shift_expr ">>" arith_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000692\end{verbatim}
693
694These operators accept short integers as arguments only.
695They shift their left argument to the left or right by the number of bits
Guido van Rossum743d1e71992-01-07 16:43:53 +0000696given by the right argument. Shifts are ``logical"", e.g., bits shifted
Guido van Rossumf2612d11991-11-21 13:53:03 +0000697out on one end are lost, and bits shifted in are zero;
698negative numbers are shifted as if they were unsigned in C.
699Negative shift counts and shift counts greater than {\em or equal to}
700the word size yield undefined results.
701
702\section{Bitwise AND expressions}
703
704\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000705and_expr: shift_expr | and_expr "&" shift_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000706\end{verbatim}
707
708This operator yields the bitwise AND of its arguments,
709which must be short integers.
710
711\section{Bitwise XOR expressions}
712
713\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000714xor_expr: and_expr | xor_expr "^" and_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000715\end{verbatim}
716
717This operator yields the bitwise exclusive OR of its arguments,
718which must be short integers.
719
720\section{Bitwise OR expressions}
721
722\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000723or_expr: xor_expr | or_expr "|" xor_expr
Guido van Rossumf2612d11991-11-21 13:53:03 +0000724\end{verbatim}
725
726This operator yields the bitwise OR of its arguments,
727which must be short integers.
728
729\section{Expressions and expression lists}
730
731\begin{verbatim}
732expression: or_expression
Guido van Rossum743d1e71992-01-07 16:43:53 +0000733expr_list: expression ("," expression)* [","]
Guido van Rossumf2612d11991-11-21 13:53:03 +0000734\end{verbatim}
735
736An expression list containing at least one comma yields a new tuple.
737The length of the tuple is the number of expressions in the list.
738The expressions are evaluated from left to right.
739
740The trailing comma is required only to create a single tuple;
741it is optional in all other cases (a single expression without
742a trailing comma doesn't create a tuple, but rather yields the
743value of that expression).
744
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000745To create an empty tuple, use an empty pair of parentheses: \verb\()\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000746
747\section{Comparisons}
748
749\begin{verbatim}
750comparison: expression (comp_operator expression)*
Guido van Rossum743d1e71992-01-07 16:43:53 +0000751comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000752\end{verbatim}
753
754Comparisons yield integer value: 1 for true, 0 for false.
755
756Comparisons can be chained arbitrarily,
757e.g., $x < y <= z$ is equivalent to
758$x < y$ {\tt and} $y <= z$, except that $y$ is evaluated only once
759(but in both cases $z$ is not evaluated at all when $x < y$ is
760found to be false).
761
762Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to
763$e_0 op_1 e_1$ {\tt and} $e_1 op_2 e_2$ {\tt and} ... {\tt and}
764$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
765
766Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
767between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
768
Guido van Rossum743d1e71992-01-07 16:43:53 +0000769The forms \verb\<>\ and \verb\!=\ are equivalent.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000770
Guido van Rossum743d1e71992-01-07 16:43:53 +0000771The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "<>"} compare
Guido van Rossumf2612d11991-11-21 13:53:03 +0000772the values of two objects. The objects needn't have the same type.
773If both are numbers, they are compared to a common type.
774Otherwise, objects of different types {\em always} compare unequal,
775and are ordered consistently but arbitrarily, except that
776the value \verb\None\ compares smaller than the values of any other type.
777
778(This unusual
779definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000780operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000781
782Comparison of objects of the same type depends on the type:
783
784\begin{itemize}
785\item Numbers are compared arithmetically.
786\item Strings are compared lexicographically using the numeric
787 equivalents (the result of the built-in function ord())
788 of their characters.
789\item Tuples and lists are compared lexicographically
790 using comparison of corresponding items.
791\item Dictionaries compare unequal unless they are the same object;
792 the choice whether one dictionary object is considered smaller
793 or larger than another one is made arbitrarily but
794 consistently within one execution of a program.
795\item The latter rule is also used for most other built-in types.
796\end{itemize}
797
798The operators \verb\in\ and \verb\not in\ test for sequence membership:
799if $y$ is a sequence, $x {\tt in} y$ is true if and only if there exists
800an index $i$ such that $x = y_i$.
801$x {\tt not in} y$ yields the inverse truth value.
802The exception {\tt TypeError} is raised when $y$ is not a sequence,
803or when $y$ is a string and $x$ is not a string of length one.
804
805The operators \verb\is\ and \verb\is not\ compare object identity:
806$x {\tt is} y$ is true if and only if $x$ and $y$ are the same object.
807$x {\tt is not} y$ yields the inverse truth value.
808
809\section{Boolean operators}
810
811\begin{verbatim}
812condition: or_test
Guido van Rossum743d1e71992-01-07 16:43:53 +0000813or_test: and_test | or_test "or" and_test
814and_test: not_test | and_test "and" not_test
815not_test: comparison | "not" not_test
Guido van Rossumf2612d11991-11-21 13:53:03 +0000816\end{verbatim}
817
818In the context of Boolean operators, and also when conditions are
819used by control flow statements, the following values are interpreted
820as false: None, numeric zero of all types, empty sequences (strings,
821tuples and lists), and empty mappings (dictionaries).
822All other values are interpreted as true.
823
824The operator \verb\not\ yields 1 if its argument is false, 0 otherwise.
825
826The condition $x {\tt and} y$ first evaluates $x$; if $x$ is false,
827$x$ is returned; otherwise, $y$ is evaluated and returned.
828
829The condition $x {\tt or} y$ first evaluates $x$; if $x$ is true,
830$x$ is returned; otherwise, $y$ is evaluated and returned.
831
832(Note that \verb\and\ and \verb\or\ do not restrict the value and type
833they return to 0 and 1, but rather return the last evaluated argument.
834This is sometimes useful, e.g., if $s$ is a string, which should be
835replaced by a default value if it is empty, $s {\tt or} 'foo'$
836returns the desired value. Because \verb\not\ has to invent a value
837anyway, it does not bother to return a value of the same type as its
838argument, so \verb\not 'foo'\ yields $0$, not $''$.)
839
840\chapter{Simple statements}
841
842Simple statements are comprised within a single logical line.
843Several simple statements may occor on a single line separated
844by semicolons. The syntax for simple statements is:
845
846\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000847stmt_list: simple_stmt (";" simple_stmt)* [";"]
Guido van Rossumf2612d11991-11-21 13:53:03 +0000848simple_stmt: expression_stmt
849 | assignment
850 | pass_stmt
851 | del_stmt
852 | print_stmt
853 | return_stmt
854 | raise_stmt
855 | break_stmt
856 | continue_stmt
857 | import_stmt
Guido van Rossum743d1e71992-01-07 16:43:53 +0000858 | global_stmt
Guido van Rossumf2612d11991-11-21 13:53:03 +0000859\end{verbatim}
860
861\section{Expression statements}
862
863\begin{verbatim}
864expression_stmt: expression_list
865\end{verbatim}
866
867An expression statement evaluates the expression list (which may
868be a single expression).
869If the value is not \verb\None\, it is converted to a string
870using the rules for string conversions, and the resulting string
871is written to standard output on a line by itself.
872
873(The exception for \verb\None\ is made so that procedure calls,
874which are syntactically equivalent to expressions,
875do not cause any output.)
876
877\section{Assignments}
878
879\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000880assignment: target_list ("=" target_list)* "=" expression_list
881target_list: target ("," target)* [","]
882target: identifier | "(" target_list ")" | "[" target_list "]"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000883 | attributeref | subscription | slicing
884\end{verbatim}
885
886(See the section on primaries for the definition of the last
887three symbols.)
888
889An assignment evaluates the expression list (remember that this can
890be a single expression or a comma-separated list,
891the latter yielding a tuple)
892and assigns the single resulting object to each of the target lists,
893from left to right.
894
895Assignment is defined recursively depending on the type of the
896target. Where assignment is to part of a mutable object
897(through an attribute reference, subscription or slicing),
898the mutable object must ultimately perform the
899assignment and decide about its validity, raising an exception
900if the assignment is unacceptable. The rules observed by
901various types and the exceptions raised are given with the
902definition of the object types (some of which are defined
903in the library reference).
904
905Assignment of an object to a target list is recursively
906defined as follows.
907
908\begin{itemize}
909\item
910If the target list contains no commas (except in nested constructs):
911the object is assigned to the single target contained in the list.
912
913\item
914If the target list contains commas (that are not in nested constructs):
915the object must be a tuple with as many items
916as the list contains targets, and the items are assigned, from left
917to right, to the corresponding targets.
918
919\end{itemize}
920
921Assignment of an object to a (non-list)
922target is recursively defined as follows.
923
924\begin{itemize}
925
926\item
927If the target is an identifier (name):
928the object is bound to that name
929in the current local scope. Any previous binding of the same name
930is undone.
931
932\item
933If the target is a target list enclosed in parentheses:
934the object is assigned to that target list.
935
936\item
937If the target is a target list enclosed in square brackets:
938the object must be a list with as many items
939as the target list contains targets,
940and the list's items are assigned, from left to right,
941to the corresponding targets.
942
943\item
944If the target is an attribute reference:
945The primary expression in the reference is evaluated.
946It should yield an object with assignable attributes;
947if this is not the case, a {\tt TypeError} exception is raised.
948That object is then asked to assign the assigned object
949to the given attribute; if it cannot perform the assignment,
950it raises an exception.
951
952\item
953If the target is a subscription:
954The primary expression in the reference is evaluated.
955It should yield either a mutable sequence object or a mapping
956(dictionary) object.
957Next, the subscript expression is evaluated.
958
959If the primary is a sequence object, the subscript must yield a
960nonnegative integer smaller than the sequence's length,
961and the sequence is asked to assign the assigned object
962to its item with that index.
963
964If the primary is a mapping object, the subscript must have a
965type compatible with the mapping's key type,
966and the mapping is then asked to to create a key/datum pair
967which maps the subscript to the assigned object.
968
969Various exceptions can be raised.
970
971\item
972If the target is a slicing:
973The primary expression in the reference is evaluated.
974It should yield a mutable sequence object (currently only lists).
975The assigned object should be a sequence object of the same type.
976Next, the lower and upper bound expressions are evaluated,
977insofar they are present; defaults are zero and the sequence's length.
978The bounds should evaluate to (small) integers.
979If either bound is negative, the sequence's length is added to it (once).
980The resulting bounds are clipped to lie between zero
981and the sequence's length, inclusive.
982(XXX Shouldn't this description be with expressions?)
983Finally, the sequence object is asked to replace the items
984indicated by the slice with the items of the assigned sequence.
985This may change the sequence's length, if it allows it.
986
987\end{itemize}
988
989(In the original implementation, the syntax for targets is taken
990to be the same as for expressions, and invalid syntax is rejected
991during the code generation phase, causing less detailed error
992messages.)
993
994\section{The {\tt pass} statement}
995
996\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +0000997pass_stmt: "pass"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000998\end{verbatim}
999
1000{\tt pass} is a null operation -- when it is executed,
1001nothing happens.
1002
1003\section{The {\tt del} statement}
1004
1005\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001006del_stmt: "del" target_list
Guido van Rossumf2612d11991-11-21 13:53:03 +00001007\end{verbatim}
1008
1009Deletion is recursively defined similar to assignment.
1010
1011(XXX Rather that spelling it out in full details,
1012here are some hints.)
1013
1014Deletion of a target list recursively deletes each target,
1015from left to right.
1016
1017Deletion of a name removes the binding of that name (which must exist)
1018from the local scope.
1019
1020Deletion of attribute references, subscriptions and slicings
1021is passed to the primary object involved; deletion of a slicing
1022is in general equivalent to assignment of an empty slice of the
1023right type (but even this is determined by the sliced object).
1024
1025\section{The {\tt print} statement}
1026
1027\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001028print_stmt: "print" [ condition ("," condition)* [","] ]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001029\end{verbatim}
1030
1031{\tt print} evaluates each condition in turn and writes the resulting
1032object to standard output (see below).
1033If an object is not a string, it is first converted to
1034a string using the rules for string conversions.
1035The (resulting or original) string is then written.
1036A space is written before each object is (converted and) written,
1037unless the output system believes it is positioned at the beginning
1038of a line. This is the case: (1) when no characters have been written
1039to standard output; or (2) when the last character written to
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001040standard output is \verb/\n/;
Guido van Rossumf2612d11991-11-21 13:53:03 +00001041or (3) when the last I/O operation
1042on standard output was not a \verb\print\ statement.
1043
1044Finally,
Guido van Rossum4fc43bc1991-11-25 17:26:57 +00001045a \verb/\n/ character is written at the end,
Guido van Rossumf2612d11991-11-21 13:53:03 +00001046unless the \verb\print\ statement ends with a comma.
1047This is the only action if the statement contains just the keyword
1048\verb\print\.
1049
1050Standard output is defined as the file object named \verb\stdout\
1051in the built-in module \verb\sys\. If no such object exists,
1052or if it is not a writable file, a {\tt RuntimeError} exception is raised.
1053(The original implementation attempts to write to the system's original
1054standard output instead, but this is not safe, and should be fixed.)
1055
1056\section{The {\tt return} statement}
1057
1058\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001059return_stmt: "return" [condition_list]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001060\end{verbatim}
1061
1062\verb\return\ may only occur syntactically nested in a function
1063definition, not within a nested class definition.
1064
1065If a condition list is present, it is evaluated, else \verb\None\
1066is substituted.
1067
1068\verb\return\ leaves the current function call with the condition
1069list (or \verb\None\) as return value.
1070
1071When \verb\return\ passes control out of a \verb\try\ statement
1072with a \verb\finally\ clause, that finally clause is executed
1073before really leaving the function.
1074(XXX This should be made more exact, a la Modula-3.)
1075
1076\section{The {\tt raise} statement}
1077
1078\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001079raise_stmt: "raise" condition ["," condition]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001080\end{verbatim}
1081
1082\verb\raise\ evaluates its first condition, which must yield
1083a string object. If there is a second condition, this is evaluated,
1084else \verb\None\ is substituted.
1085
1086It then raises the exception identified by the first object,
1087with the second one (or \verb\None\) as its parameter.
1088
1089\section{The {\tt break} statement}
1090
1091\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001092break_stmt: "break"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001093\end{verbatim}
1094
1095\verb\break\ may only occur syntactically nested in a \verb\for\
1096or \verb\while\ loop, not nested in a function or class definition.
1097
1098It terminates the neares enclosing loop, skipping the optional
1099\verb\else\ clause if the loop has one.
1100
1101If a \verb\for\ loop is terminated by \verb\break\, the loop control
1102target (list) keeps its current value.
1103
1104When \verb\break\ passes control out of a \verb\try\ statement
1105with a \verb\finally\ clause, that finally clause is executed
1106before really leaving the loop.
1107
1108\section{The {\tt continue} statement}
1109
1110\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001111continue_stmt: "continue"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001112\end{verbatim}
1113
1114\verb\continue\ may only occur syntactically nested in a \verb\for\
1115or \verb\while\ loop, not nested in a function or class definition,
1116and {\em not nested in a \verb\try\ statement with a \verb\finally\
1117clause}.
1118
1119It continues with the next cycle of the nearest enclosing loop.
1120
1121\section{The {\tt import} statement}
1122
1123\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001124import_stmt: "import" identifier ("," identifier)*
1125 | "from" identifier "import" identifier ("," identifier)*
1126 | "from" identifier "import" "*"
1127\end{verbatim}
1128
1129(XXX To be done.)
1130
1131\section{The {\tt global} statement}
1132
1133\begin{verbatim}
1134global_stmt: "global" identifier ("," identifier)*
Guido van Rossumf2612d11991-11-21 13:53:03 +00001135\end{verbatim}
1136
1137(XXX To be done.)
1138
1139\chapter{Compound statements}
1140
1141(XXX The semantic definitions of this chapter are still to be done.)
1142
1143\begin{verbatim}
1144statement: stmt_list NEWLINE | compound_stmt
1145compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1146suite: statement | NEWLINE INDENT statement+ DEDENT
1147\end{verbatim}
1148
1149\section{The {\tt if} statement}
1150
1151\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001152if_stmt: "if" condition ":" suite
1153 ("elif" condition ":" suite)*
1154 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001155\end{verbatim}
1156
1157\section{The {\tt while} statement}
1158
1159\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001160while_stmt: "while" condition ":" suite ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001161\end{verbatim}
1162
1163\section{The {\tt for} statement}
1164
1165\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001166for_stmt: "for" target_list "in" condition_list ":" suite
1167 ["else" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001168\end{verbatim}
1169
1170\section{The {\tt try} statement}
1171
1172\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001173try_stmt: "try" ":" suite
1174 ("except" condition ["," condition] ":" suite)*
1175 ["finally" ":" suite]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001176\end{verbatim}
1177
1178\section{Function definitions}
1179
1180\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001181funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
1182parameter_list: parameter ("," parameter)*
1183parameter: identifier | "(" parameter_list ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001184\end{verbatim}
1185
1186\section{Class definitions}
1187
1188\begin{verbatim}
Guido van Rossum743d1e71992-01-07 16:43:53 +00001189classdef: "class" identifier [inheritance] ":" suite
1190inheritance: "(" expression ("," expression)* ")"
Guido van Rossumf2612d11991-11-21 13:53:03 +00001191\end{verbatim}
1192
1193XXX Syntax for scripts, modules
1194XXX Syntax for interactive input, eval, exec, input
Guido van Rossum743d1e71992-01-07 16:43:53 +00001195XXX New definition of expressions (as conditions)
Guido van Rossumf2612d11991-11-21 13:53:03 +00001196
1197\end{document}