Updated markup style (got rid of \verb@...@, mostly).
This commit is contained in:
parent
2094e044c7
commit
5c07d9b028
|
@ -43,20 +43,20 @@ name: lc_letter (lc_letter | "_")*
|
|||
lc_letter: "a"..."z"
|
||||
\end{verbatim}
|
||||
|
||||
The first line says that a \verb@name@ is an \verb@lc_letter@ followed by
|
||||
a sequence of zero or more \verb@lc_letter@s and underscores. An
|
||||
\verb@lc_letter@ in turn is any of the single characters `a' through `z'.
|
||||
(This rule is actually adhered to for the names defined in lexical and
|
||||
grammar rules in this document.)
|
||||
The first line says that a \code{name} is an \code{lc_letter} followed by
|
||||
a sequence of zero or more \code{lc_letter}s and underscores. An
|
||||
\code{lc_letter} in turn is any of the single characters \character{a}
|
||||
through \character{z}. (This rule is actually adhered to for the
|
||||
names defined in lexical and grammar rules in this document.)
|
||||
|
||||
Each rule begins with a name (which is the name defined by the rule)
|
||||
and a colon. A vertical bar (\verb@|@) is used to separate
|
||||
and a colon. A vertical bar (\code{|}) 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
|
||||
phrase enclosed in square brackets (\verb@[ ]@) means zero or one
|
||||
star (\code{*}) means zero or more repetitions of the preceding item;
|
||||
likewise, a plus (\code{+}) means one or more repetitions, and a
|
||||
phrase enclosed in square brackets (\code{[ ]}) means zero or one
|
||||
occurrences (in other words, the enclosed phrase is optional). The
|
||||
\verb@*@ and \verb@+@ operators bind as tightly as possible;
|
||||
\code{*} and \code{+} operators bind as tightly as possible;
|
||||
parentheses are used for grouping. Literal strings are enclosed in
|
||||
quotes. White space is only meaningful to separate tokens.
|
||||
Rules are normally contained on a single line; rules with many
|
||||
|
@ -66,11 +66,11 @@ first beginning with a vertical bar.
|
|||
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
|
||||
characters. A phrase between angular brackets (\code{<...>}) gives an
|
||||
informal description of the symbol defined; e.g. this could be used
|
||||
to describe the notion of `control character' if needed.
|
||||
\index{lexical definitions}
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
|
||||
Even though the notation used is almost the same, there is a big
|
||||
difference between the meaning of lexical and syntactic definitions:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
\chapter{Lexical analysis}
|
||||
|
||||
A Python program is read by a {\em parser}. Input to the parser is a
|
||||
stream of {\em tokens}, generated by the {\em lexical analyzer}. This
|
||||
A Python program is read by a \emph{parser}. Input to the parser is a
|
||||
stream of \emph{tokens}, generated by the \emph{lexical analyzer}. This
|
||||
chapter describes how the lexical analyzer breaks a file into tokens.
|
||||
\index{lexical analysis}
|
||||
\index{parser}
|
||||
|
@ -19,7 +19,7 @@ syntax (e.g. between statements in compound statements).
|
|||
|
||||
\subsection{Comments}
|
||||
|
||||
A comment starts with a hash character (\verb@#@) that is not part of
|
||||
A comment starts with a hash character (\code{\#}) that is not part of
|
||||
a string literal, and ends at the end of the physical line. A comment
|
||||
always signifies the end of the logical line. Comments are ignored by
|
||||
the syntax.
|
||||
|
@ -31,7 +31,7 @@ the syntax.
|
|||
\subsection{Explicit line joining}
|
||||
|
||||
Two or more physical lines may be joined into logical lines using
|
||||
backslash characters (\verb/\/), as follows: when a physical line ends
|
||||
backslash characters (\code{\e}), as follows: when a physical line ends
|
||||
in a backslash that is not part of a string literal or comment, it is
|
||||
joined with the following forming a single logical line, deleting the
|
||||
backslash and the following end-of-line character. For example:
|
||||
|
@ -91,7 +91,7 @@ turn is used to determine the grouping of statements.
|
|||
|
||||
First, tabs are replaced (from left to right) by one to eight spaces
|
||||
such that the total number of characters up to there is a multiple of
|
||||
eight (this is intended to be the same rule as used by {\UNIX}). The
|
||||
eight (this is intended to be the same rule as used by \UNIX{}). The
|
||||
total number of spaces preceding the first non-blank character then
|
||||
determines the line's indentation. Indentation cannot be split over
|
||||
multiple physical lines using backslashes.
|
||||
|
@ -107,7 +107,7 @@ the stack will always be strictly increasing from bottom to top. At
|
|||
the beginning of each logical line, the line's indentation level is
|
||||
compared to the top of the stack. If it is equal, nothing happens.
|
||||
If it is larger, it is pushed on the stack, and one INDENT token is
|
||||
generated. If it is smaller, it {\em must} be one of the numbers
|
||||
generated. If it is smaller, it \emph{must} be one of the numbers
|
||||
occurring on the stack; all numbers on the stack that are larger are
|
||||
popped off, and for each number popped off a DEDENT token is
|
||||
generated. At the end of the file, a DEDENT token is generated for
|
||||
|
@ -145,7 +145,7 @@ The following example shows various indentation errors:
|
|||
|
||||
(Actually, the first three errors are detected by the parser; only the
|
||||
last error is found by the lexical analyzer --- the indentation of
|
||||
\verb@return r@ does not match a level popped off the stack.)
|
||||
\code{return r} does not match a level popped off the stack.)
|
||||
|
||||
\section{Other tokens}
|
||||
|
||||
|
@ -174,10 +174,10 @@ Identifiers are unlimited in length. Case is significant.
|
|||
|
||||
\subsection{Keywords}
|
||||
|
||||
The following identifiers are used as reserved words, or {\em
|
||||
keywords} of the language, and cannot be used as ordinary
|
||||
identifiers. They must be spelled exactly as written here:
|
||||
\index{keyword}
|
||||
The following identifiers are used as reserved words, or
|
||||
\emph{keywords} of the language, and cannot be used as ordinary
|
||||
identifiers. They must be spelled exactly as written here:%
|
||||
\index{keyword}%
|
||||
\index{reserved word}
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -212,13 +212,13 @@ shortstringchar: <any ASCII character except "\" or newline or the quote>
|
|||
longstringchar: <any ASCII character except "\">
|
||||
escapeseq: "\" <any ASCII character>
|
||||
\end{verbatim}
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
|
||||
In ``long strings'' (strings surrounded by sets of three quotes),
|
||||
unescaped newlines and quotes are allowed (and are retained), except
|
||||
that three unescaped quotes in a row terminate the string. (A
|
||||
``quote'' is the character used to open the string, i.e. either
|
||||
\verb/'/ or \verb/"/.)
|
||||
\code{'} or \code{"}.)
|
||||
|
||||
Escape sequences in strings are interpreted according to rules similar
|
||||
to those used by Standard C. The recognized escape sequences are:
|
||||
|
@ -230,32 +230,32 @@ to those used by Standard C. The recognized escape sequences are:
|
|||
\begin{center}
|
||||
\begin{tabular}{|l|l|}
|
||||
\hline
|
||||
\verb/\/{\em newline} & Ignored \\
|
||||
\verb/\\/ & Backslash (\verb/\/) \\
|
||||
\verb/\'/ & Single quote (\verb/'/) \\
|
||||
\verb/\"/ & Double quote (\verb/"/) \\
|
||||
\verb/\a/ & \ASCII{} Bell (BEL) \\
|
||||
\verb/\b/ & \ASCII{} Backspace (BS) \\
|
||||
%\verb/\E/ & \ASCII{} Escape (ESC) \\
|
||||
\verb/\f/ & \ASCII{} Formfeed (FF) \\
|
||||
\verb/\n/ & \ASCII{} Linefeed (LF) \\
|
||||
\verb/\r/ & \ASCII{} Carriage Return (CR) \\
|
||||
\verb/\t/ & \ASCII{} Horizontal Tab (TAB) \\
|
||||
\verb/\v/ & \ASCII{} Vertical Tab (VT) \\
|
||||
\verb/\/{\em ooo} & \ASCII{} character with octal value {\em ooo} \\
|
||||
\verb/\x/{\em xx...} & \ASCII{} character with hex value {\em xx...} \\
|
||||
\code{\e}\emph{newline} & Ignored \\
|
||||
\code{\e\e} & Backslash (\code{\e}) \\
|
||||
\code{\e'} & Single quote (\code{'}) \\
|
||||
\code{\e"} & Double quote (\code{"}) \\
|
||||
\code{\e a} & \ASCII{} Bell (BEL) \\
|
||||
\code{\e b} & \ASCII{} Backspace (BS) \\
|
||||
%\code{\e E} & \ASCII{} Escape (ESC) \\
|
||||
\code{\e f} & \ASCII{} Formfeed (FF) \\
|
||||
\code{\e n} & \ASCII{} Linefeed (LF) \\
|
||||
\code{\e r} & \ASCII{} Carriage Return (CR) \\
|
||||
\code{\e t} & \ASCII{} Horizontal Tab (TAB) \\
|
||||
\code{\e v} & \ASCII{} Vertical Tab (VT) \\
|
||||
\code{\e}\emph{ooo} & \ASCII{} character with octal value \emph{ooo} \\
|
||||
\code{\e x}\emph{xx...} & \ASCII{} character with hex value \emph{xx...} \\
|
||||
\hline
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
|
||||
In strict compatibility with Standard C, up to three octal digits are
|
||||
In strict compatibility with Standard \C, up to three octal digits are
|
||||
accepted, but an unlimited number of hex digits is taken to be part of
|
||||
the hex escape (and then the lower 8 bits of the resulting hex number
|
||||
are used in all current implementations...).
|
||||
|
||||
All unrecognized escape sequences are left in the string unchanged,
|
||||
i.e., {\em the backslash is left in the string.} (This behavior is
|
||||
i.e., \emph{the backslash is left in the string.} (This behavior is
|
||||
useful when debugging: if an escape sequence is mistyped, the
|
||||
resulting output is more easily recognized as broken. It also helps a
|
||||
great deal for string literals used as regular expressions or
|
||||
|
@ -331,8 +331,8 @@ Some examples of floating point literals:
|
|||
\end{verbatim}
|
||||
|
||||
Note that numeric literals do not include a sign; a phrase like
|
||||
\verb@-1@ is actually an expression composed of the operator
|
||||
\verb@-@ and the literal \verb@1@.
|
||||
\code{-1} is actually an expression composed of the operator
|
||||
\code{-} and the literal \code{1}.
|
||||
|
||||
\section{Operators}
|
||||
|
||||
|
@ -345,7 +345,7 @@ The following tokens are operators:
|
|||
< == > <= <> != >=
|
||||
\end{verbatim}
|
||||
|
||||
The comparison operators \verb@<>@ and \verb@!=@ are alternate
|
||||
The comparison operators \code{<>} and \code{!=} are alternate
|
||||
spellings of the same operator.
|
||||
|
||||
\section{Delimiters}
|
||||
|
@ -363,7 +363,7 @@ meaning:
|
|||
The following printing \ASCII{} characters are not used in Python. Their
|
||||
occurrence outside string literals and comments is an unconditional
|
||||
error:
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
|
||||
\begin{verbatim}
|
||||
@ $ ?
|
||||
|
|
|
@ -220,14 +220,14 @@ read from a file.
|
|||
\obindex{string}
|
||||
\index{character}
|
||||
\index{byte}
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
|
||||
(On systems whose native character set is not \ASCII{}, strings may use
|
||||
EBCDIC in their internal representation, provided the functions
|
||||
\function{chr()} and \function{ord()} implement a mapping between \ASCII{} and
|
||||
EBCDIC, and string comparison preserves the \ASCII{} order.
|
||||
Or perhaps someone can propose a better rule?)
|
||||
\index{ASCII}
|
||||
\index{ASCII@\ASCII{}}
|
||||
\index{EBCDIC}
|
||||
\index{character set}
|
||||
\indexii{string}{comparison}
|
||||
|
|
160
Doc/ref/ref5.tex
160
Doc/ref/ref5.tex
|
@ -2,8 +2,8 @@
|
|||
\index{expression}
|
||||
\index{condition}
|
||||
|
||||
{\bf Note:} In this and the following chapters, extended BNF notation
|
||||
will be used to describe syntax, not lexical analysis.
|
||||
\strong{Note:} In this and the following chapters, extended BNF
|
||||
notation will be used to describe syntax, not lexical analysis.
|
||||
\index{BNF}
|
||||
|
||||
This chapter explains the meaning of the elements of expressions and
|
||||
|
@ -12,14 +12,14 @@ may be used wherever an expression is required by enclosing it in
|
|||
parentheses. The only places where expressions are used in the syntax
|
||||
instead of conditions is in expression statements and on the
|
||||
right-hand side of assignment statements; this catches some nasty bugs
|
||||
like accidentally writing \verb@x == 1@ instead of \verb@x = 1@.
|
||||
like accidentally writing \code{x == 1} instead of \code{x = 1}.
|
||||
\indexii{assignment}{statement}
|
||||
|
||||
The comma plays several roles in Python's syntax. It is usually an
|
||||
operator with a lower precedence than all others, but occasionally
|
||||
serves other purposes as well; e.g. it separates function arguments,
|
||||
is used in list and dictionary constructors, and has special semantics
|
||||
in \verb@print@ statements.
|
||||
in \keyword{print} statements.
|
||||
\index{comma}
|
||||
|
||||
When (one alternative of) a syntax rule has the form
|
||||
|
@ -28,8 +28,8 @@ When (one alternative of) a syntax rule has the form
|
|||
name: othername
|
||||
\end{verbatim}
|
||||
|
||||
and no semantics are given, the semantics of this form of \verb@name@
|
||||
are the same as for \verb@othername@.
|
||||
and no semantics are given, the semantics of this form of \code{name}
|
||||
are the same as for \code{othername}.
|
||||
\index{syntax}
|
||||
|
||||
\section{Arithmetic conversions}
|
||||
|
@ -38,7 +38,7 @@ are the same as for \verb@othername@.
|
|||
When a description of an arithmetic operator below uses the phrase
|
||||
``the numeric arguments are converted to a common type'',
|
||||
this both means that if either argument is not a number, a
|
||||
\verb@TypeError@ exception is raised, and that otherwise
|
||||
\exception{TypeError} exception is raised, and that otherwise
|
||||
the following conversions are applied:
|
||||
\exindex{TypeError}
|
||||
\indexii{floating point}{number}
|
||||
|
@ -73,10 +73,10 @@ enclosure: parenth_form|list_display|dict_display|string_conversion
|
|||
An identifier occurring as an atom is a reference to a local, global
|
||||
or built-in name binding. If a name is assigned to anywhere in a code
|
||||
block (even in unreachable code), and is not mentioned in a
|
||||
\verb@global@ statement in that code block, then it refers to a local
|
||||
\keyword{global} statement in that code block, then it refers to a local
|
||||
name throughout that code block. When it is not assigned to anywhere
|
||||
in the block, or when it is assigned to but also explicitly listed in
|
||||
a \verb@global@ statement, it refers to a global name if one exists,
|
||||
a \keyword{global} statement, it refers to a global name if one exists,
|
||||
else to a built-in name (and this binding may dynamically change).
|
||||
\indexii{name}{binding}
|
||||
\index{code block}
|
||||
|
@ -86,7 +86,7 @@ else to a built-in name (and this binding may dynamically change).
|
|||
|
||||
When the name is bound to an object, evaluation of the atom yields
|
||||
that object. When a name is not bound, an attempt to evaluate it
|
||||
raises a \verb@NameError@ exception.
|
||||
raises a \exception{NameError} exception.
|
||||
\exindex{NameError}
|
||||
|
||||
\subsection{Literals}
|
||||
|
@ -202,10 +202,10 @@ A string conversion evaluates the contained condition list and
|
|||
converts the resulting object into a string according to rules
|
||||
specific to its type.
|
||||
|
||||
If the object is a string, a number, \verb@None@, or a tuple, list or
|
||||
If the object is a string, a number, \code{None}, or a tuple, list or
|
||||
dictionary containing only objects whose type is one of these, the
|
||||
resulting string is a valid Python expression which can be passed to
|
||||
the built-in function \verb@eval()@ to yield an expression with the
|
||||
the built-in function \function{eval()} to yield an expression with the
|
||||
same value (or an approximation, if floating point numbers are
|
||||
involved).
|
||||
|
||||
|
@ -217,9 +217,9 @@ dictionaries that contain a reference to themselves, directly or
|
|||
indirectly.)
|
||||
\obindex{recursive}
|
||||
|
||||
The built-in function \verb@repr()@ performs exactly the same
|
||||
The built-in function \function{repr()} performs exactly the same
|
||||
conversion in its argument as enclosing it it reverse quotes does.
|
||||
The built-in function \verb@str()@ performs a similar but more
|
||||
The built-in function \function{str()} performs a similar but more
|
||||
user-friendly conversion.
|
||||
\bifuncindex{repr}
|
||||
\bifuncindex{str}
|
||||
|
@ -246,10 +246,11 @@ attributeref: primary "." identifier
|
|||
The primary must evaluate to an object of a type that supports
|
||||
attribute references, e.g. a module or a list. This object is then
|
||||
asked to produce the attribute whose name is the identifier. If this
|
||||
attribute is not available, the exception \verb@AttributeError@ is
|
||||
raised. Otherwise, the type and value of the object produced is
|
||||
determined by the object. Multiple evaluations of the same attribute
|
||||
reference may yield different objects.
|
||||
attribute is not available, the exception
|
||||
\exception{AttributeError}\exindex{AttributeError} is raised.
|
||||
Otherwise, the type and value of the object produced is determined by
|
||||
the object. Multiple evaluations of the same attribute reference may
|
||||
yield different objects.
|
||||
\obindex{module}
|
||||
\obindex{list}
|
||||
|
||||
|
@ -278,7 +279,7 @@ the value in the mapping that corresponds to that key.
|
|||
|
||||
If it is a sequence, the condition must evaluate to a plain integer.
|
||||
If this value is negative, the length of the sequence is added to it
|
||||
(so that, e.g. \verb@x[-1]@ selects the last item of \verb@x@.)
|
||||
(so that, e.g. \code{x[-1]} selects the last item of \code{x}.)
|
||||
The resulting value must be a nonnegative integer smaller than the
|
||||
number of items in the sequence, and the subscription selects the item
|
||||
whose index is that value (counting from zero).
|
||||
|
@ -332,7 +333,7 @@ objects, and methods of class instances are callable). If it is a
|
|||
class, the argument list must be empty; otherwise, the arguments are
|
||||
evaluated.
|
||||
|
||||
A call always returns some value, possibly \verb@None@, unless it
|
||||
A call always returns some value, possibly \code{None}, unless it
|
||||
raises an exception. How this value is computed depends on the type
|
||||
of the callable object. If it is:
|
||||
|
||||
|
@ -342,7 +343,7 @@ of the callable object. If it is:
|
|||
executed, passing it the argument list. The first thing the code
|
||||
block will do is bind the formal parameters to the arguments; this is
|
||||
described in section \ref{function}. When the code block executes a
|
||||
\verb@return@ statement, this specifies the return value of the
|
||||
\keyword{return} statement, this specifies the return value of the
|
||||
function call.
|
||||
\indexii{function}{call}
|
||||
\indexiii{user-defined}{function}{call}
|
||||
|
@ -385,22 +386,22 @@ All unary arithmetic (and bit-wise) operations have the same priority:
|
|||
u_expr: primary | "-" u_expr | "+" u_expr | "~" u_expr
|
||||
\end{verbatim}
|
||||
|
||||
The unary \verb@"-"@ (minus) operator yields the negation of its
|
||||
The unary \code{-} (minus) operator yields the negation of its
|
||||
numeric argument.
|
||||
\index{negation}
|
||||
\index{minus}
|
||||
|
||||
The unary \verb@"+"@ (plus) operator yields its numeric argument
|
||||
The unary \code{+} (plus) operator yields its numeric argument
|
||||
unchanged.
|
||||
\index{plus}
|
||||
|
||||
The unary \verb@"~"@ (invert) operator yields the bit-wise inversion
|
||||
The unary \code{~} (invert) operator yields the bit-wise inversion
|
||||
of its plain or long integer argument. The bit-wise inversion of
|
||||
\verb@x@ is defined as \verb@-(x+1)@.
|
||||
\code{x} is defined as \code{-(x+1)}.
|
||||
\index{inversion}
|
||||
|
||||
In all three cases, if the argument does not have the proper type,
|
||||
a \verb@TypeError@ exception is raised.
|
||||
a \exception{TypeError} exception is raised.
|
||||
\exindex{TypeError}
|
||||
|
||||
\section{Binary arithmetic operations}
|
||||
|
@ -418,7 +419,7 @@ m_expr: u_expr | m_expr "*" u_expr
|
|||
a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr
|
||||
\end{verbatim}
|
||||
|
||||
The \verb@"*"@ (multiplication) operator yields the product of its
|
||||
The \code{*} (multiplication) operator yields the product of its
|
||||
arguments. The arguments must either both be numbers, or one argument
|
||||
must be a plain integer and the other must be a sequence. In the
|
||||
former case, the numbers are converted to a common type and then
|
||||
|
@ -426,40 +427,40 @@ multiplied together. In the latter case, sequence repetition is
|
|||
performed; a negative repetition factor yields an empty sequence.
|
||||
\index{multiplication}
|
||||
|
||||
The \verb@"/"@ (division) operator yields the quotient of its
|
||||
The \code{/} (division) operator yields the quotient of its
|
||||
arguments. The numeric arguments are first converted to a common
|
||||
type. Plain or long integer division yields an integer of the same
|
||||
type; the result is that of mathematical division with the `floor'
|
||||
function applied to the result. Division by zero raises the
|
||||
\verb@ZeroDivisionError@ exception.
|
||||
\exception{ZeroDivisionError} exception.
|
||||
\exindex{ZeroDivisionError}
|
||||
\index{division}
|
||||
|
||||
The \verb@"%"@ (modulo) operator yields the remainder from the
|
||||
The \code{\%} (modulo) operator yields the remainder from the
|
||||
division of the first argument by the second. The numeric arguments
|
||||
are first converted to a common type. A zero right argument raises
|
||||
the \verb@ZeroDivisionError@ exception. The arguments may be floating
|
||||
point numbers, e.g. \verb@3.14 % 0.7@ equals \verb@0.34@. The modulo
|
||||
the \exception{ZeroDivisionError} exception. The arguments may be floating
|
||||
point numbers, e.g. \code{3.14 \% 0.7} equals \code{0.34}. The modulo
|
||||
operator always yields a result with the same sign as its second
|
||||
operand (or zero); the absolute value of the result is strictly
|
||||
smaller than the second operand.
|
||||
\index{modulo}
|
||||
|
||||
The integer division and modulo operators are connected by the
|
||||
following identity: \verb@x == (x/y)*y + (x%y)@. Integer division and
|
||||
modulo are also connected with the built-in function \verb@divmod()@:
|
||||
\verb@divmod(x, y) == (x/y, x%y)@. These identities don't hold for
|
||||
following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and
|
||||
modulo are also connected with the built-in function \function{divmod()}:
|
||||
\code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for
|
||||
floating point numbers; there a similar identity holds where
|
||||
\verb@x/y@ is replaced by \verb@floor(x/y)@).
|
||||
\code{x/y} is replaced by \code{floor(x/y)}).
|
||||
|
||||
The \verb@"+"@ (addition) operator yields the sum of its arguments.
|
||||
The \code{+} (addition) operator yields the sum of its arguments.
|
||||
The arguments must either both be numbers, or both sequences of the
|
||||
same type. In the former case, the numbers are converted to a common
|
||||
type and then added together. In the latter case, the sequences are
|
||||
concatenated.
|
||||
\index{addition}
|
||||
|
||||
The \verb@"-"@ (subtraction) operator yields the difference of its
|
||||
The \code{-} (subtraction) operator yields the difference of its
|
||||
arguments. The numeric arguments are first converted to a common
|
||||
type.
|
||||
\index{subtraction}
|
||||
|
@ -485,7 +486,7 @@ multiplication with \code{pow(2,\var{n})}; for plain integers there is
|
|||
no overflow check so this drops bits and flips the sign if the result
|
||||
is not less than \code{pow(2,31)} in absolute value.
|
||||
|
||||
Negative shift counts raise a \verb@ValueError@ exception.
|
||||
Negative shift counts raise a \exception{ValueError} exception.
|
||||
\exindex{ValueError}
|
||||
|
||||
\section{Binary bit-wise operations}
|
||||
|
@ -499,18 +500,18 @@ xor_expr: and_expr | xor_expr "^" and_expr
|
|||
or_expr: xor_expr | or_expr "|" xor_expr
|
||||
\end{verbatim}
|
||||
|
||||
The \verb@"&"@ operator yields the bitwise AND of its arguments, which
|
||||
The \code{\&} operator yields the bitwise AND of its arguments, which
|
||||
must be plain or long integers. The arguments are converted to a
|
||||
common type.
|
||||
\indexii{bit-wise}{and}
|
||||
|
||||
The \verb@"^"@ operator yields the bitwise XOR (exclusive OR) of its
|
||||
The \code{\^} operator yields the bitwise XOR (exclusive OR) of its
|
||||
arguments, which must be plain or long integers. The arguments are
|
||||
converted to a common type.
|
||||
\indexii{bit-wise}{xor}
|
||||
\indexii{exclusive}{or}
|
||||
|
||||
The \verb@"|"@ operator yields the bitwise (inclusive) OR of its
|
||||
The \code{|} operator yields the bitwise (inclusive) OR of its
|
||||
arguments, which must be plain or long integers. The arguments are
|
||||
converted to a common type.
|
||||
\indexii{bit-wise}{or}
|
||||
|
@ -519,19 +520,19 @@ converted to a common type.
|
|||
\section{Comparisons}
|
||||
\index{comparison}
|
||||
|
||||
Contrary to C, all comparison operations in Python have the same
|
||||
Contrary to \C, all comparison operations in Python have the same
|
||||
priority, which is lower than that of any arithmetic, shifting or
|
||||
bitwise operation. Also contrary to C, expressions like
|
||||
\verb@a < b < c@ have the interpretation that is conventional in
|
||||
bitwise operation. Also contrary to \C, expressions like
|
||||
\code{a < b < c} have the interpretation that is conventional in
|
||||
mathematics:
|
||||
\index{C}
|
||||
\indexii{C}{language}
|
||||
|
||||
\begin{verbatim}
|
||||
comparison: or_expr (comp_operator or_expr)*
|
||||
comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
|
||||
\end{verbatim}
|
||||
|
||||
Comparisons yield integer values: 1 for true, 0 for false.
|
||||
Comparisons yield integer values: \code{1} for true, \code{0} for false.
|
||||
|
||||
Comparisons can be chained arbitrarily, e.g. \code{x < y <= z} is
|
||||
equivalent to \code{x < y and y <= z}, except that \code{y} is
|
||||
|
@ -542,16 +543,16 @@ when \code{x < y} is found to be false).
|
|||
Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are
|
||||
expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison
|
||||
operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent
|
||||
to \var{a opa b} \code{and} \var{b opb c} \code{and} \ldots \code{and}
|
||||
to \var{a opa b} \keyword{and} \var{b opb c} \keyword{and} \ldots \keyword{and}
|
||||
\var{y opy z}, except that each expression is evaluated at most once.
|
||||
|
||||
Note that \var{a opa b opb c} doesn't imply any kind of comparison
|
||||
between \var{a} and \var{c}, so that e.g.\ \code{x < y > z} is
|
||||
perfectly legal (though perhaps not pretty).
|
||||
|
||||
The forms \verb@<>@ and \verb@!=@ are equivalent; for consistency with
|
||||
C, \verb@!=@ is preferred; where \verb@!=@ is mentioned below
|
||||
\verb@<>@ is also implied.
|
||||
The forms \code{<>} and \code{!=} are equivalent; for consistency with
|
||||
C, \code{!=} is preferred; where \code{!=} is mentioned below
|
||||
\code{<>} is also implied.
|
||||
|
||||
The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare
|
||||
the values of two objects. The objects needn't have the same type.
|
||||
|
@ -560,8 +561,8 @@ objects of different types {\em always} compare unequal, and are
|
|||
ordered consistently but arbitrarily.
|
||||
|
||||
(This unusual definition of comparison is done to simplify the
|
||||
definition of operations like sorting and the \verb@in@ and
|
||||
\verb@not@ \verb@in@ operators.)
|
||||
definition of operations like sorting and the \keyword{in} and
|
||||
\keyword{not in} operators.)
|
||||
|
||||
Comparison of objects of the same type depends on the type:
|
||||
|
||||
|
@ -572,7 +573,8 @@ Numbers are compared arithmetically.
|
|||
|
||||
\item
|
||||
Strings are compared lexicographically using the numeric equivalents
|
||||
(the result of the built-in function \verb@ord@) of their characters.
|
||||
(the result of the built-in function \function{ord()}) of their
|
||||
characters.
|
||||
|
||||
\item
|
||||
Tuples and lists are compared lexicographically using comparison of
|
||||
|
@ -585,7 +587,7 @@ comparison of their sorted (key, value) lists.%
|
|||
but about the only sensible definition. An earlier version of Python
|
||||
compared dictionaries by identity only, but this caused surprises
|
||||
because people expected to be able to test a dictionary for emptiness
|
||||
by comparing it to {\tt \{\}}.}
|
||||
by comparing it to \code{\{\}}.}
|
||||
|
||||
\item
|
||||
Most other types compare unequal unless they are the same object;
|
||||
|
@ -595,12 +597,12 @@ execution of a program.
|
|||
|
||||
\end{itemize}
|
||||
|
||||
The operators \verb@in@ and \verb@not in@ test for sequence
|
||||
The operators \keyword{in} and \keyword{not in} test for sequence
|
||||
membership: if \var{y} is a sequence, \code{\var{x} in \var{y}} is
|
||||
true if and only if there exists an index \var{i} such that
|
||||
\code{\var{x} = \var{y}[\var{i}]}.
|
||||
\code{\var{x} not in \var{y}} yields the inverse truth value. The
|
||||
exception \verb@TypeError@ is raised when \var{y} is not a sequence,
|
||||
exception \exception{TypeError} is raised when \var{y} is not a sequence,
|
||||
or when \var{y} is a string and \var{x} is not a string of length one.%
|
||||
\footnote{The latter restriction is sometimes a nuisance.}
|
||||
\opindex{in}
|
||||
|
@ -608,9 +610,9 @@ or when \var{y} is a string and \var{x} is not a string of length one.%
|
|||
\indexii{membership}{test}
|
||||
\obindex{sequence}
|
||||
|
||||
The operators \verb@is@ and \verb@is not@ test for object identity:
|
||||
\var{x} \code{is} \var{y} is true if and only if \var{x} and \var{y}
|
||||
are the same object. \var{x} \code{is not} \var{y} yields the inverse
|
||||
The operators \keyword{is} and \keyword{is not} test for object identity:
|
||||
\code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y}
|
||||
are the same object. \code{\var{x} is not \var{y}} yields the inverse
|
||||
truth value.
|
||||
\opindex{is}
|
||||
\opindex{is not}
|
||||
|
@ -631,38 +633,40 @@ lambda_form: "lambda" [parameter_list]: condition
|
|||
|
||||
In the context of Boolean operations, and also when conditions are
|
||||
used by control flow statements, the following values are interpreted
|
||||
as false: \verb@None@, numeric zero of all types, empty sequences
|
||||
as false: \code{None}, numeric zero of all types, empty sequences
|
||||
(strings, tuples and lists), and empty mappings (dictionaries). All
|
||||
other values are interpreted as true.
|
||||
|
||||
The operator \verb@not@ yields 1 if its argument is false, 0 otherwise.
|
||||
The operator \keyword{not} yields \code{1} if its argument is false,
|
||||
\code{0} otherwise.
|
||||
\opindex{not}
|
||||
|
||||
The condition \var{x} \verb@and@ \var{y} first evaluates \var{x}; if
|
||||
The condition \code{\var{x} and \var{y}} first evaluates \var{x}; if
|
||||
\var{x} is false, its value is returned; otherwise, \var{y} is
|
||||
evaluated and the resulting value is returned.
|
||||
\opindex{and}
|
||||
|
||||
The condition \var{x} \verb@or@ \var{y} first evaluates \var{x}; if
|
||||
The condition \code{\var{x} or \var{y}} first evaluates \var{x}; if
|
||||
\var{x} is true, its value is returned; otherwise, \var{y} is
|
||||
evaluated and the resulting value is returned.
|
||||
\opindex{or}
|
||||
|
||||
(Note that \verb@and@ and \verb@or@ do not restrict the value and type
|
||||
they return to 0 and 1, but rather return the last evaluated argument.
|
||||
This is sometimes useful, e.g. if \verb@s@ is a string that should be
|
||||
(Note that \keyword{and} and \keyword{or} do not restrict the value
|
||||
and type they return to \code{0} and \code{1}, but rather return the
|
||||
last evaluated argument.
|
||||
This is sometimes useful, e.g.\ if \code{s} is a string that should be
|
||||
replaced by a default value if it is empty, the expression
|
||||
\verb@s or 'foo'@ yields the desired value. Because \verb@not@ has to
|
||||
\code{s or 'foo'} yields the desired value. Because \keyword{not} has to
|
||||
invent a value anyway, it does not bother to return a value of the
|
||||
same type as its argument, so e.g. \verb@not 'foo'@ yields \verb@0@,
|
||||
not \verb@''@.)
|
||||
same type as its argument, so e.g. \code{not 'foo'} yields \code{0},
|
||||
not \code{''}.)
|
||||
|
||||
Lambda forms (lambda expressions) have the same syntactic position as
|
||||
conditions. They are a shorthand to create anonymous functions; the
|
||||
expression {\em {\tt lambda} arguments{\tt :} condition}
|
||||
expression \code{lambda \var{arguments}: \var{condition}}
|
||||
yields a function object that behaves virtually identical to one
|
||||
defined with
|
||||
{\em {\tt def} name {\tt (}arguments{\tt ): return} condition}.
|
||||
\code{def \var{name}(\var{arguments}): return \var{condition}}.
|
||||
See section \ref{function} for the syntax of
|
||||
parameter lists. Note that functions created with lambda forms cannot
|
||||
contain statements.
|
||||
|
@ -705,7 +709,7 @@ tuple, but rather yields the value of that expression (condition).
|
|||
\indexii{trailing}{comma}
|
||||
|
||||
(To create an empty tuple, use an empty pair of parentheses:
|
||||
\verb@()@.)
|
||||
\code{()}.)
|
||||
|
||||
\section{Summary}
|
||||
|
||||
|
@ -719,14 +723,14 @@ chain from left to right --- see above).
|
|||
\begin{center}
|
||||
\begin{tabular}{|c|c|}
|
||||
\hline
|
||||
\code{or} & Boolean OR \\
|
||||
\keyword{or} & Boolean OR \\
|
||||
\hline
|
||||
\code{and} & Boolean AND \\
|
||||
\keyword{and} & Boolean AND \\
|
||||
\hline
|
||||
\code{not} \var{x} & Boolean NOT \\
|
||||
\keyword{not} \var{x} & Boolean NOT \\
|
||||
\hline
|
||||
\code{in}, \code{not} \code{in} & Membership tests \\
|
||||
\code{is}, \code{is} \code{not} & Identity tests \\
|
||||
\keyword{in}, \keyword{not} \keyword{in} & Membership tests \\
|
||||
\keyword{is}, \keyword{is not} & Identity tests \\
|
||||
\code{<}, \code{<=}, \code{>}, \code{>=}, \code{<>}, \code{!=}, \code{=} &
|
||||
Comparisons \\
|
||||
\hline
|
||||
|
|
124
Doc/ref/ref7.tex
124
Doc/ref/ref7.tex
|
@ -6,8 +6,8 @@ or control the execution of those other statements in some way. In
|
|||
general, compound statements span multiple lines, although in simple
|
||||
incarnations a whole compound statement may be contained in one line.
|
||||
|
||||
The \verb@if@, \verb@while@ and \verb@for@ statements implement
|
||||
traditional control flow constructs. \verb@try@ specifies exception
|
||||
The \keyword{if}, \keyword{while} and \keyword{for} statements implement
|
||||
traditional control flow constructs. \keyword{try} specifies exception
|
||||
handlers and/or cleanup code for a group of statements. Function and
|
||||
class definitions are also syntactically compound statements.
|
||||
|
||||
|
@ -21,7 +21,7 @@ statements on the same line as the header, following the header's
|
|||
colon, or it can be one or more indented statements on subsequent
|
||||
lines. Only the latter form of suite can contain nested compound
|
||||
statements; the following is illegal, mostly because it wouldn't be
|
||||
clear to which \verb@if@ clause a following \verb@else@ clause would
|
||||
clear to which \keyword{if} clause a following \keyword{else} clause would
|
||||
belong:
|
||||
\index{clause}
|
||||
\index{suite}
|
||||
|
@ -32,7 +32,7 @@ if test1: if test2: print x
|
|||
|
||||
Also note that the semicolon binds tighter than the colon in this
|
||||
context, so that in the following example, either all or none of the
|
||||
\verb@print@ statements are executed:
|
||||
\keyword{print} statements are executed:
|
||||
|
||||
\begin{verbatim}
|
||||
if x < y < z: print x; print y; print z
|
||||
|
@ -48,24 +48,24 @@ statement: stmt_list NEWLINE | compound_stmt
|
|||
stmt_list: simple_stmt (";" simple_stmt)* [";"]
|
||||
\end{verbatim}
|
||||
|
||||
Note that statements always end in a \verb@NEWLINE@ possibly followed
|
||||
by a \verb@DEDENT@.
|
||||
Note that statements always end in a \code{NEWLINE} possibly followed
|
||||
by a \code{DEDENT}.
|
||||
\index{NEWLINE token}
|
||||
\index{DEDENT token}
|
||||
|
||||
Also note that optional continuation clauses always begin with a
|
||||
keyword that cannot start a statement, thus there are no ambiguities
|
||||
(the `dangling \verb@else@' problem is solved in Python by requiring
|
||||
nested \verb@if@ statements to be indented).
|
||||
(the `dangling \keyword{else}' problem is solved in Python by requiring
|
||||
nested \keyword{if} statements to be indented).
|
||||
\indexii{dangling}{else}
|
||||
|
||||
The formatting of the grammar rules in the following sections places
|
||||
each clause on a separate line for clarity.
|
||||
|
||||
\section{The {\tt if} statement}
|
||||
\section{The \keyword{if} statement}
|
||||
\stindex{if}
|
||||
|
||||
The \verb@if@ statement is used for conditional execution:
|
||||
The \keyword{if} statement is used for conditional execution:
|
||||
|
||||
\begin{verbatim}
|
||||
if_stmt: "if" condition ":" suite
|
||||
|
@ -76,17 +76,17 @@ if_stmt: "if" condition ":" suite
|
|||
It selects exactly one of the suites by evaluating the conditions one
|
||||
by one until one is found to be true (see section \ref{Booleans} for
|
||||
the definition of true and false); then that suite is executed (and no
|
||||
other part of the \verb@if@ statement is executed or evaluated). If
|
||||
all conditions are false, the suite of the \verb@else@ clause, if
|
||||
other part of the \keyword{if} statement is executed or evaluated). If
|
||||
all conditions are false, the suite of the \keyword{else} clause, if
|
||||
present, is executed.
|
||||
\kwindex{elif}
|
||||
\kwindex{else}
|
||||
|
||||
\section{The {\tt while} statement}
|
||||
\section{The \keyword{while} statement}
|
||||
\stindex{while}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@while@ statement is used for repeated execution as long as a
|
||||
The \keyword{while} statement is used for repeated execution as long as a
|
||||
condition is true:
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -96,22 +96,22 @@ while_stmt: "while" condition ":" suite
|
|||
|
||||
This repeatedly tests the condition and, if it is true, executes the
|
||||
first suite; if the condition is false (which may be the first time it
|
||||
is tested) the suite of the \verb@else@ clause, if present, is
|
||||
is tested) the suite of the \keyword{else} clause, if present, is
|
||||
executed and the loop terminates.
|
||||
\kwindex{else}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
A \keyword{break} statement executed in the first suite terminates the
|
||||
loop without executing the \keyword{else} clause's suite. A
|
||||
\keyword{continue} statement executed in the first suite skips the rest
|
||||
of the suite and goes back to testing the condition.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
||||
\section{The {\tt for} statement}
|
||||
\section{The \keyword{for} statement}
|
||||
\stindex{for}
|
||||
\indexii{loop}{statement}
|
||||
|
||||
The \verb@for@ statement is used to iterate over the elements of a
|
||||
The \keyword{for} statement is used to iterate over the elements of a
|
||||
sequence (string, tuple or list):
|
||||
\obindex{sequence}
|
||||
|
||||
|
@ -125,16 +125,16 @@ suite is then executed once for each item in the sequence, in the
|
|||
order of ascending indices. Each item in turn is assigned to the
|
||||
target list using the standard rules for assignments, and then the
|
||||
suite is executed. When the items are exhausted (which is immediately
|
||||
when the sequence is empty), the suite in the \verb@else@ clause, if
|
||||
when the sequence is empty), the suite in the \keyword{else} clause, if
|
||||
present, is executed, and the loop terminates.
|
||||
\kwindex{in}
|
||||
\kwindex{else}
|
||||
\indexii{target}{list}
|
||||
|
||||
A \verb@break@ statement executed in the first suite terminates the
|
||||
loop without executing the \verb@else@ clause's suite. A
|
||||
\verb@continue@ statement executed in the first suite skips the rest
|
||||
of the suite and continues with the next item, or with the \verb@else@
|
||||
A \keyword{break} statement executed in the first suite terminates the
|
||||
loop without executing the \keyword{else} clause's suite. A
|
||||
\keyword{continue} statement executed in the first suite skips the rest
|
||||
of the suite and continues with the next item, or with the \keyword{else}
|
||||
clause if there was no next item.
|
||||
\stindex{break}
|
||||
\stindex{continue}
|
||||
|
@ -146,14 +146,14 @@ The target list is not deleted when the loop is finished, but if the
|
|||
sequence is empty, it will not have been assigned to at all by the
|
||||
loop.
|
||||
|
||||
Hint: the built-in function \verb@range()@ returns a sequence of
|
||||
Hint: the built-in function \function{range()} returns a sequence of
|
||||
integers suitable to emulate the effect of Pascal's
|
||||
\verb@for i := a to b do@;
|
||||
e.g. \verb@range(3)@ returns the list \verb@[0, 1, 2]@.
|
||||
\code{for i := a to b do};
|
||||
e.g. \code{range(3)} returns the list \code{[0, 1, 2]}.
|
||||
\bifuncindex{range}
|
||||
\index{Pascal}
|
||||
\indexii{Pascal}{language}
|
||||
|
||||
{\bf Warning:} There is a subtlety when the sequence is being modified
|
||||
\strong{Warning:} There is a subtlety when the sequence is being modified
|
||||
by the loop (this can only occur for mutable sequences, i.e. lists).
|
||||
An internal counter is used to keep track of which item is used next,
|
||||
and this is incremented on each iteration. When this counter has
|
||||
|
@ -173,10 +173,10 @@ for x in a[:]:
|
|||
if x < 0: a.remove(x)
|
||||
\end{verbatim}
|
||||
|
||||
\section{The {\tt try} statement} \label{try}
|
||||
\section{The \keyword{try} statement} \label{try}
|
||||
\stindex{try}
|
||||
|
||||
The \verb@try@ statement specifies exception handlers and/or cleanup
|
||||
The \keyword{try} statement specifies exception handlers and/or cleanup
|
||||
code for a group of statements:
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -188,13 +188,15 @@ try_fin_stmt: "try" ":" suite
|
|||
"finally" ":" suite
|
||||
\end{verbatim}
|
||||
|
||||
There are two forms of \verb@try@ statement: \verb@try...except@ and
|
||||
\verb@try...finally@. These forms cannot be mixed.
|
||||
There are two forms of \keyword{try} statement:
|
||||
\keyword{try}...\keyword{except} and
|
||||
\keyword{try}...\keyword{finally}. These forms cannot be mixed.
|
||||
|
||||
The \verb@try...except@ form specifies one or more exception handlers
|
||||
(the \verb@except@ clauses). When no exception occurs in the
|
||||
\verb@try@ clause, no exception handler is executed. When an
|
||||
exception occurs in the \verb@try@ suite, a search for an exception
|
||||
The \keyword{try}...\keyword{except} form specifies one or more
|
||||
exception handlers
|
||||
(the \keyword{except} clauses). When no exception occurs in the
|
||||
\keyword{try} clause, no exception handler is executed. When an
|
||||
exception occurs in the \keyword{try} suite, a search for an exception
|
||||
handler is started. This inspects the except clauses in turn until
|
||||
one is found that matches the exception. A condition-less except
|
||||
clause, if present, must be last; it matches any exception. For an
|
||||
|
@ -214,7 +216,7 @@ handler continues in the surrounding code and on the invocation stack.
|
|||
If the evaluation of a condition in the header of an except clause
|
||||
raises an exception, the original search for a handler is cancelled
|
||||
and a search starts for the new exception in the surrounding code and
|
||||
on the call stack (it is treated as if the entire \verb@try@ statement
|
||||
on the call stack (it is treated as if the entire \keyword{try} statement
|
||||
raised the exception).
|
||||
|
||||
When a matching except clause is found, the exception's parameter is
|
||||
|
@ -226,10 +228,10 @@ exception, and the exception occurs in the try clause of the inner
|
|||
handler, the outer handler will not handle the exception.)
|
||||
|
||||
Before an except clause's suite is executed, details about the
|
||||
exception are assigned to three variables in the \verb@sys@ module:
|
||||
\verb@sys.exc_type@ receives the object identifying the exception;
|
||||
\verb@sys.exc_value@ receives the exception's parameter;
|
||||
\verb@sys.exc_traceback@ receives a traceback object (see section
|
||||
exception are assigned to three variables in the \module{sys} module:
|
||||
\code{sys.exc_type} receives the object identifying the exception;
|
||||
\code{sys.exc_value} receives the exception's parameter;
|
||||
\code{sys.exc_traceback} receives a traceback object (see section
|
||||
\ref{traceback}) identifying the point in the program where the
|
||||
exception occurred.
|
||||
\refbimodindex{sys}
|
||||
|
@ -238,25 +240,25 @@ exception occurred.
|
|||
\ttindex{exc_traceback}
|
||||
\obindex{traceback}
|
||||
|
||||
The optional \verb@else@ clause is executed when no exception occurs
|
||||
in the \verb@try@ clause. Exceptions in the \verb@else@ clause are
|
||||
not handled by the preceding \verb@except@ clauses.
|
||||
The optional \keyword{else} clause is executed when no exception occurs
|
||||
in the \keyword{try} clause. Exceptions in the \keyword{else} clause are
|
||||
not handled by the preceding \keyword{except} clauses.
|
||||
\kwindex{else}
|
||||
|
||||
The \verb@try...finally@ form specifies a `cleanup' handler. The
|
||||
\verb@try@ clause is executed. When no exception occurs, the
|
||||
\verb@finally@ clause is executed. When an exception occurs in the
|
||||
\verb@try@ clause, the exception is temporarily saved, the
|
||||
\verb@finally@ clause is executed, and then the saved exception is
|
||||
re-raised. If the \verb@finally@ clause raises another exception or
|
||||
executes a \verb@return@, \verb@break@ or \verb@continue@ statement,
|
||||
The \keyword{try}...\keyword{finally} form specifies a `cleanup' handler. The
|
||||
\keyword{try} clause is executed. When no exception occurs, the
|
||||
\keyword{finally} clause is executed. When an exception occurs in the
|
||||
\keyword{try} clause, the exception is temporarily saved, the
|
||||
\keyword{finally} clause is executed, and then the saved exception is
|
||||
re-raised. If the \keyword{finally} clause raises another exception or
|
||||
executes a \keyword{return}, \keyword{break} or \keyword{continue} statement,
|
||||
the saved exception is lost.
|
||||
\kwindex{finally}
|
||||
|
||||
When a \verb@return@ or \verb@break@ statement is executed in the
|
||||
\verb@try@ suite of a \verb@try...finally@ statement, the
|
||||
\verb@finally@ clause is also executed `on the way out'. A
|
||||
\verb@continue@ statement is illegal in the \verb@try@ clause. (The
|
||||
When a \keyword{return} or \keyword{break} statement is executed in the
|
||||
\keyword{try} suite of a \keyword{try}...\keyword{finally} statement, the
|
||||
\keyword{finally} clause is also executed `on the way out'. A
|
||||
\keyword{continue} statement is illegal in the \keyword{try} clause. (The
|
||||
reason is a problem with the current implementation --- this
|
||||
restriction may be lifted in the future).
|
||||
\stindex{return}
|
||||
|
@ -295,7 +297,7 @@ as the global name space to be used when the function is called.
|
|||
The function definition does not execute the function body; this gets
|
||||
executed only when the function is called.
|
||||
|
||||
When one or more top-level parameters have the form {\em parameter =
|
||||
When one or more top-level parameters have the form \var{parameter \code{=}
|
||||
condition}, the function is said to have ``default parameter values''.
|
||||
Default parameter values are evaluated when the function definition is
|
||||
executed. For a parameter with a default value, the correponding
|
||||
|
@ -304,7 +306,7 @@ default value is substituted. If a parameter has a default value, all
|
|||
following parameters must also have a default value --- this is a
|
||||
syntactic restriction that is not expressed by the grammar.%
|
||||
\footnote{Currently this is not checked; instead,
|
||||
{\tt def f(a=1,b)} is interpreted as {\tt def f(a=1,b=None)}.}
|
||||
\code{def f(a=1, b)} is interpreted as \code{def f(a=1, b=None)}.}
|
||||
\indexiii{default}{parameter}{value}
|
||||
|
||||
Function call semantics are described in section \ref{calls}. When a
|
||||
|
@ -338,11 +340,11 @@ hand side's value is that of the argument.
|
|||
If the formal parameter list ends in a star followed by an identifier,
|
||||
preceded by zero or more comma-followed parameters, there must be at
|
||||
least as many arguments as there are parameters preceding the star.
|
||||
Call this number {\em N}. The first {\em N} arguments are assigned to
|
||||
Call this number \var{N}. The first \var{N} arguments are assigned to
|
||||
the corresponding formal parameters in the way descibed above. A
|
||||
tuple containing the remaining arguments, if any, is then assigned to
|
||||
the identifier following the star. This variable will always be a
|
||||
tuple: if there are no extra arguments, its value is \verb@()@, if
|
||||
tuple: if there are no extra arguments, its value is \code{()}, if
|
||||
there is just one extra argument, it is a singleton tuple.
|
||||
\indexii{variable length}{parameter list}
|
||||
|
||||
|
|
|
@ -13,9 +13,9 @@ While a language specification need not prescribe how the language
|
|||
interpreter is invoked, it is useful to have a notion of a complete
|
||||
Python program. A complete Python program is executed in a minimally
|
||||
initialized environment: all built-in and standard modules are
|
||||
available, but none have been initialized, except for \verb@sys@
|
||||
(various system services), \verb@__builtin__@ (built-in functions,
|
||||
exceptions and \verb@None@) and \verb@__main__@. The latter is used
|
||||
available, but none have been initialized, except for \module{sys}
|
||||
(various system services), \module{__builtin__} (built-in functions,
|
||||
exceptions and \code{None}) and \module{__main__}. The latter is used
|
||||
to provide the local and global name space for execution of the
|
||||
complete program.
|
||||
\refbimodindex{sys}
|
||||
|
@ -29,7 +29,7 @@ The interpreter may also be invoked in interactive mode; in this case,
|
|||
it does not read and execute a complete program but reads and executes
|
||||
one statement (possibly compound) at a time. The initial environment
|
||||
is identical to that of a complete program; each statement is executed
|
||||
in the name space of \verb@__main__@.
|
||||
in the name space of \module{__main__}.
|
||||
\index{interactive mode}
|
||||
\refbimodindex{__main__}
|
||||
|
||||
|
@ -59,7 +59,7 @@ This syntax is used in the following situations:
|
|||
|
||||
\item when parsing a module;
|
||||
|
||||
\item when parsing a string passed to the \verb@exec@ statement;
|
||||
\item when parsing a string passed to the \keyword{exec} statement;
|
||||
|
||||
\end{itemize}
|
||||
|
||||
|
@ -81,14 +81,14 @@ end of the input.
|
|||
There are two forms of expression input. Both ignore leading
|
||||
whitespace.
|
||||
|
||||
The string argument to \verb@eval()@ must have the following form:
|
||||
The string argument to \function{eval()} must have the following form:
|
||||
\bifuncindex{eval}
|
||||
|
||||
\begin{verbatim}
|
||||
eval_input: condition_list NEWLINE*
|
||||
\end{verbatim}
|
||||
|
||||
The input line read by \verb@input()@ must have the following form:
|
||||
The input line read by \function{input()} must have the following form:
|
||||
\bifuncindex{input}
|
||||
|
||||
\begin{verbatim}
|
||||
|
@ -96,10 +96,10 @@ input_input: condition_list NEWLINE
|
|||
\end{verbatim}
|
||||
|
||||
Note: to read `raw' input line without interpretation, you can use the
|
||||
built-in function \verb@raw_input()@ or the \verb@readline()@ method
|
||||
built-in function \function{raw_input()} or the \method{readline()} method
|
||||
of file objects.
|
||||
\obindex{file}
|
||||
\index{input!raw}
|
||||
\index{raw input}
|
||||
\bifuncindex{raw_index}
|
||||
\ttindex{readline}
|
||||
\bifuncindex{raw_input}
|
||||
\withsubitem{(file method)}{\ttindex{readline()}}
|
||||
|
|
Loading…
Reference in New Issue