Cosmetic changes; added sections on notation and on objects;

new grammar (global, '==').
This commit is contained in:
Guido van Rossum 1992-01-07 16:43:53 +00:00
parent 1230c08b8e
commit 743d1e76d0
2 changed files with 454 additions and 218 deletions

View File

@ -60,6 +60,69 @@ informal introduction to the language, see the {\em Python Tutorial}.
This reference manual describes the Python programming language. This reference manual describes the Python programming language.
It is not intended as a tutorial. It is not intended as a tutorial.
While I am trying to be as precise as possible, I chose to use English
rather than formal specifications for everything except syntax and
lexical analysis. This should make the document better understandable
to the average reader, but will leave room for ambiguities.
Consequently, if you were coming from Mars and tried to re-implement
Python from this document alone, you might in fact be implementing
quite a different language. On the other hand, if you are using
Python and wonder what the precise rules about a particular area of
the language are, you should be able to find it here.
It is dangerous to add too many implementation details to a language
reference document -- the implementation may change, and other
implementations of the same language may work differently. On the
other hand, there is currently only one Python implementation, and
particular quirks of it are sometimes worth mentioning, especially
where it differs from the ``ideal'' specification.
Every Python implementation comes with a number of built-in and
standard modules. These are not documented here, but in the separate
{\em Python Library Reference} document. A few built-in modules are
mentioned when they interact in a significant way with the language
definition.
\section{Notation}
The descriptions of lexical analysis and syntax use a modified BNF
grammar notation. This uses the following style of definition:
\begin{verbatim}
name: lcletter (lcletter | "_")*
lcletter: "a"..."z"
\end{verbatim}
The first line says that a \verb\name\ is a \verb\lcletter\ followed by
a sequence of zero or more \verb\lcletter\s and underscores. A
\verb\lcletter\ in turn is any of the single characters `a' through `z'.
(This rule is actually adhered to for the names defined in syntax and
grammar rules in this document.)
Each rule begins with a name (which is the name defined by the rule)
followed by a colon. Each rule is wholly contained on one line. A
vertical bar (\verb\|\) is used to separate alternatives, it is the
least binding operator in this notation. A star (\verb\*\) means zero
or more repetitions of the preceding item; likewise, a plus (\verb\+\)
means one or more repetitions and a question mark (\verb\?\) zero or
one (in other words, the preceding item is optional). These three
operators bind as tight as possible; parentheses are used for
grouping. Literal strings are enclosed in double quotes. White space
is only meaningful to separate tokens.
In lexical definitions (as the example above), two more conventions
are used: Two literal characters separated by three dots mean a choice
of any single character in the given (inclusive) range of ASCII
characters. A phrase between angular brackets (\verb\<...>\) gives an
informal description of the symbol defined; e.g., this could be used
to describe the notion of `control character' if needed.
Although the notation used is almost the same, there is a big
difference between the meaning of lexical and syntactic definitions:
a lexical definition operates on the individual characters of the
input source, while a syntax definition operates on the stream of
tokens generated by the lexical analysis.
\chapter{Lexical analysis} \chapter{Lexical analysis}
A Python program is read by a {\em parser}. Input to the parser is a A Python program is read by a {\em parser}. Input to the parser is a
@ -130,11 +193,6 @@ Spaces and tabs are not tokens, but serve to delimit tokens. Where
ambiguity exists, a token comprises the longest possible string that ambiguity exists, a token comprises the longest possible string that
forms a legal token, when read from left to right. forms a legal token, when read from left to right.
Tokens are described using an extended regular expression notation.
This is similar to the extended BNF notation used later, except that
the notation \verb\<...>\ is used to give an informal description of a
character, and that spaces and tabs are not to be ignored.
\section{Identifiers} \section{Identifiers}
Identifiers are described by the following regular expressions: Identifiers are described by the following regular expressions:
@ -142,9 +200,9 @@ Identifiers are described by the following regular expressions:
\begin{verbatim} \begin{verbatim}
identifier: (letter|"_") (letter|digit|"_")* identifier: (letter|"_") (letter|digit|"_")*
letter: lowercase | uppercase letter: lowercase | uppercase
lowercase: "a"|"b"|...|"z" lowercase: "a"..."z"
uppercase: "A"|"B"|...|"Z" uppercase: "A"..."Z"
digit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" digit: "0"..."9"
\end{verbatim} \end{verbatim}
Identifiers are unlimited in length. Case is significant. Identifiers are unlimited in length. Case is significant.
@ -156,13 +214,14 @@ keywords} of the language, and may not be used as ordinary
identifiers. They must be spelled exactly as written here: identifiers. They must be spelled exactly as written here:
\begin{verbatim} \begin{verbatim}
and del for is raise and del for in print
break elif from not return break elif from is raise
class else if or try class else global not return
continue except import pass while continue except if or try
def finally in print def finally import pass while
\end{verbatim} \end{verbatim}
% # This Python program sorts and formats the above table
% import string % import string
% l = [] % l = []
% try: % try:
@ -185,8 +244,8 @@ String literals are described by the following regular expressions:
\begin{verbatim} \begin{verbatim}
stringliteral: "'" stringitem* "'" stringliteral: "'" stringitem* "'"
stringitem: stringchar | escapeseq stringitem: stringchar | escapeseq
stringchar: <any character except newline or "\" or "'"> stringchar: <any ASCII character except newline or "\" or "'">
escapeseq: "'" <any character except newline> escapeseq: "'" <any ASCII character except newline>
\end{verbatim} \end{verbatim}
String literals cannot span physical line boundaries. Escape String literals cannot span physical line boundaries. Escape
@ -208,7 +267,7 @@ are:
\verb/\t/ & ASCII Horizontal Tab (TAB) \\ \verb/\t/ & ASCII Horizontal Tab (TAB) \\
\verb/\v/ & ASCII Vertical Tab (VT) \\ \verb/\v/ & ASCII Vertical Tab (VT) \\
\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\ \verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
\verb/\x/{em xx...} & ASCII character with hex value {\em xx} \\ \verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\
\hline \hline
\end{tabular} \end{tabular}
\end{center} \end{center}
@ -221,9 +280,10 @@ are used...).
All unrecognized escape sequences are left in the string {\em All unrecognized escape sequences are left in the string {\em
unchanged}, i.e., the backslash is left in the string. (This rule is unchanged}, i.e., the backslash is left in the string. (This rule is
useful when debugging: if an escape sequence is mistyped, the useful when debugging: if an escape sequence is mistyped, the
resulting output is more easily recognized as broken. It also helps resulting output is more easily recognized as broken. It also helps a
somewhat for string literals used as regular expressions or otherwise great deal for string literals used as regular expressions or
passed to other modules that do their own escape handling.) otherwise passed to other modules that do their own escape handling --
but you may end up quadrupling backslashes that must appear literally.)
\subsection{Numeric literals} \subsection{Numeric literals}
@ -239,9 +299,9 @@ decimalinteger: nonzerodigit digit* | "0"
octinteger: "0" octdigit+ octinteger: "0" octdigit+
hexinteger: "0" ("x"|"X") hexdigit+ hexinteger: "0" ("x"|"X") hexdigit+
nonzerodigit: "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" nonzerodigit: "1"..."9"
octdigit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7" octdigit: "0"..."7"
hexdigit: digit|"a"|"b"|"c"|"d"|"e"|"f"|"A"|"B"|"C"|"D"|"E"|"F" hexdigit: digit|"a"..."f"|"A"..."F"
\end{verbatim} \end{verbatim}
Floating point numbers are described by the following regular expressions: Floating point numbers are described by the following regular expressions:
@ -260,16 +320,20 @@ The following tokens are operators:
\begin{verbatim} \begin{verbatim}
+ - * / % + - * / %
<< >> & | ^ ~ << >> & | ^ ~
< = == > <= <> != >= < == > <= <> != >=
\end{verbatim} \end{verbatim}
The comparison operators \verb\<>\ and \verb\!=\ are alternate
spellings of the same operator.
\section{Delimiters} \section{Delimiters}
The following tokens are delimiters: The following tokens serve as delimiters or otherwise have a special
meaning:
\begin{verbatim} \begin{verbatim}
( ) [ ] { } ( ) [ ] { }
; , : . ` ; , : . ` =
\end{verbatim} \end{verbatim}
The following printing ASCII characters are currently not used; The following printing ASCII characters are currently not used;
@ -281,35 +345,83 @@ their occurrence is an unconditional error:
\chapter{Execution model} \chapter{Execution model}
(XXX This chapter should explain the general model (XXX This chapter should explain the general model of the execution of
of the execution of Python code and Python code and the evaluation of expressions. It should introduce
the evaluation of expressions. objects, values, code blocks, scopes, name spaces, name binding,
It should introduce objects, values, code blocks, scopes, name spaces, types, sequences, numbers, mappings, exceptions, and other technical
name binding, terms needed to make the following chapters concise and exact.)
types, sequences, numbers, mappings,
exceptions, and other technical terms needed to make the following \section{Objects, values and types}
chapters concise and exact.)
I won't try to define rigorously here what an object is, but I'll give
some properties of objects that are important to know about.
Every object has an identity, a type and a value. An object's {\em
identity} never changes once it has been created; think of it as the
object's (permanent) address. An object's {\em type} determines the
operations that an object supports (e.g., can its length be taken?)
and also defines the ``meaning'' of the object's value; it also never
changes. The {\em value} of some objects can change; whether an
object's value can change is a property of its type.
Objects are never explicitly destroyed; however, when they become
unreachable they may be garbage-collected. An implementation,
however, is allowed to delay garbage collection or omit it altogether
-- it is a matter of implementation quality how garbage collection is
implemented. (Implementation note: the current implementation uses a
reference-counting scheme which collects most objects as soon as they
become onreachable, but does not detect garbage containing circular
references.)
(Some objects contain references to ``external'' resources such as
open files. It is understood that these resources are freed when the
object is garbage-collected, but since garbage collection is not
guaranteed such objects also provide an explicit way to release the
external resource (e.g., a \verb\close\ method) and programs are
recommended to use this.)
Some objects contain references to other objects. These references
are part of the object's value; in most cases, when such a
``container'' object is compared to another (of the same type), the
comparison takes the {\em values} of the referenced objects into
account (not their identities).
Except for their identity, types affect almost any aspect of objects.
Even object identities are affected in some sense: for immutable
types, operations that compute new values may actually return a
reference to an existing object with the same type and value, while
for mutable objects this is not allowed. E.g., after
\begin{verbatim}
a = 1; b = 1; c = []; d = []
\end{verbatim}
\verb\a\ and \verb\b\ may or may not refer to the same object, but
\verb\c\ and \verb\d\ are guaranteed to refer to two different, unique,
newly created lists.
\section{Execution frames, name spaces, and scopes}
XXX
\chapter{Expressions and conditions} \chapter{Expressions and conditions}
(From now on, extended BNF notation will be used to describe From now on, extended BNF notation will be used to describe syntax,
syntax, not lexical analysis.) not lexical analysis.
(XXX Explain the notation.)
This chapter explains the meaning of the elements of expressions and This chapter explains the meaning of the elements of expressions and
conditions. Conditions are a superset of expressions, and a condition conditions. Conditions are a superset of expressions, and a condition
may be used where an expression is required by enclosing it in may be used where an expression is required by enclosing it in
parentheses. The only place where an unparenthesized condition parentheses. The only place where an unparenthesized condition is not
is not allowed is on the right-hand side of the assignment operator, allowed is on the right-hand side of the assignment operator, because
because this operator is the same token (\verb\=\) as used for this operator is the same token (\verb\=\) as used for compasisons.
compasisons.
The comma plays a somewhat special role in Python's syntax. The comma plays a somewhat special role in Python's syntax. It is an
It is an operator with a lower precedence than all others, but operator with a lower precedence than all others, but occasionally
occasionally serves other purposes as well (e.g., it has special serves other purposes as well (e.g., it has special semantics in print
semantics in print statements). When a comma is accepted by the statements). When a comma is accepted by the syntax, one of the
syntax, one of the syntactic categories \verb\expression_list\ syntactic categories \verb\expression_list\ or \verb\condition_list\
or \verb\condition_list\ is always used. is always used.
When (one alternative of) a syntax rule has the form When (one alternative of) a syntax rule has the form
@ -351,11 +463,11 @@ Syntax rules for atoms:
atom: identifier | literal | parenth_form | string_conversion atom: identifier | literal | parenth_form | string_conversion
literal: stringliteral | integer | longinteger | floatnumber literal: stringliteral | integer | longinteger | floatnumber
parenth_form: enclosure | list_display | dict_display parenth_form: enclosure | list_display | dict_display
enclosure: '(' [condition_list] ')' enclosure: "(" [condition_list] ")"
list_display: '[' [condition_list] ']' list_display: "[" [condition_list] "]"
dict_display: '{' [key_datum (',' key_datum)* [','] '}' dict_display: "{" [key_datum ("," key_datum)* [","] "}"
key_datum: condition ':' condition key_datum: condition ":" condition
string_conversion:'`' condition_list '`' string_conversion:"`" condition_list "`"
\end{verbatim} \end{verbatim}
\subsection{Identifiers (Names)} \subsection{Identifiers (Names)}
@ -413,10 +525,9 @@ define the entries of the dictionary:
each key object is used as a key into the dictionary to store each key object is used as a key into the dictionary to store
the corresponding datum pair. the corresponding datum pair.
Key objects must be strings, otherwise a {\tt TypeError} Keys must be strings, otherwise a {\tt TypeError} exception is raised.
exception is raised. Clashes between keys are not detected; the last datum (textually
Clashes between keys are not detected; the last datum stored for a given rightmost in the display) stored for a given key value prevails.
key value prevails.
\subsection{String conversions} \subsection{String conversions}
@ -445,10 +556,10 @@ Their syntax is:
\begin{verbatim} \begin{verbatim}
primary: atom | attributeref | call | subscription | slicing primary: atom | attributeref | call | subscription | slicing
attributeref: primary '.' identifier attributeref: primary "." identifier
call: primary '(' [condition_list] ')' call: primary "(" [condition_list] ")"
subscription: primary '[' condition ']' subscription: primary "[" condition "]"
slicing: primary '[' [condition] ':' [condition] ']' slicing: primary "[" [condition] ":" [condition] "]"
\end{verbatim} \end{verbatim}
\subsection{Attribute references} \subsection{Attribute references}
@ -465,7 +576,7 @@ Factors represent the unary numeric operators.
Their syntax is: Their syntax is:
\begin{verbatim} \begin{verbatim}
factor: primary | '-' factor | '+' factor | '~' factor factor: primary | "-" factor | "+" factor | "~" factor
\end{verbatim} \end{verbatim}
The unary \verb\-\ operator yields the negative of its numeric argument. The unary \verb\-\ operator yields the negative of its numeric argument.
@ -483,7 +594,7 @@ a {\tt TypeError} exception is raised.
Terms represent the most tightly binding binary operators: Terms represent the most tightly binding binary operators:
\begin{verbatim} \begin{verbatim}
term: factor | term '*' factor | term '/' factor | term '%' factor term: factor | term "*" factor | term "/" factor | term "%" factor
\end{verbatim} \end{verbatim}
The \verb\*\ operator yields the product of its arguments. The \verb\*\ operator yields the product of its arguments.
@ -494,13 +605,13 @@ and then multiplied together.
In the latter case, string repetition is performed; a negative In the latter case, string repetition is performed; a negative
repetition factor yields the empty string. repetition factor yields the empty string.
The \verb|'/'| operator yields the quotient of its arguments. The \verb|"/"| operator yields the quotient of its arguments.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
(Short or long) integer division yields an integer of the same type, (Short or long) integer division yields an integer of the same type,
truncating towards zero. truncating towards zero.
Division by zero raises a {\tt RuntimeError} exception. Division by zero raises a {\tt RuntimeError} exception.
The \verb|'%'| operator yields the remainder from the division The \verb|"%"| operator yields the remainder from the division
of the first argument by the second. of the first argument by the second.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$. The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$.
@ -511,28 +622,28 @@ $3.14 \% 0.7$ equals $0.34$.
\section{Arithmetic expressions} \section{Arithmetic expressions}
\begin{verbatim} \begin{verbatim}
arith_expr: term | arith_expr '+' term | arith_expr '-' term arith_expr: term | arith_expr "+" term | arith_expr "-" term
\end{verbatim} \end{verbatim}
The \verb|'+'| operator yields the sum of its arguments. The \verb|"+"| operator yields the sum of its arguments.
The arguments must either both be numbers, or both strings. The arguments must either both be numbers, or both strings.
In the former case, the numbers are converted to a common type In the former case, the numbers are converted to a common type
and then added together. and then added together.
In the latter case, the strings are concatenated directly, In the latter case, the strings are concatenated directly,
without inserting a space. without inserting a space.
The \verb|'-'| operator yields the difference of its arguments. The \verb|"-"| operator yields the difference of its arguments.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
\section{Shift expressions} \section{Shift expressions}
\begin{verbatim} \begin{verbatim}
shift_expr: arith_expr | shift_expr '<<' arith_expr | shift_expr '>>' arith_expr shift_expr: arith_expr | shift_expr "<<" arith_expr | shift_expr ">>" arith_expr
\end{verbatim} \end{verbatim}
These operators accept short integers as arguments only. These operators accept short integers as arguments only.
They shift their left argument to the left or right by the number of bits They shift their left argument to the left or right by the number of bits
given by the right argument. Shifts are ``logical'', e.g., bits shifted given by the right argument. Shifts are ``logical"", e.g., bits shifted
out on one end are lost, and bits shifted in are zero; out on one end are lost, and bits shifted in are zero;
negative numbers are shifted as if they were unsigned in C. negative numbers are shifted as if they were unsigned in C.
Negative shift counts and shift counts greater than {\em or equal to} Negative shift counts and shift counts greater than {\em or equal to}
@ -541,7 +652,7 @@ the word size yield undefined results.
\section{Bitwise AND expressions} \section{Bitwise AND expressions}
\begin{verbatim} \begin{verbatim}
and_expr: shift_expr | and_expr '&' shift_expr and_expr: shift_expr | and_expr "&" shift_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise AND of its arguments, This operator yields the bitwise AND of its arguments,
@ -550,7 +661,7 @@ which must be short integers.
\section{Bitwise XOR expressions} \section{Bitwise XOR expressions}
\begin{verbatim} \begin{verbatim}
xor_expr: and_expr | xor_expr '^' and_expr xor_expr: and_expr | xor_expr "^" and_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise exclusive OR of its arguments, This operator yields the bitwise exclusive OR of its arguments,
@ -559,7 +670,7 @@ which must be short integers.
\section{Bitwise OR expressions} \section{Bitwise OR expressions}
\begin{verbatim} \begin{verbatim}
or_expr: xor_expr | or_expr '|' xor_expr or_expr: xor_expr | or_expr "|" xor_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise OR of its arguments, This operator yields the bitwise OR of its arguments,
@ -569,7 +680,7 @@ which must be short integers.
\begin{verbatim} \begin{verbatim}
expression: or_expression expression: or_expression
expr_list: expression (',' expression)* [','] expr_list: expression ("," expression)* [","]
\end{verbatim} \end{verbatim}
An expression list containing at least one comma yields a new tuple. An expression list containing at least one comma yields a new tuple.
@ -587,7 +698,7 @@ To create an empty tuple, use an empty pair of parentheses: \verb\()\.
\begin{verbatim} \begin{verbatim}
comparison: expression (comp_operator expression)* comparison: expression (comp_operator expression)*
comp_operator: '<'|'>'|'='|'=='|'>='|'<='|'<>'|'!='|['not'] 'in'|is' ['not'] comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
\end{verbatim} \end{verbatim}
Comparisons yield integer value: 1 for true, 0 for false. Comparisons yield integer value: 1 for true, 0 for false.
@ -605,12 +716,9 @@ $e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
For the benefit of C programmers, The forms \verb\<>\ and \verb\!=\ are equivalent.
the comparison operators \verb\=\ and \verb\==\ are equivalent,
and so are \verb\<>\ and \verb\!=\.
Use of the C variants is discouraged.
The operators {\tt '<', '>', '=', '>=', '<='}, and {\tt '<>'} compare The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "<>"} compare
the values of two objects. The objects needn't have the same type. the values of two objects. The objects needn't have the same type.
If both are numbers, they are compared to a common type. If both are numbers, they are compared to a common type.
Otherwise, objects of different types {\em always} compare unequal, Otherwise, objects of different types {\em always} compare unequal,
@ -652,9 +760,9 @@ $x {\tt is not} y$ yields the inverse truth value.
\begin{verbatim} \begin{verbatim}
condition: or_test condition: or_test
or_test: and_test | or_test 'or' and_test or_test: and_test | or_test "or" and_test
and_test: not_test | and_test 'and' not_test and_test: not_test | and_test "and" not_test
not_test: comparison | 'not' not_test not_test: comparison | "not" not_test
\end{verbatim} \end{verbatim}
In the context of Boolean operators, and also when conditions are In the context of Boolean operators, and also when conditions are
@ -686,7 +794,7 @@ Several simple statements may occor on a single line separated
by semicolons. The syntax for simple statements is: by semicolons. The syntax for simple statements is:
\begin{verbatim} \begin{verbatim}
stmt_list: simple_stmt (';' simple_stmt)* [';'] stmt_list: simple_stmt (";" simple_stmt)* [";"]
simple_stmt: expression_stmt simple_stmt: expression_stmt
| assignment | assignment
| pass_stmt | pass_stmt
@ -697,6 +805,7 @@ simple_stmt: expression_stmt
| break_stmt | break_stmt
| continue_stmt | continue_stmt
| import_stmt | import_stmt
| global_stmt
\end{verbatim} \end{verbatim}
\section{Expression statements} \section{Expression statements}
@ -718,9 +827,9 @@ do not cause any output.)
\section{Assignments} \section{Assignments}
\begin{verbatim} \begin{verbatim}
assignment: target_list ('=' target_list)* '=' expression_list assignment: target_list ("=" target_list)* "=" expression_list
target_list: target (',' target)* [','] target_list: target ("," target)* [","]
target: identifier | '(' target_list ')' | '[' target_list ']' target: identifier | "(" target_list ")" | "[" target_list "]"
| attributeref | subscription | slicing | attributeref | subscription | slicing
\end{verbatim} \end{verbatim}
@ -835,7 +944,7 @@ messages.)
\section{The {\tt pass} statement} \section{The {\tt pass} statement}
\begin{verbatim} \begin{verbatim}
pass_stmt: 'pass' pass_stmt: "pass"
\end{verbatim} \end{verbatim}
{\tt pass} is a null operation -- when it is executed, {\tt pass} is a null operation -- when it is executed,
@ -844,7 +953,7 @@ nothing happens.
\section{The {\tt del} statement} \section{The {\tt del} statement}
\begin{verbatim} \begin{verbatim}
del_stmt: 'del' target_list del_stmt: "del" target_list
\end{verbatim} \end{verbatim}
Deletion is recursively defined similar to assignment. Deletion is recursively defined similar to assignment.
@ -866,7 +975,7 @@ right type (but even this is determined by the sliced object).
\section{The {\tt print} statement} \section{The {\tt print} statement}
\begin{verbatim} \begin{verbatim}
print_stmt: 'print' [ condition (',' condition)* [','] ] print_stmt: "print" [ condition ("," condition)* [","] ]
\end{verbatim} \end{verbatim}
{\tt print} evaluates each condition in turn and writes the resulting {\tt print} evaluates each condition in turn and writes the resulting
@ -897,7 +1006,7 @@ standard output instead, but this is not safe, and should be fixed.)
\section{The {\tt return} statement} \section{The {\tt return} statement}
\begin{verbatim} \begin{verbatim}
return_stmt: 'return' [condition_list] return_stmt: "return" [condition_list]
\end{verbatim} \end{verbatim}
\verb\return\ may only occur syntactically nested in a function \verb\return\ may only occur syntactically nested in a function
@ -917,7 +1026,7 @@ before really leaving the function.
\section{The {\tt raise} statement} \section{The {\tt raise} statement}
\begin{verbatim} \begin{verbatim}
raise_stmt: 'raise' condition [',' condition] raise_stmt: "raise" condition ["," condition]
\end{verbatim} \end{verbatim}
\verb\raise\ evaluates its first condition, which must yield \verb\raise\ evaluates its first condition, which must yield
@ -930,7 +1039,7 @@ with the second one (or \verb\None\) as its parameter.
\section{The {\tt break} statement} \section{The {\tt break} statement}
\begin{verbatim} \begin{verbatim}
break_stmt: 'break' break_stmt: "break"
\end{verbatim} \end{verbatim}
\verb\break\ may only occur syntactically nested in a \verb\for\ \verb\break\ may only occur syntactically nested in a \verb\for\
@ -949,7 +1058,7 @@ before really leaving the loop.
\section{The {\tt continue} statement} \section{The {\tt continue} statement}
\begin{verbatim} \begin{verbatim}
continue_stmt: 'continue' continue_stmt: "continue"
\end{verbatim} \end{verbatim}
\verb\continue\ may only occur syntactically nested in a \verb\for\ \verb\continue\ may only occur syntactically nested in a \verb\for\
@ -962,9 +1071,17 @@ It continues with the next cycle of the nearest enclosing loop.
\section{The {\tt import} statement} \section{The {\tt import} statement}
\begin{verbatim} \begin{verbatim}
import_stmt: 'import' identifier (',' identifier)* import_stmt: "import" identifier ("," identifier)*
| 'from' identifier 'import' identifier (',' identifier)* | "from" identifier "import" identifier ("," identifier)*
| 'from' identifier 'import' '*' | "from" identifier "import" "*"
\end{verbatim}
(XXX To be done.)
\section{The {\tt global} statement}
\begin{verbatim}
global_stmt: "global" identifier ("," identifier)*
\end{verbatim} \end{verbatim}
(XXX To be done.) (XXX To be done.)
@ -982,48 +1099,49 @@ suite: statement | NEWLINE INDENT statement+ DEDENT
\section{The {\tt if} statement} \section{The {\tt if} statement}
\begin{verbatim} \begin{verbatim}
if_stmt: 'if' condition ':' suite if_stmt: "if" condition ":" suite
('elif' condition ':' suite)* ("elif" condition ":" suite)*
['else' ':' suite] ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt while} statement} \section{The {\tt while} statement}
\begin{verbatim} \begin{verbatim}
while_stmt: 'while' condition ':' suite ['else' ':' suite] while_stmt: "while" condition ":" suite ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt for} statement} \section{The {\tt for} statement}
\begin{verbatim} \begin{verbatim}
for_stmt: 'for' target_list 'in' condition_list ':' suite for_stmt: "for" target_list "in" condition_list ":" suite
['else' ':' suite] ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt try} statement} \section{The {\tt try} statement}
\begin{verbatim} \begin{verbatim}
try_stmt: 'try' ':' suite try_stmt: "try" ":" suite
('except' condition [',' condition] ':' suite)* ("except" condition ["," condition] ":" suite)*
['finally' ':' suite] ["finally" ":" suite]
\end{verbatim} \end{verbatim}
\section{Function definitions} \section{Function definitions}
\begin{verbatim} \begin{verbatim}
funcdef: 'def' identifier '(' [parameter_list] ')' ':' suite funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
parameter_list: parameter (',' parameter)* parameter_list: parameter ("," parameter)*
parameter: identifier | '(' parameter_list ')' parameter: identifier | "(" parameter_list ")"
\end{verbatim} \end{verbatim}
\section{Class definitions} \section{Class definitions}
\begin{verbatim} \begin{verbatim}
classdef: 'class' identifier '(' ')' [inheritance] ':' suite classdef: "class" identifier [inheritance] ":" suite
inheritance: '=' identifier '(' ')' (',' identifier '(' ')')* inheritance: "(" expression ("," expression)* ")"
\end{verbatim} \end{verbatim}
XXX Syntax for scripts, modules XXX Syntax for scripts, modules
XXX Syntax for interactive input, eval, exec, input XXX Syntax for interactive input, eval, exec, input
XXX New definition of expressions (as conditions)
\end{document} \end{document}

View File

@ -60,6 +60,69 @@ informal introduction to the language, see the {\em Python Tutorial}.
This reference manual describes the Python programming language. This reference manual describes the Python programming language.
It is not intended as a tutorial. It is not intended as a tutorial.
While I am trying to be as precise as possible, I chose to use English
rather than formal specifications for everything except syntax and
lexical analysis. This should make the document better understandable
to the average reader, but will leave room for ambiguities.
Consequently, if you were coming from Mars and tried to re-implement
Python from this document alone, you might in fact be implementing
quite a different language. On the other hand, if you are using
Python and wonder what the precise rules about a particular area of
the language are, you should be able to find it here.
It is dangerous to add too many implementation details to a language
reference document -- the implementation may change, and other
implementations of the same language may work differently. On the
other hand, there is currently only one Python implementation, and
particular quirks of it are sometimes worth mentioning, especially
where it differs from the ``ideal'' specification.
Every Python implementation comes with a number of built-in and
standard modules. These are not documented here, but in the separate
{\em Python Library Reference} document. A few built-in modules are
mentioned when they interact in a significant way with the language
definition.
\section{Notation}
The descriptions of lexical analysis and syntax use a modified BNF
grammar notation. This uses the following style of definition:
\begin{verbatim}
name: lcletter (lcletter | "_")*
lcletter: "a"..."z"
\end{verbatim}
The first line says that a \verb\name\ is a \verb\lcletter\ followed by
a sequence of zero or more \verb\lcletter\s and underscores. A
\verb\lcletter\ in turn is any of the single characters `a' through `z'.
(This rule is actually adhered to for the names defined in syntax and
grammar rules in this document.)
Each rule begins with a name (which is the name defined by the rule)
followed by a colon. Each rule is wholly contained on one line. A
vertical bar (\verb\|\) is used to separate alternatives, it is the
least binding operator in this notation. A star (\verb\*\) means zero
or more repetitions of the preceding item; likewise, a plus (\verb\+\)
means one or more repetitions and a question mark (\verb\?\) zero or
one (in other words, the preceding item is optional). These three
operators bind as tight as possible; parentheses are used for
grouping. Literal strings are enclosed in double quotes. White space
is only meaningful to separate tokens.
In lexical definitions (as the example above), two more conventions
are used: Two literal characters separated by three dots mean a choice
of any single character in the given (inclusive) range of ASCII
characters. A phrase between angular brackets (\verb\<...>\) gives an
informal description of the symbol defined; e.g., this could be used
to describe the notion of `control character' if needed.
Although the notation used is almost the same, there is a big
difference between the meaning of lexical and syntactic definitions:
a lexical definition operates on the individual characters of the
input source, while a syntax definition operates on the stream of
tokens generated by the lexical analysis.
\chapter{Lexical analysis} \chapter{Lexical analysis}
A Python program is read by a {\em parser}. Input to the parser is a A Python program is read by a {\em parser}. Input to the parser is a
@ -130,11 +193,6 @@ Spaces and tabs are not tokens, but serve to delimit tokens. Where
ambiguity exists, a token comprises the longest possible string that ambiguity exists, a token comprises the longest possible string that
forms a legal token, when read from left to right. forms a legal token, when read from left to right.
Tokens are described using an extended regular expression notation.
This is similar to the extended BNF notation used later, except that
the notation \verb\<...>\ is used to give an informal description of a
character, and that spaces and tabs are not to be ignored.
\section{Identifiers} \section{Identifiers}
Identifiers are described by the following regular expressions: Identifiers are described by the following regular expressions:
@ -142,9 +200,9 @@ Identifiers are described by the following regular expressions:
\begin{verbatim} \begin{verbatim}
identifier: (letter|"_") (letter|digit|"_")* identifier: (letter|"_") (letter|digit|"_")*
letter: lowercase | uppercase letter: lowercase | uppercase
lowercase: "a"|"b"|...|"z" lowercase: "a"..."z"
uppercase: "A"|"B"|...|"Z" uppercase: "A"..."Z"
digit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" digit: "0"..."9"
\end{verbatim} \end{verbatim}
Identifiers are unlimited in length. Case is significant. Identifiers are unlimited in length. Case is significant.
@ -156,13 +214,14 @@ keywords} of the language, and may not be used as ordinary
identifiers. They must be spelled exactly as written here: identifiers. They must be spelled exactly as written here:
\begin{verbatim} \begin{verbatim}
and del for is raise and del for in print
break elif from not return break elif from is raise
class else if or try class else global not return
continue except import pass while continue except if or try
def finally in print def finally import pass while
\end{verbatim} \end{verbatim}
% # This Python program sorts and formats the above table
% import string % import string
% l = [] % l = []
% try: % try:
@ -185,8 +244,8 @@ String literals are described by the following regular expressions:
\begin{verbatim} \begin{verbatim}
stringliteral: "'" stringitem* "'" stringliteral: "'" stringitem* "'"
stringitem: stringchar | escapeseq stringitem: stringchar | escapeseq
stringchar: <any character except newline or "\" or "'"> stringchar: <any ASCII character except newline or "\" or "'">
escapeseq: "'" <any character except newline> escapeseq: "'" <any ASCII character except newline>
\end{verbatim} \end{verbatim}
String literals cannot span physical line boundaries. Escape String literals cannot span physical line boundaries. Escape
@ -208,7 +267,7 @@ are:
\verb/\t/ & ASCII Horizontal Tab (TAB) \\ \verb/\t/ & ASCII Horizontal Tab (TAB) \\
\verb/\v/ & ASCII Vertical Tab (VT) \\ \verb/\v/ & ASCII Vertical Tab (VT) \\
\verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\ \verb/\/{\em ooo} & ASCII character with octal value {\em ooo} \\
\verb/\x/{em xx...} & ASCII character with hex value {\em xx} \\ \verb/\x/{em xx...} & ASCII character with hex value {\em xx...} \\
\hline \hline
\end{tabular} \end{tabular}
\end{center} \end{center}
@ -221,9 +280,10 @@ are used...).
All unrecognized escape sequences are left in the string {\em All unrecognized escape sequences are left in the string {\em
unchanged}, i.e., the backslash is left in the string. (This rule is unchanged}, i.e., the backslash is left in the string. (This rule is
useful when debugging: if an escape sequence is mistyped, the useful when debugging: if an escape sequence is mistyped, the
resulting output is more easily recognized as broken. It also helps resulting output is more easily recognized as broken. It also helps a
somewhat for string literals used as regular expressions or otherwise great deal for string literals used as regular expressions or
passed to other modules that do their own escape handling.) otherwise passed to other modules that do their own escape handling --
but you may end up quadrupling backslashes that must appear literally.)
\subsection{Numeric literals} \subsection{Numeric literals}
@ -239,9 +299,9 @@ decimalinteger: nonzerodigit digit* | "0"
octinteger: "0" octdigit+ octinteger: "0" octdigit+
hexinteger: "0" ("x"|"X") hexdigit+ hexinteger: "0" ("x"|"X") hexdigit+
nonzerodigit: "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" nonzerodigit: "1"..."9"
octdigit: "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7" octdigit: "0"..."7"
hexdigit: digit|"a"|"b"|"c"|"d"|"e"|"f"|"A"|"B"|"C"|"D"|"E"|"F" hexdigit: digit|"a"..."f"|"A"..."F"
\end{verbatim} \end{verbatim}
Floating point numbers are described by the following regular expressions: Floating point numbers are described by the following regular expressions:
@ -260,16 +320,20 @@ The following tokens are operators:
\begin{verbatim} \begin{verbatim}
+ - * / % + - * / %
<< >> & | ^ ~ << >> & | ^ ~
< = == > <= <> != >= < == > <= <> != >=
\end{verbatim} \end{verbatim}
The comparison operators \verb\<>\ and \verb\!=\ are alternate
spellings of the same operator.
\section{Delimiters} \section{Delimiters}
The following tokens are delimiters: The following tokens serve as delimiters or otherwise have a special
meaning:
\begin{verbatim} \begin{verbatim}
( ) [ ] { } ( ) [ ] { }
; , : . ` ; , : . ` =
\end{verbatim} \end{verbatim}
The following printing ASCII characters are currently not used; The following printing ASCII characters are currently not used;
@ -281,35 +345,83 @@ their occurrence is an unconditional error:
\chapter{Execution model} \chapter{Execution model}
(XXX This chapter should explain the general model (XXX This chapter should explain the general model of the execution of
of the execution of Python code and Python code and the evaluation of expressions. It should introduce
the evaluation of expressions. objects, values, code blocks, scopes, name spaces, name binding,
It should introduce objects, values, code blocks, scopes, name spaces, types, sequences, numbers, mappings, exceptions, and other technical
name binding, terms needed to make the following chapters concise and exact.)
types, sequences, numbers, mappings,
exceptions, and other technical terms needed to make the following \section{Objects, values and types}
chapters concise and exact.)
I won't try to define rigorously here what an object is, but I'll give
some properties of objects that are important to know about.
Every object has an identity, a type and a value. An object's {\em
identity} never changes once it has been created; think of it as the
object's (permanent) address. An object's {\em type} determines the
operations that an object supports (e.g., can its length be taken?)
and also defines the ``meaning'' of the object's value; it also never
changes. The {\em value} of some objects can change; whether an
object's value can change is a property of its type.
Objects are never explicitly destroyed; however, when they become
unreachable they may be garbage-collected. An implementation,
however, is allowed to delay garbage collection or omit it altogether
-- it is a matter of implementation quality how garbage collection is
implemented. (Implementation note: the current implementation uses a
reference-counting scheme which collects most objects as soon as they
become onreachable, but does not detect garbage containing circular
references.)
(Some objects contain references to ``external'' resources such as
open files. It is understood that these resources are freed when the
object is garbage-collected, but since garbage collection is not
guaranteed such objects also provide an explicit way to release the
external resource (e.g., a \verb\close\ method) and programs are
recommended to use this.)
Some objects contain references to other objects. These references
are part of the object's value; in most cases, when such a
``container'' object is compared to another (of the same type), the
comparison takes the {\em values} of the referenced objects into
account (not their identities).
Except for their identity, types affect almost any aspect of objects.
Even object identities are affected in some sense: for immutable
types, operations that compute new values may actually return a
reference to an existing object with the same type and value, while
for mutable objects this is not allowed. E.g., after
\begin{verbatim}
a = 1; b = 1; c = []; d = []
\end{verbatim}
\verb\a\ and \verb\b\ may or may not refer to the same object, but
\verb\c\ and \verb\d\ are guaranteed to refer to two different, unique,
newly created lists.
\section{Execution frames, name spaces, and scopes}
XXX
\chapter{Expressions and conditions} \chapter{Expressions and conditions}
(From now on, extended BNF notation will be used to describe From now on, extended BNF notation will be used to describe syntax,
syntax, not lexical analysis.) not lexical analysis.
(XXX Explain the notation.)
This chapter explains the meaning of the elements of expressions and This chapter explains the meaning of the elements of expressions and
conditions. Conditions are a superset of expressions, and a condition conditions. Conditions are a superset of expressions, and a condition
may be used where an expression is required by enclosing it in may be used where an expression is required by enclosing it in
parentheses. The only place where an unparenthesized condition parentheses. The only place where an unparenthesized condition is not
is not allowed is on the right-hand side of the assignment operator, allowed is on the right-hand side of the assignment operator, because
because this operator is the same token (\verb\=\) as used for this operator is the same token (\verb\=\) as used for compasisons.
compasisons.
The comma plays a somewhat special role in Python's syntax. The comma plays a somewhat special role in Python's syntax. It is an
It is an operator with a lower precedence than all others, but operator with a lower precedence than all others, but occasionally
occasionally serves other purposes as well (e.g., it has special serves other purposes as well (e.g., it has special semantics in print
semantics in print statements). When a comma is accepted by the statements). When a comma is accepted by the syntax, one of the
syntax, one of the syntactic categories \verb\expression_list\ syntactic categories \verb\expression_list\ or \verb\condition_list\
or \verb\condition_list\ is always used. is always used.
When (one alternative of) a syntax rule has the form When (one alternative of) a syntax rule has the form
@ -351,11 +463,11 @@ Syntax rules for atoms:
atom: identifier | literal | parenth_form | string_conversion atom: identifier | literal | parenth_form | string_conversion
literal: stringliteral | integer | longinteger | floatnumber literal: stringliteral | integer | longinteger | floatnumber
parenth_form: enclosure | list_display | dict_display parenth_form: enclosure | list_display | dict_display
enclosure: '(' [condition_list] ')' enclosure: "(" [condition_list] ")"
list_display: '[' [condition_list] ']' list_display: "[" [condition_list] "]"
dict_display: '{' [key_datum (',' key_datum)* [','] '}' dict_display: "{" [key_datum ("," key_datum)* [","] "}"
key_datum: condition ':' condition key_datum: condition ":" condition
string_conversion:'`' condition_list '`' string_conversion:"`" condition_list "`"
\end{verbatim} \end{verbatim}
\subsection{Identifiers (Names)} \subsection{Identifiers (Names)}
@ -413,10 +525,9 @@ define the entries of the dictionary:
each key object is used as a key into the dictionary to store each key object is used as a key into the dictionary to store
the corresponding datum pair. the corresponding datum pair.
Key objects must be strings, otherwise a {\tt TypeError} Keys must be strings, otherwise a {\tt TypeError} exception is raised.
exception is raised. Clashes between keys are not detected; the last datum (textually
Clashes between keys are not detected; the last datum stored for a given rightmost in the display) stored for a given key value prevails.
key value prevails.
\subsection{String conversions} \subsection{String conversions}
@ -445,10 +556,10 @@ Their syntax is:
\begin{verbatim} \begin{verbatim}
primary: atom | attributeref | call | subscription | slicing primary: atom | attributeref | call | subscription | slicing
attributeref: primary '.' identifier attributeref: primary "." identifier
call: primary '(' [condition_list] ')' call: primary "(" [condition_list] ")"
subscription: primary '[' condition ']' subscription: primary "[" condition "]"
slicing: primary '[' [condition] ':' [condition] ']' slicing: primary "[" [condition] ":" [condition] "]"
\end{verbatim} \end{verbatim}
\subsection{Attribute references} \subsection{Attribute references}
@ -465,7 +576,7 @@ Factors represent the unary numeric operators.
Their syntax is: Their syntax is:
\begin{verbatim} \begin{verbatim}
factor: primary | '-' factor | '+' factor | '~' factor factor: primary | "-" factor | "+" factor | "~" factor
\end{verbatim} \end{verbatim}
The unary \verb\-\ operator yields the negative of its numeric argument. The unary \verb\-\ operator yields the negative of its numeric argument.
@ -483,7 +594,7 @@ a {\tt TypeError} exception is raised.
Terms represent the most tightly binding binary operators: Terms represent the most tightly binding binary operators:
\begin{verbatim} \begin{verbatim}
term: factor | term '*' factor | term '/' factor | term '%' factor term: factor | term "*" factor | term "/" factor | term "%" factor
\end{verbatim} \end{verbatim}
The \verb\*\ operator yields the product of its arguments. The \verb\*\ operator yields the product of its arguments.
@ -494,13 +605,13 @@ and then multiplied together.
In the latter case, string repetition is performed; a negative In the latter case, string repetition is performed; a negative
repetition factor yields the empty string. repetition factor yields the empty string.
The \verb|'/'| operator yields the quotient of its arguments. The \verb|"/"| operator yields the quotient of its arguments.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
(Short or long) integer division yields an integer of the same type, (Short or long) integer division yields an integer of the same type,
truncating towards zero. truncating towards zero.
Division by zero raises a {\tt RuntimeError} exception. Division by zero raises a {\tt RuntimeError} exception.
The \verb|'%'| operator yields the remainder from the division The \verb|"%"| operator yields the remainder from the division
of the first argument by the second. of the first argument by the second.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$. The outcome of $x \% y$ is defined as $x - y*trunc(x/y)$.
@ -511,28 +622,28 @@ $3.14 \% 0.7$ equals $0.34$.
\section{Arithmetic expressions} \section{Arithmetic expressions}
\begin{verbatim} \begin{verbatim}
arith_expr: term | arith_expr '+' term | arith_expr '-' term arith_expr: term | arith_expr "+" term | arith_expr "-" term
\end{verbatim} \end{verbatim}
The \verb|'+'| operator yields the sum of its arguments. The \verb|"+"| operator yields the sum of its arguments.
The arguments must either both be numbers, or both strings. The arguments must either both be numbers, or both strings.
In the former case, the numbers are converted to a common type In the former case, the numbers are converted to a common type
and then added together. and then added together.
In the latter case, the strings are concatenated directly, In the latter case, the strings are concatenated directly,
without inserting a space. without inserting a space.
The \verb|'-'| operator yields the difference of its arguments. The \verb|"-"| operator yields the difference of its arguments.
The numeric arguments are first converted to a common type. The numeric arguments are first converted to a common type.
\section{Shift expressions} \section{Shift expressions}
\begin{verbatim} \begin{verbatim}
shift_expr: arith_expr | shift_expr '<<' arith_expr | shift_expr '>>' arith_expr shift_expr: arith_expr | shift_expr "<<" arith_expr | shift_expr ">>" arith_expr
\end{verbatim} \end{verbatim}
These operators accept short integers as arguments only. These operators accept short integers as arguments only.
They shift their left argument to the left or right by the number of bits They shift their left argument to the left or right by the number of bits
given by the right argument. Shifts are ``logical'', e.g., bits shifted given by the right argument. Shifts are ``logical"", e.g., bits shifted
out on one end are lost, and bits shifted in are zero; out on one end are lost, and bits shifted in are zero;
negative numbers are shifted as if they were unsigned in C. negative numbers are shifted as if they were unsigned in C.
Negative shift counts and shift counts greater than {\em or equal to} Negative shift counts and shift counts greater than {\em or equal to}
@ -541,7 +652,7 @@ the word size yield undefined results.
\section{Bitwise AND expressions} \section{Bitwise AND expressions}
\begin{verbatim} \begin{verbatim}
and_expr: shift_expr | and_expr '&' shift_expr and_expr: shift_expr | and_expr "&" shift_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise AND of its arguments, This operator yields the bitwise AND of its arguments,
@ -550,7 +661,7 @@ which must be short integers.
\section{Bitwise XOR expressions} \section{Bitwise XOR expressions}
\begin{verbatim} \begin{verbatim}
xor_expr: and_expr | xor_expr '^' and_expr xor_expr: and_expr | xor_expr "^" and_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise exclusive OR of its arguments, This operator yields the bitwise exclusive OR of its arguments,
@ -559,7 +670,7 @@ which must be short integers.
\section{Bitwise OR expressions} \section{Bitwise OR expressions}
\begin{verbatim} \begin{verbatim}
or_expr: xor_expr | or_expr '|' xor_expr or_expr: xor_expr | or_expr "|" xor_expr
\end{verbatim} \end{verbatim}
This operator yields the bitwise OR of its arguments, This operator yields the bitwise OR of its arguments,
@ -569,7 +680,7 @@ which must be short integers.
\begin{verbatim} \begin{verbatim}
expression: or_expression expression: or_expression
expr_list: expression (',' expression)* [','] expr_list: expression ("," expression)* [","]
\end{verbatim} \end{verbatim}
An expression list containing at least one comma yields a new tuple. An expression list containing at least one comma yields a new tuple.
@ -587,7 +698,7 @@ To create an empty tuple, use an empty pair of parentheses: \verb\()\.
\begin{verbatim} \begin{verbatim}
comparison: expression (comp_operator expression)* comparison: expression (comp_operator expression)*
comp_operator: '<'|'>'|'='|'=='|'>='|'<='|'<>'|'!='|['not'] 'in'|is' ['not'] comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
\end{verbatim} \end{verbatim}
Comparisons yield integer value: 1 for true, 0 for false. Comparisons yield integer value: 1 for true, 0 for false.
@ -605,12 +716,9 @@ $e_{n-1} op_n e_n$, except that each expression is evaluated at most once.
Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison Note that $e_0 op_1 e_1 op_2 e_2$ does not imply any kind of comparison
between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal. between $e_0$ and $e_2$, e.g., $x < y > z$ is perfectly legal.
For the benefit of C programmers, The forms \verb\<>\ and \verb\!=\ are equivalent.
the comparison operators \verb\=\ and \verb\==\ are equivalent,
and so are \verb\<>\ and \verb\!=\.
Use of the C variants is discouraged.
The operators {\tt '<', '>', '=', '>=', '<='}, and {\tt '<>'} compare The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "<>"} compare
the values of two objects. The objects needn't have the same type. the values of two objects. The objects needn't have the same type.
If both are numbers, they are compared to a common type. If both are numbers, they are compared to a common type.
Otherwise, objects of different types {\em always} compare unequal, Otherwise, objects of different types {\em always} compare unequal,
@ -652,9 +760,9 @@ $x {\tt is not} y$ yields the inverse truth value.
\begin{verbatim} \begin{verbatim}
condition: or_test condition: or_test
or_test: and_test | or_test 'or' and_test or_test: and_test | or_test "or" and_test
and_test: not_test | and_test 'and' not_test and_test: not_test | and_test "and" not_test
not_test: comparison | 'not' not_test not_test: comparison | "not" not_test
\end{verbatim} \end{verbatim}
In the context of Boolean operators, and also when conditions are In the context of Boolean operators, and also when conditions are
@ -686,7 +794,7 @@ Several simple statements may occor on a single line separated
by semicolons. The syntax for simple statements is: by semicolons. The syntax for simple statements is:
\begin{verbatim} \begin{verbatim}
stmt_list: simple_stmt (';' simple_stmt)* [';'] stmt_list: simple_stmt (";" simple_stmt)* [";"]
simple_stmt: expression_stmt simple_stmt: expression_stmt
| assignment | assignment
| pass_stmt | pass_stmt
@ -697,6 +805,7 @@ simple_stmt: expression_stmt
| break_stmt | break_stmt
| continue_stmt | continue_stmt
| import_stmt | import_stmt
| global_stmt
\end{verbatim} \end{verbatim}
\section{Expression statements} \section{Expression statements}
@ -718,9 +827,9 @@ do not cause any output.)
\section{Assignments} \section{Assignments}
\begin{verbatim} \begin{verbatim}
assignment: target_list ('=' target_list)* '=' expression_list assignment: target_list ("=" target_list)* "=" expression_list
target_list: target (',' target)* [','] target_list: target ("," target)* [","]
target: identifier | '(' target_list ')' | '[' target_list ']' target: identifier | "(" target_list ")" | "[" target_list "]"
| attributeref | subscription | slicing | attributeref | subscription | slicing
\end{verbatim} \end{verbatim}
@ -835,7 +944,7 @@ messages.)
\section{The {\tt pass} statement} \section{The {\tt pass} statement}
\begin{verbatim} \begin{verbatim}
pass_stmt: 'pass' pass_stmt: "pass"
\end{verbatim} \end{verbatim}
{\tt pass} is a null operation -- when it is executed, {\tt pass} is a null operation -- when it is executed,
@ -844,7 +953,7 @@ nothing happens.
\section{The {\tt del} statement} \section{The {\tt del} statement}
\begin{verbatim} \begin{verbatim}
del_stmt: 'del' target_list del_stmt: "del" target_list
\end{verbatim} \end{verbatim}
Deletion is recursively defined similar to assignment. Deletion is recursively defined similar to assignment.
@ -866,7 +975,7 @@ right type (but even this is determined by the sliced object).
\section{The {\tt print} statement} \section{The {\tt print} statement}
\begin{verbatim} \begin{verbatim}
print_stmt: 'print' [ condition (',' condition)* [','] ] print_stmt: "print" [ condition ("," condition)* [","] ]
\end{verbatim} \end{verbatim}
{\tt print} evaluates each condition in turn and writes the resulting {\tt print} evaluates each condition in turn and writes the resulting
@ -897,7 +1006,7 @@ standard output instead, but this is not safe, and should be fixed.)
\section{The {\tt return} statement} \section{The {\tt return} statement}
\begin{verbatim} \begin{verbatim}
return_stmt: 'return' [condition_list] return_stmt: "return" [condition_list]
\end{verbatim} \end{verbatim}
\verb\return\ may only occur syntactically nested in a function \verb\return\ may only occur syntactically nested in a function
@ -917,7 +1026,7 @@ before really leaving the function.
\section{The {\tt raise} statement} \section{The {\tt raise} statement}
\begin{verbatim} \begin{verbatim}
raise_stmt: 'raise' condition [',' condition] raise_stmt: "raise" condition ["," condition]
\end{verbatim} \end{verbatim}
\verb\raise\ evaluates its first condition, which must yield \verb\raise\ evaluates its first condition, which must yield
@ -930,7 +1039,7 @@ with the second one (or \verb\None\) as its parameter.
\section{The {\tt break} statement} \section{The {\tt break} statement}
\begin{verbatim} \begin{verbatim}
break_stmt: 'break' break_stmt: "break"
\end{verbatim} \end{verbatim}
\verb\break\ may only occur syntactically nested in a \verb\for\ \verb\break\ may only occur syntactically nested in a \verb\for\
@ -949,7 +1058,7 @@ before really leaving the loop.
\section{The {\tt continue} statement} \section{The {\tt continue} statement}
\begin{verbatim} \begin{verbatim}
continue_stmt: 'continue' continue_stmt: "continue"
\end{verbatim} \end{verbatim}
\verb\continue\ may only occur syntactically nested in a \verb\for\ \verb\continue\ may only occur syntactically nested in a \verb\for\
@ -962,9 +1071,17 @@ It continues with the next cycle of the nearest enclosing loop.
\section{The {\tt import} statement} \section{The {\tt import} statement}
\begin{verbatim} \begin{verbatim}
import_stmt: 'import' identifier (',' identifier)* import_stmt: "import" identifier ("," identifier)*
| 'from' identifier 'import' identifier (',' identifier)* | "from" identifier "import" identifier ("," identifier)*
| 'from' identifier 'import' '*' | "from" identifier "import" "*"
\end{verbatim}
(XXX To be done.)
\section{The {\tt global} statement}
\begin{verbatim}
global_stmt: "global" identifier ("," identifier)*
\end{verbatim} \end{verbatim}
(XXX To be done.) (XXX To be done.)
@ -982,48 +1099,49 @@ suite: statement | NEWLINE INDENT statement+ DEDENT
\section{The {\tt if} statement} \section{The {\tt if} statement}
\begin{verbatim} \begin{verbatim}
if_stmt: 'if' condition ':' suite if_stmt: "if" condition ":" suite
('elif' condition ':' suite)* ("elif" condition ":" suite)*
['else' ':' suite] ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt while} statement} \section{The {\tt while} statement}
\begin{verbatim} \begin{verbatim}
while_stmt: 'while' condition ':' suite ['else' ':' suite] while_stmt: "while" condition ":" suite ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt for} statement} \section{The {\tt for} statement}
\begin{verbatim} \begin{verbatim}
for_stmt: 'for' target_list 'in' condition_list ':' suite for_stmt: "for" target_list "in" condition_list ":" suite
['else' ':' suite] ["else" ":" suite]
\end{verbatim} \end{verbatim}
\section{The {\tt try} statement} \section{The {\tt try} statement}
\begin{verbatim} \begin{verbatim}
try_stmt: 'try' ':' suite try_stmt: "try" ":" suite
('except' condition [',' condition] ':' suite)* ("except" condition ["," condition] ":" suite)*
['finally' ':' suite] ["finally" ":" suite]
\end{verbatim} \end{verbatim}
\section{Function definitions} \section{Function definitions}
\begin{verbatim} \begin{verbatim}
funcdef: 'def' identifier '(' [parameter_list] ')' ':' suite funcdef: "def" identifier "(" [parameter_list] ")" ":" suite
parameter_list: parameter (',' parameter)* parameter_list: parameter ("," parameter)*
parameter: identifier | '(' parameter_list ')' parameter: identifier | "(" parameter_list ")"
\end{verbatim} \end{verbatim}
\section{Class definitions} \section{Class definitions}
\begin{verbatim} \begin{verbatim}
classdef: 'class' identifier '(' ')' [inheritance] ':' suite classdef: "class" identifier [inheritance] ":" suite
inheritance: '=' identifier '(' ')' (',' identifier '(' ')')* inheritance: "(" expression ("," expression)* ")"
\end{verbatim} \end{verbatim}
XXX Syntax for scripts, modules XXX Syntax for scripts, modules
XXX Syntax for interactive input, eval, exec, input XXX Syntax for interactive input, eval, exec, input
XXX New definition of expressions (as conditions)
\end{document} \end{document}