blob: c47b08475fbe8e5f180b5928b80cf935a75bfc0e [file] [log] [blame]
Guido van Rossumf2612d11991-11-21 13:53:03 +00001% Format this file with latex.
2
3\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
63\chapter{Lexical analysis}
64
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000065A Python program is read by a {\em parser}. Input to the parser is a
66stream of {\em tokens}, generated by the {\em lexical analyzer}. This
67chapter describes how the lexical analyzer breaks a file into tokens.
Guido van Rossumf2612d11991-11-21 13:53:03 +000068
69\section{Line structure}
70
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000071A Python program is divided in a number of logical lines. Statements
72do not straddle logical line boundaries except where explicitly
73indicated by the syntax (i.e., for compound statements). To this
74purpose, the end of a logical line is represented by the token
75NEWLINE.
Guido van Rossumf2612d11991-11-21 13:53:03 +000076
77\subsection{Comments}
78
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000079A comment starts with a hash character (\verb\#\) that is not part of
80a string literal, and ends at the end of the physical line. Comments
81are ignored by the syntax.
Guido van Rossumf2612d11991-11-21 13:53:03 +000082
83\subsection{Line joining}
84
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000085Two or more physical lines may be joined into logical lines using
86backslash characters (\verb/\/), as follows: When physical line ends
87in a backslash that is not part of a string literal or comment, it is
88joined with the following forming a single logical line, deleting the
89backslash and the following end-of-line character.
Guido van Rossumf2612d11991-11-21 13:53:03 +000090
91\subsection{Blank lines}
92
Guido van Rossum4fc43bc1991-11-25 17:26:57 +000093A logical line that contains only spaces, tabs, and possibly a
94comment, is ignored (i.e., no NEWLINE token is generated), except that
95during interactive input of statements, an entirely blank logical line
96terminates a multi-line statement.
Guido van Rossumf2612d11991-11-21 13:53:03 +000097
98\subsection{Indentation}
99
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000100Spaces and tabs at the beginning of a logical line are used to compute
Guido van Rossumf2612d11991-11-21 13:53:03 +0000101the indentation level of the line, which in turn is used to determine
102the grouping of statements.
103
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000104First, each tab is replaced by one to eight spaces such that the total
105number of spaces up to that point is a multiple of eight. The total
106number of spaces preceding the first non-blank character then
107determines the line's indentation. Indentation cannot be split over
108multiple physical lines using backslashes.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000109
110The indentation levels of consecutive lines are used to generate
111INDENT and DEDENT tokens, using a stack, as follows.
112
113Before the first line of the file is read, a single zero is pushed on
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000114the stack; this will never be popped off again. The numbers pushed on
115the stack will always be strictly increasing from bottom to top. At
116the beginning of each logical line, the line's indentation level is
117compared to the top of the stack. If it is equal, nothing happens.
118If it larger, it is pushed on the stack, and one INDENT token is
119generated. If it is smaller, it {\em must} be one of the numbers
120occurring on the stack; all numbers on the stack that are larger are
121popped off, and for each number popped off a DEDENT token is
122generated. At the end of the file, a DEDENT token is generated for
123each number remaining on the stack that is larger than zero.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000124
125\section{Other tokens}
126
127Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
128exist: identifiers, keywords, literals, operators, and delimiters.
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000129Spaces and tabs are not tokens, but serve to delimit tokens. Where
130ambiguity exists, a token comprises the longest possible string that
131forms a legal token, when read from left to right.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000132
133Tokens are described using an extended regular expression notation.
134This is similar to the extended BNF notation used later, except that
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000135the notation \verb\<...>\ is used to give an informal description of a
136character, and that spaces and tabs are not to be ignored.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000137
138\section{Identifiers}
139
140Identifiers are described by the following regular expressions:
141
142\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000143identifier: (letter|"_") (letter|digit|"_")*
Guido van Rossumf2612d11991-11-21 13:53:03 +0000144letter: lowercase | uppercase
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000145lowercase: "a"|"b"|...|"z"
146uppercase: "A"|"B"|...|"Z"
147digit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000148\end{verbatim}
149
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000150Identifiers are unlimited in length. Case is significant.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000151
152\section{Keywords}
153
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000154The following identifiers are used as reserved words, or {\em
155keywords} of the language, and may not be used as ordinary
156identifiers. They must be spelled exactly as written here:
Guido van Rossumf2612d11991-11-21 13:53:03 +0000157
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000158\begin{verbatim}
159and del for is raise
160break elif from not return
161class else if or try
162continue except import pass while
163def finally in print
164\end{verbatim}
165
166% import string
167% l = []
168% try:
169% while 1:
170% l = l + string.split(raw_input())
171% except EOFError:
172% pass
173% l.sort()
174% for i in range((len(l)+4)/5):
175% for j in range(i, len(l), 5):
176% print string.ljust(l[j], 10),
177% print
Guido van Rossumf2612d11991-11-21 13:53:03 +0000178
179\section{Literals}
180
181\subsection{String literals}
182
183String literals are described by the following regular expressions:
184
185\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000186stringliteral: "'" stringitem* "'"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000187stringitem: stringchar | escapeseq
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000188stringchar: <any character except newline or "\" or "'">
189escapeseq: "'" <any character except newline>
Guido van Rossumf2612d11991-11-21 13:53:03 +0000190\end{verbatim}
191
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000192String literals cannot span physical line boundaries. Escape
193sequences in strings are actually interpreted according to rules
194simular to those used by Standard C. The recognized escape sequences
195are:
196
197\begin{center}
198\begin{tabular}{|l|l|}
199\hline
200\verb/\\/ & Backslash (\verb/\/) \\
201\verb/\'/ & Single quote (\verb/'/) \\
202\verb/\a/ & ASCII Bell (BEL) \\
203\verb/\b/ & ASCII Backspace (BS) \\
204\verb/\E/ & ASCII Escape (ESC) \\
205\verb/\f/ & ASCII Formfeed (FF) \\
206\verb/\n/ & ASCII Linefeed (LF) \\
207\verb/\r/ & ASCII Carriage Return (CR) \\
208\verb/\t/ & ASCII Horizontal Tab (TAB) \\
209\verb/\v/ & ASCII Vertical Tab (VT) \\
210\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
211\verb/\x/{em xx...} & ASCII character with hex value {\em xx} \\
212\hline
213\end{tabular}
214\end{center}
215
216For compatibility with in Standard C, up to three octal digits are
217accepted, but an unlimited number of hex digits is taken to be part of
218the hex escape (and then the lower 8 bits of the resulting hex number
219are used...).
220
221All unrecognized escape sequences are left in the string {\em
222unchanged}, i.e., the backslash is left in the string. (This rule is
223useful when debugging: if an escape sequence is mistyped, the
224resulting output is more easily recognized as broken. It also helps
225somewhat for string literals used as regular expressions or otherwise
226passed to other modules that do their own escape handling.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000227
228\subsection{Numeric literals}
229
230There are three types of numeric literals: integers, long integers,
231and floating point numbers.
232
233Integers and long integers are described by the following regular expressions:
234
235\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000236longinteger: integer ("l"|"L")
Guido van Rossumf2612d11991-11-21 13:53:03 +0000237integer: decimalinteger | octinteger | hexinteger
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000238decimalinteger: nonzerodigit digit* | "0"
239octinteger: "0" octdigit+
240hexinteger: "0" ("x"|"X") hexdigit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000241
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000242nonzerodigit: "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
243octdigit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"
244hexdigit: digit|"a"|"b"|"c"|"d"|"e"|"f"|"A"|"B"|"C"|"D"|"E"|"F"
Guido van Rossumf2612d11991-11-21 13:53:03 +0000245\end{verbatim}
246
247Floating point numbers are described by the following regular expressions:
248
249\begin{verbatim}
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000250floatnumber: [intpart] fraction [exponent] | intpart ["."] exponent
Guido van Rossumf2612d11991-11-21 13:53:03 +0000251intpart: digit+
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000252fraction: "." digit+
253exponent: ("e"|"E") ["+"|"-"] digit+
Guido van Rossumf2612d11991-11-21 13:53:03 +0000254\end{verbatim}
255
256\section{Operators}
257
258The following tokens are operators:
259
260\begin{verbatim}
261+ - * / %
262<< >> & | ^ ~
263< = == > <= <> != >=
264\end{verbatim}
265
266\section{Delimiters}
267
268The following tokens are delimiters:
269
270\begin{verbatim}
271( ) [ ] { }
272; , : . `
273\end{verbatim}
274
275The following printing ASCII characters are currently not used;
276their occurrence is an unconditional error:
277
278\begin{verbatim}
279! @ $ " ?
280\end{verbatim}
281
282\chapter{Execution model}
283
284(XXX This chapter should explain the general model
285of the execution of Python code and
286the evaluation of expressions.
287It should introduce objects, values, code blocks, scopes, name spaces,
288name binding,
289types, sequences, numbers, mappings,
290exceptions, and other technical terms needed to make the following
291chapters concise and exact.)
292
293\chapter{Expressions and conditions}
294
295(From now on, extended BNF notation will be used to describe
296syntax, not lexical analysis.)
297(XXX Explain the notation.)
298
299This chapter explains the meaning of the elements of expressions and
300conditions. Conditions are a superset of expressions, and a condition
301may be used where an expression is required by enclosing it in
302parentheses. The only place where an unparenthesized condition
303is not allowed is on the right-hand side of the assignment operator,
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000304because this operator is the same token (\verb\=\) as used for
Guido van Rossumf2612d11991-11-21 13:53:03 +0000305compasisons.
306
307The comma plays a somewhat special role in Python's syntax.
308It is an operator with a lower precedence than all others, but
309occasionally serves other purposes as well (e.g., it has special
310semantics in print statements). When a comma is accepted by the
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000311syntax, one of the syntactic categories \verb\expression_list\
312or \verb\condition_list\ is always used.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000313
314When (one alternative of) a syntax rule has the form
315
316\begin{verbatim}
317name: othername
318\end{verbatim}
319
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000320and no semantics are given, the semantics of this form of \verb\name\
321are the same as for \verb\othername\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000322
323\section{Arithmetic conversions}
324
325When a description of an arithmetic operator below uses the phrase
326``the numeric arguments are converted to a common type'',
327this both means that if either argument is not a number, a
328{\tt TypeError} exception is raised, and that otherwise
329the following conversions are applied:
330
331\begin{itemize}
332\item First, if either argument is a floating point number,
333 the other is converted to floating point;
334\item else, if either argument is a long integer,
335 the other is converted to long integer;
336\item otherwise, both must be short integers and no conversion
337 is necessary.
338\end{itemize}
339
340(Note: ``short integers'' in Python are at least 32 bits in size;
341``long integers'' are arbitrary precision integers.)
342
343\section{Atoms}
344
345Atoms are the most basic elements of expressions.
346Forms enclosed in reverse quotes or various types of parentheses
347or braces are also categorized syntactically as atoms.
348Syntax rules for atoms:
349
350\begin{verbatim}
351atom: identifier | literal | parenth_form | string_conversion
352literal: stringliteral | integer | longinteger | floatnumber
353parenth_form: enclosure | list_display | dict_display
354enclosure: '(' [condition_list] ')'
355list_display: '[' [condition_list] ']'
356dict_display: '{' [key_datum (',' key_datum)* [','] '}'
357key_datum: condition ':' condition
358string_conversion:'`' condition_list '`'
359\end{verbatim}
360
361\subsection{Identifiers (Names)}
362
363An identifier occurring as an atom is a reference to a local, global
364or built-in name binding. If a name can be assigned to anywhere in a code
365block, it refers to a local name throughout that code block.
366Otherwise, it refers to a global name if one exists, else to a
367built-in name.
368
369When the name is bound to an object, evaluation of the atom
370yields that object.
371When it is not bound, a {\tt NameError} exception
372is raised, with the identifier as string parameter.
373
374\subsection{Literals}
375
376Evaluation of a literal yields an object of the given type
377(string, integer, long integer, floating point number)
378with the given value.
379The value may be approximated in the case of floating point literals.
380
381All literals correspond to immutable data types, and hence the object's
382identity is less important than its value.
383Multiple evaluations of the same literal (either the same occurrence
384in the program text or a different occurrence) may
385obtain the same object or a different object with the same value.
386
387(In the original implementation, all literals in the same code block
388with the same type and value yield the same object.)
389
390\subsection{Enclosures}
391
392An empty enclosure yields an empty tuple object.
393
394An enclosed condition list yields whatever that condition list yields.
395
396(Note that, except for empty tuples, tuples are not formed by
397enclosure in parentheses, but rather by use of the comma operator.)
398
399\subsection{List displays}
400
401A list display yields a new list object.
402
403If it has no condition list, the list object has no items.
404Otherwise, the elements of the condition list are evaluated
405from left to right and inserted in the list object in that order.
406
407\subsection{Dictionary displays}
408
409A dictionary display yields a new dictionary object.
410
411The key/datum pairs are evaluated from left to right to
412define the entries of the dictionary:
413each key object is used as a key into the dictionary to store
414the corresponding datum pair.
415
416Key objects must be strings, otherwise a {\tt TypeError}
417exception is raised.
418Clashes between keys are not detected; the last datum stored for a given
419key value prevails.
420
421\subsection{String conversions}
422
423A string conversion evaluates the contained condition list and converts the
424resulting object into a string according to rules specific to its type.
425
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000426If the object is a string, a number, \verb\None\, or a tuple, list or
Guido van Rossumf2612d11991-11-21 13:53:03 +0000427dictionary containing only objects whose type is in this list,
428the resulting
429string is a valid Python expression which can be passed to the
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000430built-in function \verb\eval()\ to yield an expression with the
Guido van Rossumf2612d11991-11-21 13:53:03 +0000431same value (or an approximation, if floating point numbers are
432involved).
433
434(In particular, converting a string adds quotes around it and converts
435``funny'' characters to escape sequences that are safe to print.)
436
437It is illegal to attempt to convert recursive objects (e.g.,
438lists or dictionaries that -- directly or indirectly -- contain a reference
439to themselves.)
440
441\section{Primaries}
442
443Primaries represent the most tightly bound operations of the language.
444Their syntax is:
445
446\begin{verbatim}
447primary: atom | attributeref | call | subscription | slicing
448attributeref: primary '.' identifier
449call: primary '(' [condition_list] ')'
450subscription: primary '[' condition ']'
451slicing: primary '[' [condition] ':' [condition] ']'
452\end{verbatim}
453
454\subsection{Attribute references}
455
456\subsection{Calls}
457
458\subsection{Subscriptions}
459
460\subsection{Slicings}
461
462\section{Factors}
463
464Factors represent the unary numeric operators.
465Their syntax is:
466
467\begin{verbatim}
468factor: primary | '-' factor | '+' factor | '~' factor
469\end{verbatim}
470
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000471The unary \verb\-\ operator yields the negative of its numeric argument.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000472
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000473The unary \verb\+\ operator yields its numeric argument unchanged.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000474
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000475The unary \verb\~\ operator yields the bit-wise negation of its
Guido van Rossumf2612d11991-11-21 13:53:03 +0000476integral numerical argument.
477
478In all three cases, if the argument does not have the proper type,
479a {\tt TypeError} exception is raised.
480
481\section{Terms}
482
483Terms represent the most tightly binding binary operators:
484
485\begin{verbatim}
486term: factor | term '*' factor | term '/' factor | term '%' factor
487\end{verbatim}
488
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000489The \verb\*\ operator yields the product of its arguments.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000490The arguments must either both be numbers, or one argument must be
491a (short) integer and the other must be a string.
492In the former case, the numbers are converted to a common type
493and then multiplied together.
494In the latter case, string repetition is performed; a negative
495repetition factor yields the empty string.
496
497The \verb|'/'| operator yields the quotient of its arguments.
498The numeric arguments are first converted to a common type.
499(Short or long) integer division yields an integer of the same type,
500truncating towards zero.
501Division by zero raises a {\tt RuntimeError} exception.
502
503The \verb|'%'| operator yields the remainder from the division
504of the first argument by the second.
505The numeric arguments are first converted to a common type.
Guido van Rossum47f23331991-12-06 17:21:05 +0000506The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000507A zero right argument raises a {\tt RuntimeError} exception.
508The arguments may be floating point numbers, e.g.,
Guido van Rossum47f23331991-12-06 17:21:05 +0000509$3.14 \% 0.7$ equals $0.34$.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000510
511\section{Arithmetic expressions}
512
513\begin{verbatim}
514arith_expr: term | arith_expr '+' term | arith_expr '-' term
515\end{verbatim}
516
517The \verb|'+'| operator yields the sum of its arguments.
518The arguments must either both be numbers, or both strings.
519In the former case, the numbers are converted to a common type
520and then added together.
521In the latter case, the strings are concatenated directly,
522without inserting a space.
523
524The \verb|'-'| operator yields the difference of its arguments.
525The numeric arguments are first converted to a common type.
526
527\section{Shift expressions}
528
529\begin{verbatim}
530shift_expr: arith_expr | shift_expr '<<' arith_expr | shift_expr '>>' arith_expr
531\end{verbatim}
532
533These operators accept short integers as arguments only.
534They shift their left argument to the left or right by the number of bits
535given by the right argument. Shifts are ``logical'', e.g., bits shifted
536out on one end are lost, and bits shifted in are zero;
537negative numbers are shifted as if they were unsigned in C.
538Negative shift counts and shift counts greater than {\em or equal to}
539the word size yield undefined results.
540
541\section{Bitwise AND expressions}
542
543\begin{verbatim}
544and_expr: shift_expr | and_expr '&' shift_expr
545\end{verbatim}
546
547This operator yields the bitwise AND of its arguments,
548which must be short integers.
549
550\section{Bitwise XOR expressions}
551
552\begin{verbatim}
553xor_expr: and_expr | xor_expr '^' and_expr
554\end{verbatim}
555
556This operator yields the bitwise exclusive OR of its arguments,
557which must be short integers.
558
559\section{Bitwise OR expressions}
560
561\begin{verbatim}
562or_expr: xor_expr | or_expr '|' xor_expr
563\end{verbatim}
564
565This operator yields the bitwise OR of its arguments,
566which must be short integers.
567
568\section{Expressions and expression lists}
569
570\begin{verbatim}
571expression: or_expression
572expr_list: expression (',' expression)* [',']
573\end{verbatim}
574
575An expression list containing at least one comma yields a new tuple.
576The length of the tuple is the number of expressions in the list.
577The expressions are evaluated from left to right.
578
579The trailing comma is required only to create a single tuple;
580it is optional in all other cases (a single expression without
581a trailing comma doesn't create a tuple, but rather yields the
582value of that expression).
583
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000584To create an empty tuple, use an empty pair of parentheses: \verb\()\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000585
586\section{Comparisons}
587
588\begin{verbatim}
589comparison: expression (comp_operator expression)*
590comp_operator: '<'|'>'|'='|'=='|'>='|'<='|'<>'|'!='|['not'] 'in'|is' ['not']
591\end{verbatim}
592
593Comparisons yield integer value: 1 for true, 0 for false.
594
595Comparisons can be chained arbitrarily,
596e.g., $x < y <= z$ is equivalent to
597$x < y$ {\tt and} $y <= z$, except that $y$ is evaluated only once
598(but in both cases $z$ is not evaluated at all when $x < y$ is
599found to be false).
600
601Formally, $e_0 op_1 e_1 op_2 e_2 ...e_{n-1} op_n e_n$ is equivalent to
602$e_0 op_1 e_1$ {\tt and} $e_1 op_2 e_2$ {\tt and} ... {\tt and}
603$e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
604
605Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
606between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
607
608For the benefit of C programmers,
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000609the comparison operators \verb\=\ and \verb\==\ are equivalent,
610and so are \verb\<>\ and \verb\!=\.
Guido van Rossumf2612d11991-11-21 13:53:03 +0000611Use of the C variants is discouraged.
612
613The operators {\tt '<', '>', '=', '>=', '<='}, and {\tt '<>'} compare
614the values of two objects. The objects needn't have the same type.
615If both are numbers, they are compared to a common type.
616Otherwise, objects of different types {\em always} compare unequal,
617and are ordered consistently but arbitrarily, except that
618the value \verb\None\ compares smaller than the values of any other type.
619
620(This unusual
621definition of comparison is done to simplify the definition of
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000622operations like sorting and the \verb\in\ and \verb\not in\ operators.)
Guido van Rossumf2612d11991-11-21 13:53:03 +0000623
624Comparison of objects of the same type depends on the type:
625
626\begin{itemize}
627\item Numbers are compared arithmetically.
628\item Strings are compared lexicographically using the numeric
629 equivalents (the result of the built-in function ord())
630 of their characters.
631\item Tuples and lists are compared lexicographically
632 using comparison of corresponding items.
633\item Dictionaries compare unequal unless they are the same object;
634 the choice whether one dictionary object is considered smaller
635 or larger than another one is made arbitrarily but
636 consistently within one execution of a program.
637\item The latter rule is also used for most other built-in types.
638\end{itemize}
639
640The operators \verb\in\ and \verb\not in\ test for sequence membership:
641if $y$ is a sequence, $x {\tt in} y$ is true if and only if there exists
642an index $i$ such that $x = y_i$.
643$x {\tt not in} y$ yields the inverse truth value.
644The exception {\tt TypeError} is raised when $y$ is not a sequence,
645or when $y$ is a string and $x$ is not a string of length one.
646
647The operators \verb\is\ and \verb\is not\ compare object identity:
648$x {\tt is} y$ is true if and only if $x$ and $y$ are the same object.
649$x {\tt is not} y$ yields the inverse truth value.
650
651\section{Boolean operators}
652
653\begin{verbatim}
654condition: or_test
655or_test: and_test | or_test 'or' and_test
656and_test: not_test | and_test 'and' not_test
657not_test: comparison | 'not' not_test
658\end{verbatim}
659
660In the context of Boolean operators, and also when conditions are
661used by control flow statements, the following values are interpreted
662as false: None, numeric zero of all types, empty sequences (strings,
663tuples and lists), and empty mappings (dictionaries).
664All other values are interpreted as true.
665
666The operator \verb\not\ yields 1 if its argument is false, 0 otherwise.
667
668The condition $x {\tt and} y$ first evaluates $x$; if $x$ is false,
669$x$ is returned; otherwise, $y$ is evaluated and returned.
670
671The condition $x {\tt or} y$ first evaluates $x$; if $x$ is true,
672$x$ is returned; otherwise, $y$ is evaluated and returned.
673
674(Note that \verb\and\ and \verb\or\ do not restrict the value and type
675they return to 0 and 1, but rather return the last evaluated argument.
676This is sometimes useful, e.g., if $s$ is a string, which should be
677replaced by a default value if it is empty, $s {\tt or} 'foo'$
678returns the desired value. Because \verb\not\ has to invent a value
679anyway, it does not bother to return a value of the same type as its
680argument, so \verb\not 'foo'\ yields $0$, not $''$.)
681
682\chapter{Simple statements}
683
684Simple statements are comprised within a single logical line.
685Several simple statements may occor on a single line separated
686by semicolons. The syntax for simple statements is:
687
688\begin{verbatim}
689stmt_list: simple_stmt (';' simple_stmt)* [';']
690simple_stmt: expression_stmt
691 | assignment
692 | pass_stmt
693 | del_stmt
694 | print_stmt
695 | return_stmt
696 | raise_stmt
697 | break_stmt
698 | continue_stmt
699 | import_stmt
700\end{verbatim}
701
702\section{Expression statements}
703
704\begin{verbatim}
705expression_stmt: expression_list
706\end{verbatim}
707
708An expression statement evaluates the expression list (which may
709be a single expression).
710If the value is not \verb\None\, it is converted to a string
711using the rules for string conversions, and the resulting string
712is written to standard output on a line by itself.
713
714(The exception for \verb\None\ is made so that procedure calls,
715which are syntactically equivalent to expressions,
716do not cause any output.)
717
718\section{Assignments}
719
720\begin{verbatim}
721assignment: target_list ('=' target_list)* '=' expression_list
722target_list: target (',' target)* [',']
723target: identifier | '(' target_list ')' | '[' target_list ']'
724 | attributeref | subscription | slicing
725\end{verbatim}
726
727(See the section on primaries for the definition of the last
728three symbols.)
729
730An assignment evaluates the expression list (remember that this can
731be a single expression or a comma-separated list,
732the latter yielding a tuple)
733and assigns the single resulting object to each of the target lists,
734from left to right.
735
736Assignment is defined recursively depending on the type of the
737target. Where assignment is to part of a mutable object
738(through an attribute reference, subscription or slicing),
739the mutable object must ultimately perform the
740assignment and decide about its validity, raising an exception
741if the assignment is unacceptable. The rules observed by
742various types and the exceptions raised are given with the
743definition of the object types (some of which are defined
744in the library reference).
745
746Assignment of an object to a target list is recursively
747defined as follows.
748
749\begin{itemize}
750\item
751If the target list contains no commas (except in nested constructs):
752the object is assigned to the single target contained in the list.
753
754\item
755If the target list contains commas (that are not in nested constructs):
756the object must be a tuple with as many items
757as the list contains targets, and the items are assigned, from left
758to right, to the corresponding targets.
759
760\end{itemize}
761
762Assignment of an object to a (non-list)
763target is recursively defined as follows.
764
765\begin{itemize}
766
767\item
768If the target is an identifier (name):
769the object is bound to that name
770in the current local scope. Any previous binding of the same name
771is undone.
772
773\item
774If the target is a target list enclosed in parentheses:
775the object is assigned to that target list.
776
777\item
778If the target is a target list enclosed in square brackets:
779the object must be a list with as many items
780as the target list contains targets,
781and the list's items are assigned, from left to right,
782to the corresponding targets.
783
784\item
785If the target is an attribute reference:
786The primary expression in the reference is evaluated.
787It should yield an object with assignable attributes;
788if this is not the case, a {\tt TypeError} exception is raised.
789That object is then asked to assign the assigned object
790to the given attribute; if it cannot perform the assignment,
791it raises an exception.
792
793\item
794If the target is a subscription:
795The primary expression in the reference is evaluated.
796It should yield either a mutable sequence object or a mapping
797(dictionary) object.
798Next, the subscript expression is evaluated.
799
800If the primary is a sequence object, the subscript must yield a
801nonnegative integer smaller than the sequence's length,
802and the sequence is asked to assign the assigned object
803to its item with that index.
804
805If the primary is a mapping object, the subscript must have a
806type compatible with the mapping's key type,
807and the mapping is then asked to to create a key/datum pair
808which maps the subscript to the assigned object.
809
810Various exceptions can be raised.
811
812\item
813If the target is a slicing:
814The primary expression in the reference is evaluated.
815It should yield a mutable sequence object (currently only lists).
816The assigned object should be a sequence object of the same type.
817Next, the lower and upper bound expressions are evaluated,
818insofar they are present; defaults are zero and the sequence's length.
819The bounds should evaluate to (small) integers.
820If either bound is negative, the sequence's length is added to it (once).
821The resulting bounds are clipped to lie between zero
822and the sequence's length, inclusive.
823(XXX Shouldn't this description be with expressions?)
824Finally, the sequence object is asked to replace the items
825indicated by the slice with the items of the assigned sequence.
826This may change the sequence's length, if it allows it.
827
828\end{itemize}
829
830(In the original implementation, the syntax for targets is taken
831to be the same as for expressions, and invalid syntax is rejected
832during the code generation phase, causing less detailed error
833messages.)
834
835\section{The {\tt pass} statement}
836
837\begin{verbatim}
838pass_stmt: 'pass'
839\end{verbatim}
840
841{\tt pass} is a null operation -- when it is executed,
842nothing happens.
843
844\section{The {\tt del} statement}
845
846\begin{verbatim}
847del_stmt: 'del' target_list
848\end{verbatim}
849
850Deletion is recursively defined similar to assignment.
851
852(XXX Rather that spelling it out in full details,
853here are some hints.)
854
855Deletion of a target list recursively deletes each target,
856from left to right.
857
858Deletion of a name removes the binding of that name (which must exist)
859from the local scope.
860
861Deletion of attribute references, subscriptions and slicings
862is passed to the primary object involved; deletion of a slicing
863is in general equivalent to assignment of an empty slice of the
864right type (but even this is determined by the sliced object).
865
866\section{The {\tt print} statement}
867
868\begin{verbatim}
869print_stmt: 'print' [ condition (',' condition)* [','] ]
870\end{verbatim}
871
872{\tt print} evaluates each condition in turn and writes the resulting
873object to standard output (see below).
874If an object is not a string, it is first converted to
875a string using the rules for string conversions.
876The (resulting or original) string is then written.
877A space is written before each object is (converted and) written,
878unless the output system believes it is positioned at the beginning
879of a line. This is the case: (1) when no characters have been written
880to standard output; or (2) when the last character written to
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000881standard output is \verb/\n/;
Guido van Rossumf2612d11991-11-21 13:53:03 +0000882or (3) when the last I/O operation
883on standard output was not a \verb\print\ statement.
884
885Finally,
Guido van Rossum4fc43bc1991-11-25 17:26:57 +0000886a \verb/\n/ character is written at the end,
Guido van Rossumf2612d11991-11-21 13:53:03 +0000887unless the \verb\print\ statement ends with a comma.
888This is the only action if the statement contains just the keyword
889\verb\print\.
890
891Standard output is defined as the file object named \verb\stdout\
892in the built-in module \verb\sys\. If no such object exists,
893or if it is not a writable file, a {\tt RuntimeError} exception is raised.
894(The original implementation attempts to write to the system's original
895standard output instead, but this is not safe, and should be fixed.)
896
897\section{The {\tt return} statement}
898
899\begin{verbatim}
900return_stmt: 'return' [condition_list]
901\end{verbatim}
902
903\verb\return\ may only occur syntactically nested in a function
904definition, not within a nested class definition.
905
906If a condition list is present, it is evaluated, else \verb\None\
907is substituted.
908
909\verb\return\ leaves the current function call with the condition
910list (or \verb\None\) as return value.
911
912When \verb\return\ passes control out of a \verb\try\ statement
913with a \verb\finally\ clause, that finally clause is executed
914before really leaving the function.
915(XXX This should be made more exact, a la Modula-3.)
916
917\section{The {\tt raise} statement}
918
919\begin{verbatim}
920raise_stmt: 'raise' condition [',' condition]
921\end{verbatim}
922
923\verb\raise\ evaluates its first condition, which must yield
924a string object. If there is a second condition, this is evaluated,
925else \verb\None\ is substituted.
926
927It then raises the exception identified by the first object,
928with the second one (or \verb\None\) as its parameter.
929
930\section{The {\tt break} statement}
931
932\begin{verbatim}
933break_stmt: 'break'
934\end{verbatim}
935
936\verb\break\ may only occur syntactically nested in a \verb\for\
937or \verb\while\ loop, not nested in a function or class definition.
938
939It terminates the neares enclosing loop, skipping the optional
940\verb\else\ clause if the loop has one.
941
942If a \verb\for\ loop is terminated by \verb\break\, the loop control
943target (list) keeps its current value.
944
945When \verb\break\ passes control out of a \verb\try\ statement
946with a \verb\finally\ clause, that finally clause is executed
947before really leaving the loop.
948
949\section{The {\tt continue} statement}
950
951\begin{verbatim}
952continue_stmt: 'continue'
953\end{verbatim}
954
955\verb\continue\ may only occur syntactically nested in a \verb\for\
956or \verb\while\ loop, not nested in a function or class definition,
957and {\em not nested in a \verb\try\ statement with a \verb\finally\
958clause}.
959
960It continues with the next cycle of the nearest enclosing loop.
961
962\section{The {\tt import} statement}
963
964\begin{verbatim}
965import_stmt: 'import' identifier (',' identifier)*
966 | 'from' identifier 'import' identifier (',' identifier)*
967 | 'from' identifier 'import' '*'
968\end{verbatim}
969
970(XXX To be done.)
971
972\chapter{Compound statements}
973
974(XXX The semantic definitions of this chapter are still to be done.)
975
976\begin{verbatim}
977statement: stmt_list NEWLINE | compound_stmt
978compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
979suite: statement | NEWLINE INDENT statement+ DEDENT
980\end{verbatim}
981
982\section{The {\tt if} statement}
983
984\begin{verbatim}
985if_stmt: 'if' condition ':' suite
986 ('elif' condition ':' suite)*
987 ['else' ':' suite]
988\end{verbatim}
989
990\section{The {\tt while} statement}
991
992\begin{verbatim}
993while_stmt: 'while' condition ':' suite ['else' ':' suite]
994\end{verbatim}
995
996\section{The {\tt for} statement}
997
998\begin{verbatim}
999for_stmt: 'for' target_list 'in' condition_list ':' suite
1000 ['else' ':' suite]
1001\end{verbatim}
1002
1003\section{The {\tt try} statement}
1004
1005\begin{verbatim}
1006try_stmt: 'try' ':' suite
1007 ('except' condition [',' condition] ':' suite)*
1008 ['finally' ':' suite]
1009\end{verbatim}
1010
1011\section{Function definitions}
1012
1013\begin{verbatim}
1014funcdef: 'def' identifier '(' [parameter_list] ')' ':' suite
1015parameter_list: parameter (',' parameter)*
1016parameter: identifier | '(' parameter_list ')'
1017\end{verbatim}
1018
1019\section{Class definitions}
1020
1021\begin{verbatim}
1022classdef: 'class' identifier '(' ')' [inheritance] ':' suite
1023inheritance: '=' identifier '(' ')' (',' identifier '(' ')')*
1024\end{verbatim}
1025
1026XXX Syntax for scripts, modules
1027XXX Syntax for interactive input, eval, exec, input
1028
1029\end{document}