diff options
Diffstat (limited to 'Doc/ref/ref5.tex')
-rw-r--r-- | Doc/ref/ref5.tex | 160 |
1 files changed, 82 insertions, 78 deletions
diff --git a/Doc/ref/ref5.tex b/Doc/ref/ref5.tex index b2fea3c..f296560 100644 --- a/Doc/ref/ref5.tex +++ b/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 |