| 1 | \chapter{Expressions\label{expressions}}
|
|---|
| 2 | \index{expression}
|
|---|
| 3 |
|
|---|
| 4 | This chapter explains the meaning of the elements of expressions in
|
|---|
| 5 | Python.
|
|---|
| 6 |
|
|---|
| 7 | \strong{Syntax Notes:} In this and the following chapters, extended
|
|---|
| 8 | BNF\index{BNF} notation will be used to describe syntax, not lexical
|
|---|
| 9 | analysis. When (one alternative of) a syntax rule has the form
|
|---|
| 10 |
|
|---|
| 11 | \begin{productionlist}[*]
|
|---|
| 12 | \production{name}{\token{othername}}
|
|---|
| 13 | \end{productionlist}
|
|---|
| 14 |
|
|---|
| 15 | and no semantics are given, the semantics of this form of \code{name}
|
|---|
| 16 | are the same as for \code{othername}.
|
|---|
| 17 | \index{syntax}
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | \section{Arithmetic conversions\label{conversions}}
|
|---|
| 21 | \indexii{arithmetic}{conversion}
|
|---|
| 22 |
|
|---|
| 23 | When a description of an arithmetic operator below uses the phrase
|
|---|
| 24 | ``the numeric arguments are converted to a common type,'' the
|
|---|
| 25 | arguments are coerced using the coercion rules listed at
|
|---|
| 26 | ~\ref{coercion-rules}. If both arguments are standard numeric types,
|
|---|
| 27 | the following coercions are applied:
|
|---|
| 28 |
|
|---|
| 29 | \begin{itemize}
|
|---|
| 30 | \item If either argument is a complex number, the other is converted
|
|---|
| 31 | to complex;
|
|---|
| 32 | \item otherwise, if either argument is a floating point number,
|
|---|
| 33 | the other is converted to floating point;
|
|---|
| 34 | \item otherwise, if either argument is a long integer,
|
|---|
| 35 | the other is converted to long integer;
|
|---|
| 36 | \item otherwise, both must be plain integers and no conversion
|
|---|
| 37 | is necessary.
|
|---|
| 38 | \end{itemize}
|
|---|
| 39 |
|
|---|
| 40 | Some additional rules apply for certain operators (e.g., a string left
|
|---|
| 41 | argument to the `\%' operator). Extensions can define their own
|
|---|
| 42 | coercions.
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | \section{Atoms\label{atoms}}
|
|---|
| 46 | \index{atom}
|
|---|
| 47 |
|
|---|
| 48 | Atoms are the most basic elements of expressions. The simplest atoms
|
|---|
| 49 | are identifiers or literals. Forms enclosed in
|
|---|
| 50 | reverse quotes or in parentheses, brackets or braces are also
|
|---|
| 51 | categorized syntactically as atoms. The syntax for atoms is:
|
|---|
| 52 |
|
|---|
| 53 | \begin{productionlist}
|
|---|
| 54 | \production{atom}
|
|---|
| 55 | {\token{identifier} | \token{literal} | \token{enclosure}}
|
|---|
| 56 | \production{enclosure}
|
|---|
| 57 | {\token{parenth_form} | \token{list_display}}
|
|---|
| 58 | \productioncont{| \token{generator_expression} | \token{dict_display}}
|
|---|
| 59 | \productioncont{| \token{string_conversion}}
|
|---|
| 60 | \end{productionlist}
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | \subsection{Identifiers (Names)\label{atom-identifiers}}
|
|---|
| 64 | \index{name}
|
|---|
| 65 | \index{identifier}
|
|---|
| 66 |
|
|---|
| 67 | An identifier occurring as an atom is a name. See
|
|---|
| 68 | section~\ref{naming} for documentation of naming and binding.
|
|---|
| 69 |
|
|---|
| 70 | When the name is bound to an object, evaluation of the atom yields
|
|---|
| 71 | that object. When a name is not bound, an attempt to evaluate it
|
|---|
| 72 | raises a \exception{NameError} exception.
|
|---|
| 73 | \exindex{NameError}
|
|---|
| 74 |
|
|---|
| 75 | \strong{Private name mangling:}
|
|---|
| 76 | \indexii{name}{mangling}%
|
|---|
| 77 | \indexii{private}{names}%
|
|---|
| 78 | When an identifier that textually occurs in a class definition begins
|
|---|
| 79 | with two or more underscore characters and does not end in two or more
|
|---|
| 80 | underscores, it is considered a \dfn{private name} of that class.
|
|---|
| 81 | Private names are transformed to a longer form before code is
|
|---|
| 82 | generated for them. The transformation inserts the class name in
|
|---|
| 83 | front of the name, with leading underscores removed, and a single
|
|---|
| 84 | underscore inserted in front of the class name. For example, the
|
|---|
| 85 | identifier \code{__spam} occurring in a class named \code{Ham} will be
|
|---|
| 86 | transformed to \code{_Ham__spam}. This transformation is independent
|
|---|
| 87 | of the syntactical context in which the identifier is used. If the
|
|---|
| 88 | transformed name is extremely long (longer than 255 characters),
|
|---|
| 89 | implementation defined truncation may happen. If the class name
|
|---|
| 90 | consists only of underscores, no transformation is done.
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | \subsection{Literals\label{atom-literals}}
|
|---|
| 94 | \index{literal}
|
|---|
| 95 |
|
|---|
| 96 | Python supports string literals and various numeric literals:
|
|---|
| 97 |
|
|---|
| 98 | \begin{productionlist}
|
|---|
| 99 | \production{literal}
|
|---|
| 100 | {\token{stringliteral} | \token{integer} | \token{longinteger}}
|
|---|
| 101 | \productioncont{| \token{floatnumber} | \token{imagnumber}}
|
|---|
| 102 | \end{productionlist}
|
|---|
| 103 |
|
|---|
| 104 | Evaluation of a literal yields an object of the given type (string,
|
|---|
| 105 | integer, long integer, floating point number, complex number) with the
|
|---|
| 106 | given value. The value may be approximated in the case of floating
|
|---|
| 107 | point and imaginary (complex) literals. See section \ref{literals}
|
|---|
| 108 | for details.
|
|---|
| 109 |
|
|---|
| 110 | All literals correspond to immutable data types, and hence the
|
|---|
| 111 | object's identity is less important than its value. Multiple
|
|---|
| 112 | evaluations of literals with the same value (either the same
|
|---|
| 113 | occurrence in the program text or a different occurrence) may obtain
|
|---|
| 114 | the same object or a different object with the same value.
|
|---|
| 115 | \indexiii{immutable}{data}{type}
|
|---|
| 116 | \indexii{immutable}{object}
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | \subsection{Parenthesized forms\label{parenthesized}}
|
|---|
| 120 | \index{parenthesized form}
|
|---|
| 121 |
|
|---|
| 122 | A parenthesized form is an optional expression list enclosed in
|
|---|
| 123 | parentheses:
|
|---|
| 124 |
|
|---|
| 125 | \begin{productionlist}
|
|---|
| 126 | \production{parenth_form}
|
|---|
| 127 | {"(" [\token{expression_list}] ")"}
|
|---|
| 128 | \end{productionlist}
|
|---|
| 129 |
|
|---|
| 130 | A parenthesized expression list yields whatever that expression list
|
|---|
| 131 | yields: if the list contains at least one comma, it yields a tuple;
|
|---|
| 132 | otherwise, it yields the single expression that makes up the
|
|---|
| 133 | expression list.
|
|---|
| 134 |
|
|---|
| 135 | An empty pair of parentheses yields an empty tuple object. Since
|
|---|
| 136 | tuples are immutable, the rules for literals apply (i.e., two
|
|---|
| 137 | occurrences of the empty tuple may or may not yield the same object).
|
|---|
| 138 | \indexii{empty}{tuple}
|
|---|
| 139 |
|
|---|
| 140 | Note that tuples are not formed by the parentheses, but rather by use
|
|---|
| 141 | of the comma operator. The exception is the empty tuple, for which
|
|---|
| 142 | parentheses \emph{are} required --- allowing unparenthesized ``nothing''
|
|---|
| 143 | in expressions would cause ambiguities and allow common typos to
|
|---|
| 144 | pass uncaught.
|
|---|
| 145 | \index{comma}
|
|---|
| 146 | \indexii{tuple}{display}
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | \subsection{List displays\label{lists}}
|
|---|
| 150 | \indexii{list}{display}
|
|---|
| 151 | \indexii{list}{comprehensions}
|
|---|
| 152 |
|
|---|
| 153 | A list display is a possibly empty series of expressions enclosed in
|
|---|
| 154 | square brackets:
|
|---|
| 155 |
|
|---|
| 156 | \begin{productionlist}
|
|---|
| 157 | \production{test}
|
|---|
| 158 | {\token{or_test} | \token{lambda_form}}
|
|---|
| 159 | \production{testlist}
|
|---|
| 160 | {\token{test} ( "," \token{test} )* [ "," ]}
|
|---|
| 161 | \production{list_display}
|
|---|
| 162 | {"[" [\token{listmaker}] "]"}
|
|---|
| 163 | \production{listmaker}
|
|---|
| 164 | {\token{expression} ( \token{list_for}
|
|---|
| 165 | | ( "," \token{expression} )* [","] )}
|
|---|
| 166 | \production{list_iter}
|
|---|
| 167 | {\token{list_for} | \token{list_if}}
|
|---|
| 168 | \production{list_for}
|
|---|
| 169 | {"for" \token{expression_list} "in" \token{testlist}
|
|---|
| 170 | [\token{list_iter}]}
|
|---|
| 171 | \production{list_if}
|
|---|
| 172 | {"if" \token{test} [\token{list_iter}]}
|
|---|
| 173 | \end{productionlist}
|
|---|
| 174 |
|
|---|
| 175 | A list display yields a new list object. Its contents are specified
|
|---|
| 176 | by providing either a list of expressions or a list comprehension.
|
|---|
| 177 | \indexii{list}{comprehensions}
|
|---|
| 178 | When a comma-separated list of expressions is supplied, its elements are
|
|---|
| 179 | evaluated from left to right and placed into the list object in that
|
|---|
| 180 | order. When a list comprehension is supplied, it consists of a
|
|---|
| 181 | single expression followed by at least one \keyword{for} clause and zero or
|
|---|
| 182 | more \keyword{for} or \keyword{if} clauses. In this
|
|---|
| 183 | case, the elements of the new list are those that would be produced
|
|---|
| 184 | by considering each of the \keyword{for} or \keyword{if} clauses a block,
|
|---|
| 185 | nesting from
|
|---|
| 186 | left to right, and evaluating the expression to produce a list element
|
|---|
| 187 | each time the innermost block is reached\footnote{In Python 2.3, a
|
|---|
| 188 | list comprehension "leaks" the control variables of each
|
|---|
| 189 | \samp{for} it contains into the containing scope. However, this
|
|---|
| 190 | behavior is deprecated, and relying on it will not work once this
|
|---|
| 191 | bug is fixed in a future release}.
|
|---|
| 192 | \obindex{list}
|
|---|
| 193 | \indexii{empty}{list}
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | \subsection{Generator expressions\label{genexpr}}
|
|---|
| 197 | \indexii{generator}{expression}
|
|---|
| 198 |
|
|---|
| 199 | A generator expression is a compact generator notation in parentheses:
|
|---|
| 200 |
|
|---|
| 201 | \begin{productionlist}
|
|---|
| 202 | \production{generator_expression}
|
|---|
| 203 | {"(" \token{test} \token{genexpr_for} ")"}
|
|---|
| 204 | \production{genexpr_for}
|
|---|
| 205 | {"for" \token{expression_list} "in" \token{test}
|
|---|
| 206 | [\token{genexpr_iter}]}
|
|---|
| 207 | \production{genexpr_iter}
|
|---|
| 208 | {\token{genexpr_for} | \token{genexpr_if}}
|
|---|
| 209 | \production{genexpr_if}
|
|---|
| 210 | {"if" \token{test} [\token{genexpr_iter}]}
|
|---|
| 211 | \end{productionlist}
|
|---|
| 212 |
|
|---|
| 213 | A generator expression yields a new generator object.
|
|---|
| 214 | \obindex{generator}
|
|---|
| 215 | \obindex{generator expression}
|
|---|
| 216 | It consists of a single expression followed by at least one
|
|---|
| 217 | \keyword{for} clause and zero or more \keyword{for} or \keyword{if}
|
|---|
| 218 | clauses. The iterating values of the new generator are those that
|
|---|
| 219 | would be produced by considering each of the \keyword{for} or
|
|---|
| 220 | \keyword{if} clauses a block, nesting from left to right, and
|
|---|
| 221 | evaluating the expression to yield a value that is reached the
|
|---|
| 222 | innermost block for each iteration.
|
|---|
| 223 |
|
|---|
| 224 | Variables used in the generator expression are evaluated lazily
|
|---|
| 225 | when the \method{next()} method is called for generator object
|
|---|
| 226 | (in the same fashion as normal generators). However, the leftmost
|
|---|
| 227 | \keyword{for} clause is immediately evaluated so that error produced
|
|---|
| 228 | by it can be seen before any other possible error in the code that
|
|---|
| 229 | handles the generator expression.
|
|---|
| 230 | Subsequent \keyword{for} clauses cannot be evaluated immediately since
|
|---|
| 231 | they may depend on the previous \keyword{for} loop.
|
|---|
| 232 | For example: \samp{(x*y for x in range(10) for y in bar(x))}.
|
|---|
| 233 |
|
|---|
| 234 | The parentheses can be omitted on calls with only one argument.
|
|---|
| 235 | See section \ref{calls} for the detail.
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 | \subsection{Dictionary displays\label{dict}}
|
|---|
| 239 | \indexii{dictionary}{display}
|
|---|
| 240 |
|
|---|
| 241 | A dictionary display is a possibly empty series of key/datum pairs
|
|---|
| 242 | enclosed in curly braces:
|
|---|
| 243 | \index{key}
|
|---|
| 244 | \index{datum}
|
|---|
| 245 | \index{key/datum pair}
|
|---|
| 246 |
|
|---|
| 247 | \begin{productionlist}
|
|---|
| 248 | \production{dict_display}
|
|---|
| 249 | {"\{" [\token{key_datum_list}] "\}"}
|
|---|
| 250 | \production{key_datum_list}
|
|---|
| 251 | {\token{key_datum} ("," \token{key_datum})* [","]}
|
|---|
| 252 | \production{key_datum}
|
|---|
| 253 | {\token{expression} ":" \token{expression}}
|
|---|
| 254 | \end{productionlist}
|
|---|
| 255 |
|
|---|
| 256 | A dictionary display yields a new dictionary object.
|
|---|
| 257 | \obindex{dictionary}
|
|---|
| 258 |
|
|---|
| 259 | The key/datum pairs are evaluated from left to right to define the
|
|---|
| 260 | entries of the dictionary: each key object is used as a key into the
|
|---|
| 261 | dictionary to store the corresponding datum.
|
|---|
| 262 |
|
|---|
| 263 | Restrictions on the types of the key values are listed earlier in
|
|---|
| 264 | section \ref{types}. (To summarize, the key type should be hashable,
|
|---|
| 265 | which excludes all mutable objects.) Clashes between duplicate keys
|
|---|
| 266 | are not detected; the last datum (textually rightmost in the display)
|
|---|
| 267 | stored for a given key value prevails.
|
|---|
| 268 | \indexii{immutable}{object}
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | \subsection{String conversions\label{string-conversions}}
|
|---|
| 272 | \indexii{string}{conversion}
|
|---|
| 273 | \indexii{reverse}{quotes}
|
|---|
| 274 | \indexii{backward}{quotes}
|
|---|
| 275 | \index{back-quotes}
|
|---|
| 276 |
|
|---|
| 277 | A string conversion is an expression list enclosed in reverse (a.k.a.
|
|---|
| 278 | backward) quotes:
|
|---|
| 279 |
|
|---|
| 280 | \begin{productionlist}
|
|---|
| 281 | \production{string_conversion}
|
|---|
| 282 | {"`" \token{expression_list} "`"}
|
|---|
| 283 | \end{productionlist}
|
|---|
| 284 |
|
|---|
| 285 | A string conversion evaluates the contained expression list and
|
|---|
| 286 | converts the resulting object into a string according to rules
|
|---|
| 287 | specific to its type.
|
|---|
| 288 |
|
|---|
| 289 | If the object is a string, a number, \code{None}, or a tuple, list or
|
|---|
| 290 | dictionary containing only objects whose type is one of these, the
|
|---|
| 291 | resulting string is a valid Python expression which can be passed to
|
|---|
| 292 | the built-in function \function{eval()} to yield an expression with the
|
|---|
| 293 | same value (or an approximation, if floating point numbers are
|
|---|
| 294 | involved).
|
|---|
| 295 |
|
|---|
| 296 | (In particular, converting a string adds quotes around it and converts
|
|---|
| 297 | ``funny'' characters to escape sequences that are safe to print.)
|
|---|
| 298 |
|
|---|
| 299 | Recursive objects (for example, lists or dictionaries that contain a
|
|---|
| 300 | reference to themselves, directly or indirectly) use \samp{...} to
|
|---|
| 301 | indicate a recursive reference, and the result cannot be passed to
|
|---|
| 302 | \function{eval()} to get an equal value (\exception{SyntaxError} will
|
|---|
| 303 | be raised instead).
|
|---|
| 304 | \obindex{recursive}
|
|---|
| 305 |
|
|---|
| 306 | The built-in function \function{repr()} performs exactly the same
|
|---|
| 307 | conversion in its argument as enclosing it in parentheses and reverse
|
|---|
| 308 | quotes does. The built-in function \function{str()} performs a
|
|---|
| 309 | similar but more user-friendly conversion.
|
|---|
| 310 | \bifuncindex{repr}
|
|---|
| 311 | \bifuncindex{str}
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 | \section{Primaries\label{primaries}}
|
|---|
| 315 | \index{primary}
|
|---|
| 316 |
|
|---|
| 317 | Primaries represent the most tightly bound operations of the language.
|
|---|
| 318 | Their syntax is:
|
|---|
| 319 |
|
|---|
| 320 | \begin{productionlist}
|
|---|
| 321 | \production{primary}
|
|---|
| 322 | {\token{atom} | \token{attributeref}
|
|---|
| 323 | | \token{subscription} | \token{slicing} | \token{call}}
|
|---|
| 324 | \end{productionlist}
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 | \subsection{Attribute references\label{attribute-references}}
|
|---|
| 328 | \indexii{attribute}{reference}
|
|---|
| 329 |
|
|---|
| 330 | An attribute reference is a primary followed by a period and a name:
|
|---|
| 331 |
|
|---|
| 332 | \begin{productionlist}
|
|---|
| 333 | \production{attributeref}
|
|---|
| 334 | {\token{primary} "." \token{identifier}}
|
|---|
| 335 | \end{productionlist}
|
|---|
| 336 |
|
|---|
| 337 | The primary must evaluate to an object of a type that supports
|
|---|
| 338 | attribute references, e.g., a module, list, or an instance. This
|
|---|
| 339 | object is then asked to produce the attribute whose name is the
|
|---|
| 340 | identifier. If this attribute is not available, the exception
|
|---|
| 341 | \exception{AttributeError}\exindex{AttributeError} is raised.
|
|---|
| 342 | Otherwise, the type and value of the object produced is determined by
|
|---|
| 343 | the object. Multiple evaluations of the same attribute reference may
|
|---|
| 344 | yield different objects.
|
|---|
| 345 | \obindex{module}
|
|---|
| 346 | \obindex{list}
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 | \subsection{Subscriptions\label{subscriptions}}
|
|---|
| 350 | \index{subscription}
|
|---|
| 351 |
|
|---|
| 352 | A subscription selects an item of a sequence (string, tuple or list)
|
|---|
| 353 | or mapping (dictionary) object:
|
|---|
| 354 | \obindex{sequence}
|
|---|
| 355 | \obindex{mapping}
|
|---|
| 356 | \obindex{string}
|
|---|
| 357 | \obindex{tuple}
|
|---|
| 358 | \obindex{list}
|
|---|
| 359 | \obindex{dictionary}
|
|---|
| 360 | \indexii{sequence}{item}
|
|---|
| 361 |
|
|---|
| 362 | \begin{productionlist}
|
|---|
| 363 | \production{subscription}
|
|---|
| 364 | {\token{primary} "[" \token{expression_list} "]"}
|
|---|
| 365 | \end{productionlist}
|
|---|
| 366 |
|
|---|
| 367 | The primary must evaluate to an object of a sequence or mapping type.
|
|---|
| 368 |
|
|---|
| 369 | If the primary is a mapping, the expression list must evaluate to an
|
|---|
| 370 | object whose value is one of the keys of the mapping, and the
|
|---|
| 371 | subscription selects the value in the mapping that corresponds to that
|
|---|
| 372 | key. (The expression list is a tuple except if it has exactly one
|
|---|
| 373 | item.)
|
|---|
| 374 |
|
|---|
| 375 | If the primary is a sequence, the expression (list) must evaluate to a
|
|---|
| 376 | plain integer. If this value is negative, the length of the sequence
|
|---|
| 377 | is added to it (so that, e.g., \code{x[-1]} selects the last item of
|
|---|
| 378 | \code{x}.) The resulting value must be a nonnegative integer less
|
|---|
| 379 | than the number of items in the sequence, and the subscription selects
|
|---|
| 380 | the item whose index is that value (counting from zero).
|
|---|
| 381 |
|
|---|
| 382 | A string's items are characters. A character is not a separate data
|
|---|
| 383 | type but a string of exactly one character.
|
|---|
| 384 | \index{character}
|
|---|
| 385 | \indexii{string}{item}
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 | \subsection{Slicings\label{slicings}}
|
|---|
| 389 | \index{slicing}
|
|---|
| 390 | \index{slice}
|
|---|
| 391 |
|
|---|
| 392 | A slicing selects a range of items in a sequence object (e.g., a
|
|---|
| 393 | string, tuple or list). Slicings may be used as expressions or as
|
|---|
| 394 | targets in assignment or \keyword{del} statements. The syntax for a
|
|---|
| 395 | slicing:
|
|---|
| 396 | \obindex{sequence}
|
|---|
| 397 | \obindex{string}
|
|---|
| 398 | \obindex{tuple}
|
|---|
| 399 | \obindex{list}
|
|---|
| 400 |
|
|---|
| 401 | \begin{productionlist}
|
|---|
| 402 | \production{slicing}
|
|---|
| 403 | {\token{simple_slicing} | \token{extended_slicing}}
|
|---|
| 404 | \production{simple_slicing}
|
|---|
| 405 | {\token{primary} "[" \token{short_slice} "]"}
|
|---|
| 406 | \production{extended_slicing}
|
|---|
| 407 | {\token{primary} "[" \token{slice_list} "]" }
|
|---|
| 408 | \production{slice_list}
|
|---|
| 409 | {\token{slice_item} ("," \token{slice_item})* [","]}
|
|---|
| 410 | \production{slice_item}
|
|---|
| 411 | {\token{expression} | \token{proper_slice} | \token{ellipsis}}
|
|---|
| 412 | \production{proper_slice}
|
|---|
| 413 | {\token{short_slice} | \token{long_slice}}
|
|---|
| 414 | \production{short_slice}
|
|---|
| 415 | {[\token{lower_bound}] ":" [\token{upper_bound}]}
|
|---|
| 416 | \production{long_slice}
|
|---|
| 417 | {\token{short_slice} ":" [\token{stride}]}
|
|---|
| 418 | \production{lower_bound}
|
|---|
| 419 | {\token{expression}}
|
|---|
| 420 | \production{upper_bound}
|
|---|
| 421 | {\token{expression}}
|
|---|
| 422 | \production{stride}
|
|---|
| 423 | {\token{expression}}
|
|---|
| 424 | \production{ellipsis}
|
|---|
| 425 | {"..."}
|
|---|
| 426 | \end{productionlist}
|
|---|
| 427 |
|
|---|
| 428 | There is ambiguity in the formal syntax here: anything that looks like
|
|---|
| 429 | an expression list also looks like a slice list, so any subscription
|
|---|
| 430 | can be interpreted as a slicing. Rather than further complicating the
|
|---|
| 431 | syntax, this is disambiguated by defining that in this case the
|
|---|
| 432 | interpretation as a subscription takes priority over the
|
|---|
| 433 | interpretation as a slicing (this is the case if the slice list
|
|---|
| 434 | contains no proper slice nor ellipses). Similarly, when the slice
|
|---|
| 435 | list has exactly one short slice and no trailing comma, the
|
|---|
| 436 | interpretation as a simple slicing takes priority over that as an
|
|---|
| 437 | extended slicing.\indexii{extended}{slicing}
|
|---|
| 438 |
|
|---|
| 439 | The semantics for a simple slicing are as follows. The primary must
|
|---|
| 440 | evaluate to a sequence object. The lower and upper bound expressions,
|
|---|
| 441 | if present, must evaluate to plain integers; defaults are zero and the
|
|---|
| 442 | \code{sys.maxint}, respectively. If either bound is negative, the
|
|---|
| 443 | sequence's length is added to it. The slicing now selects all items
|
|---|
| 444 | with index \var{k} such that
|
|---|
| 445 | \code{\var{i} <= \var{k} < \var{j}} where \var{i}
|
|---|
| 446 | and \var{j} are the specified lower and upper bounds. This may be an
|
|---|
| 447 | empty sequence. It is not an error if \var{i} or \var{j} lie outside the
|
|---|
| 448 | range of valid indexes (such items don't exist so they aren't
|
|---|
| 449 | selected).
|
|---|
| 450 |
|
|---|
| 451 | The semantics for an extended slicing are as follows. The primary
|
|---|
| 452 | must evaluate to a mapping object, and it is indexed with a key that
|
|---|
| 453 | is constructed from the slice list, as follows. If the slice list
|
|---|
| 454 | contains at least one comma, the key is a tuple containing the
|
|---|
| 455 | conversion of the slice items; otherwise, the conversion of the lone
|
|---|
| 456 | slice item is the key. The conversion of a slice item that is an
|
|---|
| 457 | expression is that expression. The conversion of an ellipsis slice
|
|---|
| 458 | item is the built-in \code{Ellipsis} object. The conversion of a
|
|---|
| 459 | proper slice is a slice object (see section \ref{types}) whose
|
|---|
| 460 | \member{start}, \member{stop} and \member{step} attributes are the
|
|---|
| 461 | values of the expressions given as lower bound, upper bound and
|
|---|
| 462 | stride, respectively, substituting \code{None} for missing
|
|---|
| 463 | expressions.
|
|---|
| 464 | \withsubitem{(slice object attribute)}{\ttindex{start}
|
|---|
| 465 | \ttindex{stop}\ttindex{step}}
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 | \subsection{Calls\label{calls}}
|
|---|
| 469 | \index{call}
|
|---|
| 470 |
|
|---|
| 471 | A call calls a callable object (e.g., a function) with a possibly empty
|
|---|
| 472 | series of arguments:
|
|---|
| 473 | \obindex{callable}
|
|---|
| 474 |
|
|---|
| 475 | \begin{productionlist}
|
|---|
| 476 | \production{call}
|
|---|
| 477 | {\token{primary} "(" [\token{argument_list} [","]] ")"}
|
|---|
| 478 | {\token{primary} "(" [\token{argument_list} [","] |
|
|---|
| 479 | \token{test} \token{genexpr_for} ] ")"}
|
|---|
| 480 | \production{argument_list}
|
|---|
| 481 | {\token{positional_arguments} ["," \token{keyword_arguments}]}
|
|---|
| 482 | \productioncont{ ["," "*" \token{expression}]}
|
|---|
| 483 | \productioncont{ ["," "**" \token{expression}]}
|
|---|
| 484 | \productioncont{| \token{keyword_arguments} ["," "*" \token{expression}]}
|
|---|
| 485 | \productioncont{ ["," "**" \token{expression}]}
|
|---|
| 486 | \productioncont{| "*" \token{expression} ["," "**" \token{expression}]}
|
|---|
| 487 | \productioncont{| "**" \token{expression}}
|
|---|
| 488 | \production{positional_arguments}
|
|---|
| 489 | {\token{expression} ("," \token{expression})*}
|
|---|
| 490 | \production{keyword_arguments}
|
|---|
| 491 | {\token{keyword_item} ("," \token{keyword_item})*}
|
|---|
| 492 | \production{keyword_item}
|
|---|
| 493 | {\token{identifier} "=" \token{expression}}
|
|---|
| 494 | \end{productionlist}
|
|---|
| 495 |
|
|---|
| 496 | A trailing comma may be present after the positional and keyword
|
|---|
| 497 | arguments but does not affect the semantics.
|
|---|
| 498 |
|
|---|
| 499 | The primary must evaluate to a callable object (user-defined
|
|---|
| 500 | functions, built-in functions, methods of built-in objects, class
|
|---|
| 501 | objects, methods of class instances, and certain class instances
|
|---|
| 502 | themselves are callable; extensions may define additional callable
|
|---|
| 503 | object types). All argument expressions are evaluated before the call
|
|---|
| 504 | is attempted. Please refer to section \ref{function} for the syntax
|
|---|
| 505 | of formal parameter lists.
|
|---|
| 506 |
|
|---|
| 507 | If keyword arguments are present, they are first converted to
|
|---|
| 508 | positional arguments, as follows. First, a list of unfilled slots is
|
|---|
| 509 | created for the formal parameters. If there are N positional
|
|---|
| 510 | arguments, they are placed in the first N slots. Next, for each
|
|---|
| 511 | keyword argument, the identifier is used to determine the
|
|---|
| 512 | corresponding slot (if the identifier is the same as the first formal
|
|---|
| 513 | parameter name, the first slot is used, and so on). If the slot is
|
|---|
| 514 | already filled, a \exception{TypeError} exception is raised.
|
|---|
| 515 | Otherwise, the value of the argument is placed in the slot, filling it
|
|---|
| 516 | (even if the expression is \code{None}, it fills the slot). When all
|
|---|
| 517 | arguments have been processed, the slots that are still unfilled are
|
|---|
| 518 | filled with the corresponding default value from the function
|
|---|
| 519 | definition. (Default values are calculated, once, when the function
|
|---|
| 520 | is defined; thus, a mutable object such as a list or dictionary used
|
|---|
| 521 | as default value will be shared by all calls that don't specify an
|
|---|
| 522 | argument value for the corresponding slot; this should usually be
|
|---|
| 523 | avoided.) If there are any unfilled slots for which no default value
|
|---|
| 524 | is specified, a \exception{TypeError} exception is raised. Otherwise,
|
|---|
| 525 | the list of filled slots is used as the argument list for the call.
|
|---|
| 526 |
|
|---|
| 527 | If there are more positional arguments than there are formal parameter
|
|---|
| 528 | slots, a \exception{TypeError} exception is raised, unless a formal
|
|---|
| 529 | parameter using the syntax \samp{*identifier} is present; in this
|
|---|
| 530 | case, that formal parameter receives a tuple containing the excess
|
|---|
| 531 | positional arguments (or an empty tuple if there were no excess
|
|---|
| 532 | positional arguments).
|
|---|
| 533 |
|
|---|
| 534 | If any keyword argument does not correspond to a formal parameter
|
|---|
| 535 | name, a \exception{TypeError} exception is raised, unless a formal
|
|---|
| 536 | parameter using the syntax \samp{**identifier} is present; in this
|
|---|
| 537 | case, that formal parameter receives a dictionary containing the
|
|---|
| 538 | excess keyword arguments (using the keywords as keys and the argument
|
|---|
| 539 | values as corresponding values), or a (new) empty dictionary if there
|
|---|
| 540 | were no excess keyword arguments.
|
|---|
| 541 |
|
|---|
| 542 | If the syntax \samp{*expression} appears in the function call,
|
|---|
| 543 | \samp{expression} must evaluate to a sequence. Elements from this
|
|---|
| 544 | sequence are treated as if they were additional positional arguments;
|
|---|
| 545 | if there are postional arguments \var{x1},...,\var{xN} , and
|
|---|
| 546 | \samp{expression} evaluates to a sequence \var{y1},...,\var{yM}, this
|
|---|
| 547 | is equivalent to a call with M+N positional arguments
|
|---|
| 548 | \var{x1},...,\var{xN},\var{y1},...,\var{yM}.
|
|---|
| 549 |
|
|---|
| 550 | A consequence of this is that although the \samp{*expression} syntax
|
|---|
| 551 | appears \emph{after} any keyword arguments, it is processed
|
|---|
| 552 | \emph{before} the keyword arguments (and the
|
|---|
| 553 | \samp{**expression} argument, if any -- see below). So:
|
|---|
| 554 |
|
|---|
| 555 | \begin{verbatim}
|
|---|
| 556 | >>> def f(a, b):
|
|---|
| 557 | ... print a, b
|
|---|
| 558 | ...
|
|---|
| 559 | >>> f(b=1, *(2,))
|
|---|
| 560 | 2 1
|
|---|
| 561 | >>> f(a=1, *(2,))
|
|---|
| 562 | Traceback (most recent call last):
|
|---|
| 563 | File "<stdin>", line 1, in ?
|
|---|
| 564 | TypeError: f() got multiple values for keyword argument 'a'
|
|---|
| 565 | >>> f(1, *(2,))
|
|---|
| 566 | 1 2
|
|---|
| 567 | \end{verbatim}
|
|---|
| 568 |
|
|---|
| 569 | It is unusual for both keyword arguments and the
|
|---|
| 570 | \samp{*expression} syntax to be used in the same call, so in practice
|
|---|
| 571 | this confusion does not arise.
|
|---|
| 572 |
|
|---|
| 573 | If the syntax \samp{**expression} appears in the function call,
|
|---|
| 574 | \samp{expression} must evaluate to a (subclass of) dictionary, the
|
|---|
| 575 | contents of which are treated as additional keyword arguments. In the
|
|---|
| 576 | case of a keyword appearing in both \samp{expression} and as an
|
|---|
| 577 | explicit keyword argument, a \exception{TypeError} exception is
|
|---|
| 578 | raised.
|
|---|
| 579 |
|
|---|
| 580 | Formal parameters using the syntax \samp{*identifier} or
|
|---|
| 581 | \samp{**identifier} cannot be used as positional argument slots or
|
|---|
| 582 | as keyword argument names. Formal parameters using the syntax
|
|---|
| 583 | \samp{(sublist)} cannot be used as keyword argument names; the
|
|---|
| 584 | outermost sublist corresponds to a single unnamed argument slot, and
|
|---|
| 585 | the argument value is assigned to the sublist using the usual tuple
|
|---|
| 586 | assignment rules after all other parameter processing is done.
|
|---|
| 587 |
|
|---|
| 588 | A call always returns some value, possibly \code{None}, unless it
|
|---|
| 589 | raises an exception. How this value is computed depends on the type
|
|---|
| 590 | of the callable object.
|
|---|
| 591 |
|
|---|
| 592 | If it is---
|
|---|
| 593 |
|
|---|
| 594 | \begin{description}
|
|---|
| 595 |
|
|---|
| 596 | \item[a user-defined function:] The code block for the function is
|
|---|
| 597 | executed, passing it the argument list. The first thing the code
|
|---|
| 598 | block will do is bind the formal parameters to the arguments; this is
|
|---|
| 599 | described in section \ref{function}. When the code block executes a
|
|---|
| 600 | \keyword{return} statement, this specifies the return value of the
|
|---|
| 601 | function call.
|
|---|
| 602 | \indexii{function}{call}
|
|---|
| 603 | \indexiii{user-defined}{function}{call}
|
|---|
| 604 | \obindex{user-defined function}
|
|---|
| 605 | \obindex{function}
|
|---|
| 606 |
|
|---|
| 607 | \item[a built-in function or method:] The result is up to the
|
|---|
| 608 | interpreter; see the \citetitle[../lib/built-in-funcs.html]{Python
|
|---|
| 609 | Library Reference} for the descriptions of built-in functions and
|
|---|
| 610 | methods.
|
|---|
| 611 | \indexii{function}{call}
|
|---|
| 612 | \indexii{built-in function}{call}
|
|---|
| 613 | \indexii{method}{call}
|
|---|
| 614 | \indexii{built-in method}{call}
|
|---|
| 615 | \obindex{built-in method}
|
|---|
| 616 | \obindex{built-in function}
|
|---|
| 617 | \obindex{method}
|
|---|
| 618 | \obindex{function}
|
|---|
| 619 |
|
|---|
| 620 | \item[a class object:] A new instance of that class is returned.
|
|---|
| 621 | \obindex{class}
|
|---|
| 622 | \indexii{class object}{call}
|
|---|
| 623 |
|
|---|
| 624 | \item[a class instance method:] The corresponding user-defined
|
|---|
| 625 | function is called, with an argument list that is one longer than the
|
|---|
| 626 | argument list of the call: the instance becomes the first argument.
|
|---|
| 627 | \obindex{class instance}
|
|---|
| 628 | \obindex{instance}
|
|---|
| 629 | \indexii{class instance}{call}
|
|---|
| 630 |
|
|---|
| 631 | \item[a class instance:] The class must define a \method{__call__()}
|
|---|
| 632 | method; the effect is then the same as if that method was called.
|
|---|
| 633 | \indexii{instance}{call}
|
|---|
| 634 | \withsubitem{(object method)}{\ttindex{__call__()}}
|
|---|
| 635 |
|
|---|
| 636 | \end{description}
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 | \section{The power operator\label{power}}
|
|---|
| 640 |
|
|---|
| 641 | The power operator binds more tightly than unary operators on its
|
|---|
| 642 | left; it binds less tightly than unary operators on its right. The
|
|---|
| 643 | syntax is:
|
|---|
| 644 |
|
|---|
| 645 | \begin{productionlist}
|
|---|
| 646 | \production{power}
|
|---|
| 647 | {\token{primary} ["**" \token{u_expr}]}
|
|---|
| 648 | \end{productionlist}
|
|---|
| 649 |
|
|---|
| 650 | Thus, in an unparenthesized sequence of power and unary operators, the
|
|---|
| 651 | operators are evaluated from right to left (this does not constrain
|
|---|
| 652 | the evaluation order for the operands).
|
|---|
| 653 |
|
|---|
| 654 | The power operator has the same semantics as the built-in
|
|---|
| 655 | \function{pow()} function, when called with two arguments: it yields
|
|---|
| 656 | its left argument raised to the power of its right argument. The
|
|---|
| 657 | numeric arguments are first converted to a common type. The result
|
|---|
| 658 | type is that of the arguments after coercion.
|
|---|
| 659 |
|
|---|
| 660 | With mixed operand types, the coercion rules for binary arithmetic
|
|---|
| 661 | operators apply. For int and long int operands, the result has the
|
|---|
| 662 | same type as the operands (after coercion) unless the second argument
|
|---|
| 663 | is negative; in that case, all arguments are converted to float and a
|
|---|
| 664 | float result is delivered. For example, \code{10**2} returns \code{100},
|
|---|
| 665 | but \code{10**-2} returns \code{0.01}. (This last feature was added in
|
|---|
| 666 | Python 2.2. In Python 2.1 and before, if both arguments were of integer
|
|---|
| 667 | types and the second argument was negative, an exception was raised).
|
|---|
| 668 |
|
|---|
| 669 | Raising \code{0.0} to a negative power results in a
|
|---|
| 670 | \exception{ZeroDivisionError}. Raising a negative number to a
|
|---|
| 671 | fractional power results in a \exception{ValueError}.
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 | \section{Unary arithmetic operations \label{unary}}
|
|---|
| 675 | \indexiii{unary}{arithmetic}{operation}
|
|---|
| 676 | \indexiii{unary}{bit-wise}{operation}
|
|---|
| 677 |
|
|---|
| 678 | All unary arithmetic (and bit-wise) operations have the same priority:
|
|---|
| 679 |
|
|---|
| 680 | \begin{productionlist}
|
|---|
| 681 | \production{u_expr}
|
|---|
| 682 | {\token{power} | "-" \token{u_expr}
|
|---|
| 683 | | "+" \token{u_expr} | "{\~}" \token{u_expr}}
|
|---|
| 684 | \end{productionlist}
|
|---|
| 685 |
|
|---|
| 686 | The unary \code{-} (minus) operator yields the negation of its
|
|---|
| 687 | numeric argument.
|
|---|
| 688 | \index{negation}
|
|---|
| 689 | \index{minus}
|
|---|
| 690 |
|
|---|
| 691 | The unary \code{+} (plus) operator yields its numeric argument
|
|---|
| 692 | unchanged.
|
|---|
| 693 | \index{plus}
|
|---|
| 694 |
|
|---|
| 695 | The unary \code{\~} (invert) operator yields the bit-wise inversion
|
|---|
| 696 | of its plain or long integer argument. The bit-wise inversion of
|
|---|
| 697 | \code{x} is defined as \code{-(x+1)}. It only applies to integral
|
|---|
| 698 | numbers.
|
|---|
| 699 | \index{inversion}
|
|---|
| 700 |
|
|---|
| 701 | In all three cases, if the argument does not have the proper type,
|
|---|
| 702 | a \exception{TypeError} exception is raised.
|
|---|
| 703 | \exindex{TypeError}
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 | \section{Binary arithmetic operations\label{binary}}
|
|---|
| 707 | \indexiii{binary}{arithmetic}{operation}
|
|---|
| 708 |
|
|---|
| 709 | The binary arithmetic operations have the conventional priority
|
|---|
| 710 | levels. Note that some of these operations also apply to certain
|
|---|
| 711 | non-numeric types. Apart from the power operator, there are only two
|
|---|
| 712 | levels, one for multiplicative operators and one for additive
|
|---|
| 713 | operators:
|
|---|
| 714 |
|
|---|
| 715 | \begin{productionlist}
|
|---|
| 716 | \production{m_expr}
|
|---|
| 717 | {\token{u_expr} | \token{m_expr} "*" \token{u_expr}
|
|---|
| 718 | | \token{m_expr} "//" \token{u_expr}
|
|---|
| 719 | | \token{m_expr} "/" \token{u_expr}}
|
|---|
| 720 | \productioncont{| \token{m_expr} "\%" \token{u_expr}}
|
|---|
| 721 | \production{a_expr}
|
|---|
| 722 | {\token{m_expr} | \token{a_expr} "+" \token{m_expr}
|
|---|
| 723 | | \token{a_expr} "-" \token{m_expr}}
|
|---|
| 724 | \end{productionlist}
|
|---|
| 725 |
|
|---|
| 726 | The \code{*} (multiplication) operator yields the product of its
|
|---|
| 727 | arguments. The arguments must either both be numbers, or one argument
|
|---|
| 728 | must be an integer (plain or long) and the other must be a sequence.
|
|---|
| 729 | In the former case, the numbers are converted to a common type and
|
|---|
| 730 | then multiplied together. In the latter case, sequence repetition is
|
|---|
| 731 | performed; a negative repetition factor yields an empty sequence.
|
|---|
| 732 | \index{multiplication}
|
|---|
| 733 |
|
|---|
| 734 | The \code{/} (division) and \code{//} (floor division) operators yield
|
|---|
| 735 | the quotient of their arguments. The numeric arguments are first
|
|---|
| 736 | converted to a common type. Plain or long integer division yields an
|
|---|
| 737 | integer of the same type; the result is that of mathematical division
|
|---|
| 738 | with the `floor' function applied to the result. Division by zero
|
|---|
| 739 | raises the
|
|---|
| 740 | \exception{ZeroDivisionError} exception.
|
|---|
| 741 | \exindex{ZeroDivisionError}
|
|---|
| 742 | \index{division}
|
|---|
| 743 |
|
|---|
| 744 | The \code{\%} (modulo) operator yields the remainder from the
|
|---|
| 745 | division of the first argument by the second. The numeric arguments
|
|---|
| 746 | are first converted to a common type. A zero right argument raises
|
|---|
| 747 | the \exception{ZeroDivisionError} exception. The arguments may be floating
|
|---|
| 748 | point numbers, e.g., \code{3.14\%0.7} equals \code{0.34} (since
|
|---|
| 749 | \code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always
|
|---|
| 750 | yields a result with the same sign as its second operand (or zero);
|
|---|
| 751 | the absolute value of the result is strictly smaller than the absolute
|
|---|
| 752 | value of the second operand\footnote{
|
|---|
| 753 | While \code{abs(x\%y) < abs(y)} is true mathematically, for
|
|---|
| 754 | floats it may not be true numerically due to roundoff. For
|
|---|
| 755 | example, and assuming a platform on which a Python float is an
|
|---|
| 756 | IEEE 754 double-precision number, in order that \code{-1e-100 \% 1e100}
|
|---|
| 757 | have the same sign as \code{1e100}, the computed result is
|
|---|
| 758 | \code{-1e-100 + 1e100}, which is numerically exactly equal
|
|---|
| 759 | to \code{1e100}. Function \function{fmod()} in the \module{math}
|
|---|
| 760 | module returns a result whose sign matches the sign of the
|
|---|
| 761 | first argument instead, and so returns \code{-1e-100} in this case.
|
|---|
| 762 | Which approach is more appropriate depends on the application.
|
|---|
| 763 | }.
|
|---|
| 764 | \index{modulo}
|
|---|
| 765 |
|
|---|
| 766 | The integer division and modulo operators are connected by the
|
|---|
| 767 | following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and
|
|---|
| 768 | modulo are also connected with the built-in function \function{divmod()}:
|
|---|
| 769 | \code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for
|
|---|
| 770 | floating point numbers; there similar identities hold
|
|---|
| 771 | approximately where \code{x/y} is replaced by \code{floor(x/y)} or
|
|---|
| 772 | \code{floor(x/y) - 1}\footnote{
|
|---|
| 773 | If x is very close to an exact integer multiple of y, it's
|
|---|
| 774 | possible for \code{floor(x/y)} to be one larger than
|
|---|
| 775 | \code{(x-x\%y)/y} due to rounding. In such cases, Python returns
|
|---|
| 776 | the latter result, in order to preserve that \code{divmod(x,y)[0]
|
|---|
| 777 | * y + x \%{} y} be very close to \code{x}.
|
|---|
| 778 | }.
|
|---|
| 779 |
|
|---|
| 780 | In addition to performing the modulo operation on numbers, the \code{\%}
|
|---|
| 781 | operator is also overloaded by string and unicode objects to perform
|
|---|
| 782 | string formatting (also known as interpolation). The syntax for string
|
|---|
| 783 | formatting is described in the
|
|---|
| 784 | \citetitle[../lib/typesseq-strings.html]{Python Library Reference},
|
|---|
| 785 | section ``Sequence Types''.
|
|---|
| 786 |
|
|---|
| 787 | \deprecated{2.3}{The floor division operator, the modulo operator,
|
|---|
| 788 | and the \function{divmod()} function are no longer defined for complex
|
|---|
| 789 | numbers. Instead, convert to a floating point number using the
|
|---|
| 790 | \function{abs()} function if appropriate.}
|
|---|
| 791 |
|
|---|
| 792 | The \code{+} (addition) operator yields the sum of its arguments.
|
|---|
| 793 | The arguments must either both be numbers or both sequences of the
|
|---|
| 794 | same type. In the former case, the numbers are converted to a common
|
|---|
| 795 | type and then added together. In the latter case, the sequences are
|
|---|
| 796 | concatenated.
|
|---|
| 797 | \index{addition}
|
|---|
| 798 |
|
|---|
| 799 | The \code{-} (subtraction) operator yields the difference of its
|
|---|
| 800 | arguments. The numeric arguments are first converted to a common
|
|---|
| 801 | type.
|
|---|
| 802 | \index{subtraction}
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 | \section{Shifting operations\label{shifting}}
|
|---|
| 806 | \indexii{shifting}{operation}
|
|---|
| 807 |
|
|---|
| 808 | The shifting operations have lower priority than the arithmetic
|
|---|
| 809 | operations:
|
|---|
| 810 |
|
|---|
| 811 | \begin{productionlist}
|
|---|
| 812 | % The empty groups below prevent conversion to guillemets.
|
|---|
| 813 | \production{shift_expr}
|
|---|
| 814 | {\token{a_expr}
|
|---|
| 815 | | \token{shift_expr} ( "<{}<" | ">{}>" ) \token{a_expr}}
|
|---|
| 816 | \end{productionlist}
|
|---|
| 817 |
|
|---|
| 818 | These operators accept plain or long integers as arguments. The
|
|---|
| 819 | arguments are converted to a common type. They shift the first
|
|---|
| 820 | argument to the left or right by the number of bits given by the
|
|---|
| 821 | second argument.
|
|---|
| 822 |
|
|---|
| 823 | A right shift by \var{n} bits is defined as division by
|
|---|
| 824 | \code{pow(2,\var{n})}. A left shift by \var{n} bits is defined as
|
|---|
| 825 | multiplication with \code{pow(2,\var{n})}; for plain integers there is
|
|---|
| 826 | no overflow check so in that case the operation drops bits and flips
|
|---|
| 827 | the sign if the result is not less than \code{pow(2,31)} in absolute
|
|---|
| 828 | value. Negative shift counts raise a \exception{ValueError}
|
|---|
| 829 | exception.
|
|---|
| 830 | \exindex{ValueError}
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 | \section{Binary bit-wise operations\label{bitwise}}
|
|---|
| 834 | \indexiii{binary}{bit-wise}{operation}
|
|---|
| 835 |
|
|---|
| 836 | Each of the three bitwise operations has a different priority level:
|
|---|
| 837 |
|
|---|
| 838 | \begin{productionlist}
|
|---|
| 839 | \production{and_expr}
|
|---|
| 840 | {\token{shift_expr} | \token{and_expr} "\&" \token{shift_expr}}
|
|---|
| 841 | \production{xor_expr}
|
|---|
| 842 | {\token{and_expr} | \token{xor_expr} "\textasciicircum" \token{and_expr}}
|
|---|
| 843 | \production{or_expr}
|
|---|
| 844 | {\token{xor_expr} | \token{or_expr} "|" \token{xor_expr}}
|
|---|
| 845 | \end{productionlist}
|
|---|
| 846 |
|
|---|
| 847 | The \code{\&} operator yields the bitwise AND of its arguments, which
|
|---|
| 848 | must be plain or long integers. The arguments are converted to a
|
|---|
| 849 | common type.
|
|---|
| 850 | \indexii{bit-wise}{and}
|
|---|
| 851 |
|
|---|
| 852 | The \code{\^} operator yields the bitwise XOR (exclusive OR) of its
|
|---|
| 853 | arguments, which must be plain or long integers. The arguments are
|
|---|
| 854 | converted to a common type.
|
|---|
| 855 | \indexii{bit-wise}{xor}
|
|---|
| 856 | \indexii{exclusive}{or}
|
|---|
| 857 |
|
|---|
| 858 | The \code{|} operator yields the bitwise (inclusive) OR of its
|
|---|
| 859 | arguments, which must be plain or long integers. The arguments are
|
|---|
| 860 | converted to a common type.
|
|---|
| 861 | \indexii{bit-wise}{or}
|
|---|
| 862 | \indexii{inclusive}{or}
|
|---|
| 863 |
|
|---|
| 864 |
|
|---|
| 865 | \section{Comparisons\label{comparisons}}
|
|---|
| 866 | \index{comparison}
|
|---|
| 867 |
|
|---|
| 868 | Unlike C, all comparison operations in Python have the same priority,
|
|---|
| 869 | which is lower than that of any arithmetic, shifting or bitwise
|
|---|
| 870 | operation. Also unlike C, expressions like \code{a < b < c} have the
|
|---|
| 871 | interpretation that is conventional in mathematics:
|
|---|
| 872 | \indexii{C}{language}
|
|---|
| 873 |
|
|---|
| 874 | \begin{productionlist}
|
|---|
| 875 | \production{comparison}
|
|---|
| 876 | {\token{or_expr} ( \token{comp_operator} \token{or_expr} )*}
|
|---|
| 877 | \production{comp_operator}
|
|---|
| 878 | {"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="}
|
|---|
| 879 | \productioncont{| "is" ["not"] | ["not"] "in"}
|
|---|
| 880 | \end{productionlist}
|
|---|
| 881 |
|
|---|
| 882 | Comparisons yield boolean values: \code{True} or \code{False}.
|
|---|
| 883 |
|
|---|
| 884 | Comparisons can be chained arbitrarily, e.g., \code{x < y <= z} is
|
|---|
| 885 | equivalent to \code{x < y and y <= z}, except that \code{y} is
|
|---|
| 886 | evaluated only once (but in both cases \code{z} is not evaluated at all
|
|---|
| 887 | when \code{x < y} is found to be false).
|
|---|
| 888 | \indexii{chaining}{comparisons}
|
|---|
| 889 |
|
|---|
| 890 | Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are
|
|---|
| 891 | expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison
|
|---|
| 892 | operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent
|
|---|
| 893 | to \var{a opa b} \keyword{and} \var{b opb c} \keyword{and} \ldots
|
|---|
| 894 | \var{y opy z}, except that each expression is evaluated at most once.
|
|---|
| 895 |
|
|---|
| 896 | Note that \var{a opa b opb c} doesn't imply any kind of comparison
|
|---|
| 897 | between \var{a} and \var{c}, so that, e.g., \code{x < y > z} is
|
|---|
| 898 | perfectly legal (though perhaps not pretty).
|
|---|
| 899 |
|
|---|
| 900 | The forms \code{<>} and \code{!=} are equivalent; for consistency with
|
|---|
| 901 | C, \code{!=} is preferred; where \code{!=} is mentioned below
|
|---|
| 902 | \code{<>} is also accepted. The \code{<>} spelling is considered
|
|---|
| 903 | obsolescent.
|
|---|
| 904 |
|
|---|
| 905 | The operators \code{<}, \code{>}, \code{==}, \code{>=}, \code{<=}, and
|
|---|
| 906 | \code{!=} compare
|
|---|
| 907 | the values of two objects. The objects need not have the same type.
|
|---|
| 908 | If both are numbers, they are converted to a common type. Otherwise,
|
|---|
| 909 | objects of different types \emph{always} compare unequal, and are
|
|---|
| 910 | ordered consistently but arbitrarily. You can control comparison
|
|---|
| 911 | behavior of objects of non-builtin types by defining a \code{__cmp__}
|
|---|
| 912 | method or rich comparison methods like \code{__gt__}, described in
|
|---|
| 913 | section~\ref{specialnames}.
|
|---|
| 914 |
|
|---|
| 915 | (This unusual definition of comparison was used to simplify the
|
|---|
| 916 | definition of operations like sorting and the \keyword{in} and
|
|---|
| 917 | \keyword{not in} operators. In the future, the comparison rules for
|
|---|
| 918 | objects of different types are likely to change.)
|
|---|
| 919 |
|
|---|
| 920 | Comparison of objects of the same type depends on the type:
|
|---|
| 921 |
|
|---|
| 922 | \begin{itemize}
|
|---|
| 923 |
|
|---|
| 924 | \item
|
|---|
| 925 | Numbers are compared arithmetically.
|
|---|
| 926 |
|
|---|
| 927 | \item
|
|---|
| 928 | Strings are compared lexicographically using the numeric equivalents
|
|---|
| 929 | (the result of the built-in function \function{ord()}) of their
|
|---|
| 930 | characters. Unicode and 8-bit strings are fully interoperable in this
|
|---|
| 931 | behavior.
|
|---|
| 932 |
|
|---|
| 933 | \item
|
|---|
| 934 | Tuples and lists are compared lexicographically using comparison of
|
|---|
| 935 | corresponding elements. This means that to compare equal, each
|
|---|
| 936 | element must compare equal and the two sequences must be of the same
|
|---|
| 937 | type and have the same length.
|
|---|
| 938 |
|
|---|
| 939 | If not equal, the sequences are ordered the same as their first
|
|---|
| 940 | differing elements. For example, \code{cmp([1,2,x], [1,2,y])} returns
|
|---|
| 941 | the same as \code{cmp(x,y)}. If the corresponding element does not
|
|---|
| 942 | exist, the shorter sequence is ordered first (for example,
|
|---|
| 943 | \code{[1,2] < [1,2,3]}).
|
|---|
| 944 |
|
|---|
| 945 | \item
|
|---|
| 946 | Mappings (dictionaries) compare equal if and only if their sorted
|
|---|
| 947 | (key, value) lists compare equal.\footnote{The implementation computes
|
|---|
| 948 | this efficiently, without constructing lists or sorting.}
|
|---|
| 949 | Outcomes other than equality are resolved consistently, but are not
|
|---|
| 950 | otherwise defined.\footnote{Earlier versions of Python used
|
|---|
| 951 | lexicographic comparison of the sorted (key, value) lists, but this
|
|---|
| 952 | was very expensive for the common case of comparing for equality. An
|
|---|
| 953 | even earlier version of Python compared dictionaries by identity only,
|
|---|
| 954 | but this caused surprises because people expected to be able to test
|
|---|
| 955 | a dictionary for emptiness by comparing it to \code{\{\}}.}
|
|---|
| 956 |
|
|---|
| 957 | \item
|
|---|
| 958 | Most other objects of builtin types compare unequal unless they are
|
|---|
| 959 | the same object;
|
|---|
| 960 | the choice whether one object is considered smaller or larger than
|
|---|
| 961 | another one is made arbitrarily but consistently within one
|
|---|
| 962 | execution of a program.
|
|---|
| 963 |
|
|---|
| 964 | \end{itemize}
|
|---|
| 965 |
|
|---|
| 966 | The operators \keyword{in} and \keyword{not in} test for set
|
|---|
| 967 | membership. \code{\var{x} in \var{s}} evaluates to true if \var{x}
|
|---|
| 968 | is a member of the set \var{s}, and false otherwise. \code{\var{x}
|
|---|
| 969 | not in \var{s}} returns the negation of \code{\var{x} in \var{s}}.
|
|---|
| 970 | The set membership test has traditionally been bound to sequences; an
|
|---|
| 971 | object is a member of a set if the set is a sequence and contains an
|
|---|
| 972 | element equal to that object. However, it is possible for an object
|
|---|
| 973 | to support membership tests without being a sequence. In particular,
|
|---|
| 974 | dictionaries support membership testing as a nicer way of spelling
|
|---|
| 975 | \code{\var{key} in \var{dict}}; other mapping types may follow suit.
|
|---|
| 976 |
|
|---|
| 977 | For the list and tuple types, \code{\var{x} in \var{y}} is true if and
|
|---|
| 978 | only if there exists an index \var{i} such that
|
|---|
| 979 | \code{\var{x} == \var{y}[\var{i}]} is true.
|
|---|
| 980 |
|
|---|
| 981 | For the Unicode and string types, \code{\var{x} in \var{y}} is true if
|
|---|
| 982 | and only if \var{x} is a substring of \var{y}. An equivalent test is
|
|---|
| 983 | \code{y.find(x) != -1}. Note, \var{x} and \var{y} need not be the
|
|---|
| 984 | same type; consequently, \code{u'ab' in 'abc'} will return \code{True}.
|
|---|
| 985 | Empty strings are always considered to be a substring of any other string,
|
|---|
| 986 | so \code{"" in "abc"} will return \code{True}.
|
|---|
| 987 | \versionchanged[Previously, \var{x} was required to be a string of
|
|---|
| 988 | length \code{1}]{2.3}
|
|---|
| 989 |
|
|---|
| 990 | For user-defined classes which define the \method{__contains__()} method,
|
|---|
| 991 | \code{\var{x} in \var{y}} is true if and only if
|
|---|
| 992 | \code{\var{y}.__contains__(\var{x})} is true.
|
|---|
| 993 |
|
|---|
| 994 | For user-defined classes which do not define \method{__contains__()} and
|
|---|
| 995 | do define \method{__getitem__()}, \code{\var{x} in \var{y}} is true if
|
|---|
| 996 | and only if there is a non-negative integer index \var{i} such that
|
|---|
| 997 | \code{\var{x} == \var{y}[\var{i}]}, and all lower integer indices
|
|---|
| 998 | do not raise \exception{IndexError} exception. (If any other exception
|
|---|
| 999 | is raised, it is as if \keyword{in} raised that exception).
|
|---|
| 1000 |
|
|---|
| 1001 | The operator \keyword{not in} is defined to have the inverse true value
|
|---|
| 1002 | of \keyword{in}.
|
|---|
| 1003 | \opindex{in}
|
|---|
| 1004 | \opindex{not in}
|
|---|
| 1005 | \indexii{membership}{test}
|
|---|
| 1006 | \obindex{sequence}
|
|---|
| 1007 |
|
|---|
| 1008 | The operators \keyword{is} and \keyword{is not} test for object identity:
|
|---|
| 1009 | \code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y}
|
|---|
| 1010 | are the same object. \code{\var{x} is not \var{y}} yields the inverse
|
|---|
| 1011 | truth value.
|
|---|
| 1012 | \opindex{is}
|
|---|
| 1013 | \opindex{is not}
|
|---|
| 1014 | \indexii{identity}{test}
|
|---|
| 1015 |
|
|---|
| 1016 |
|
|---|
| 1017 | \section{Boolean operations\label{Booleans}}
|
|---|
| 1018 | \indexii{Boolean}{operation}
|
|---|
| 1019 |
|
|---|
| 1020 | Boolean operations have the lowest priority of all Python operations:
|
|---|
| 1021 |
|
|---|
| 1022 | \begin{productionlist}
|
|---|
| 1023 | \production{expression}
|
|---|
| 1024 | {\token{or_test} [\token{if} \token{or_test} \token{else}
|
|---|
| 1025 | \token{test}] | \token{lambda_form}}
|
|---|
| 1026 | \production{or_test}
|
|---|
| 1027 | {\token{and_test} | \token{or_test} "or" \token{and_test}}
|
|---|
| 1028 | \production{and_test}
|
|---|
| 1029 | {\token{not_test} | \token{and_test} "and" \token{not_test}}
|
|---|
| 1030 | \production{not_test}
|
|---|
| 1031 | {\token{comparison} | "not" \token{not_test}}
|
|---|
| 1032 | \end{productionlist}
|
|---|
| 1033 |
|
|---|
| 1034 | In the context of Boolean operations, and also when expressions are
|
|---|
| 1035 | used by control flow statements, the following values are interpreted
|
|---|
| 1036 | as false: \code{False}, \code{None}, numeric zero of all types, and empty
|
|---|
| 1037 | strings and containers (including strings, tuples, lists, dictionaries,
|
|---|
| 1038 | sets and frozensets). All other values are interpreted as true.
|
|---|
| 1039 |
|
|---|
| 1040 | The operator \keyword{not} yields \code{True} if its argument is false,
|
|---|
| 1041 | \code{False} otherwise.
|
|---|
| 1042 | \opindex{not}
|
|---|
| 1043 |
|
|---|
| 1044 | The expression \code{\var{x} if \var{C} else \var{y}} first evaluates
|
|---|
| 1045 | \var{C} (\emph{not} \var{x}); if \var{C} is true, \var{x} is evaluated and
|
|---|
| 1046 | its value is returned; otherwise, \var{y} is evaluated and its value is
|
|---|
| 1047 | returned. \versionadded{2.5}
|
|---|
| 1048 |
|
|---|
| 1049 | The expression \code{\var{x} and \var{y}} first evaluates \var{x}; if
|
|---|
| 1050 | \var{x} is false, its value is returned; otherwise, \var{y} is
|
|---|
| 1051 | evaluated and the resulting value is returned.
|
|---|
| 1052 | \opindex{and}
|
|---|
| 1053 |
|
|---|
| 1054 | The expression \code{\var{x} or \var{y}} first evaluates \var{x}; if
|
|---|
| 1055 | \var{x} is true, its value is returned; otherwise, \var{y} is
|
|---|
| 1056 | evaluated and the resulting value is returned.
|
|---|
| 1057 | \opindex{or}
|
|---|
| 1058 |
|
|---|
| 1059 | (Note that neither \keyword{and} nor \keyword{or} restrict the value
|
|---|
| 1060 | and type they return to \code{False} and \code{True}, but rather return the
|
|---|
| 1061 | last evaluated argument.
|
|---|
| 1062 | This is sometimes useful, e.g., if \code{s} is a string that should be
|
|---|
| 1063 | replaced by a default value if it is empty, the expression
|
|---|
| 1064 | \code{s or 'foo'} yields the desired value. Because \keyword{not} has to
|
|---|
| 1065 | invent a value anyway, it does not bother to return a value of the
|
|---|
| 1066 | same type as its argument, so e.g., \code{not 'foo'} yields \code{False},
|
|---|
| 1067 | not \code{''}.)
|
|---|
| 1068 |
|
|---|
| 1069 | \section{Lambdas\label{lambdas}}
|
|---|
| 1070 | \indexii{lambda}{expression}
|
|---|
| 1071 | \indexii{lambda}{form}
|
|---|
| 1072 | \indexii{anonymous}{function}
|
|---|
| 1073 |
|
|---|
| 1074 | \begin{productionlist}
|
|---|
| 1075 | \production{lambda_form}
|
|---|
| 1076 | {"lambda" [\token{parameter_list}]: \token{expression}}
|
|---|
| 1077 | \end{productionlist}
|
|---|
| 1078 |
|
|---|
| 1079 | Lambda forms (lambda expressions) have the same syntactic position as
|
|---|
| 1080 | expressions. They are a shorthand to create anonymous functions; the
|
|---|
| 1081 | expression \code{lambda \var{arguments}: \var{expression}}
|
|---|
| 1082 | yields a function object. The unnamed object behaves like a function
|
|---|
| 1083 | object defined with
|
|---|
| 1084 |
|
|---|
| 1085 | \begin{verbatim}
|
|---|
| 1086 | def name(arguments):
|
|---|
| 1087 | return expression
|
|---|
| 1088 | \end{verbatim}
|
|---|
| 1089 |
|
|---|
| 1090 | See section \ref{function} for the syntax of parameter lists. Note
|
|---|
| 1091 | that functions created with lambda forms cannot contain statements.
|
|---|
| 1092 | \label{lambda}
|
|---|
| 1093 |
|
|---|
| 1094 | \section{Expression lists\label{exprlists}}
|
|---|
| 1095 | \indexii{expression}{list}
|
|---|
| 1096 |
|
|---|
| 1097 | \begin{productionlist}
|
|---|
| 1098 | \production{expression_list}
|
|---|
| 1099 | {\token{expression} ( "," \token{expression} )* [","]}
|
|---|
| 1100 | \end{productionlist}
|
|---|
| 1101 |
|
|---|
| 1102 | An expression list containing at least one comma yields a
|
|---|
| 1103 | tuple. The length of the tuple is the number of expressions in the
|
|---|
| 1104 | list. The expressions are evaluated from left to right.
|
|---|
| 1105 | \obindex{tuple}
|
|---|
| 1106 |
|
|---|
| 1107 | The trailing comma is required only to create a single tuple (a.k.a. a
|
|---|
| 1108 | \emph{singleton}); it is optional in all other cases. A single
|
|---|
| 1109 | expression without a trailing comma doesn't create a
|
|---|
| 1110 | tuple, but rather yields the value of that expression.
|
|---|
| 1111 | (To create an empty tuple, use an empty pair of parentheses:
|
|---|
| 1112 | \code{()}.)
|
|---|
| 1113 | \indexii{trailing}{comma}
|
|---|
| 1114 |
|
|---|
| 1115 | \section{Evaluation order\label{evalorder}}
|
|---|
| 1116 | \indexii{evaluation}{order}
|
|---|
| 1117 |
|
|---|
| 1118 | Python evaluates expressions from left to right. Notice that while
|
|---|
| 1119 | evaluating an assignment, the right-hand side is evaluated before
|
|---|
| 1120 | the left-hand side.
|
|---|
| 1121 |
|
|---|
| 1122 | In the following lines, expressions will be evaluated in the
|
|---|
| 1123 | arithmetic order of their suffixes:
|
|---|
| 1124 |
|
|---|
| 1125 | \begin{verbatim}
|
|---|
| 1126 | expr1, expr2, expr3, expr4
|
|---|
| 1127 | (expr1, expr2, expr3, expr4)
|
|---|
| 1128 | {expr1: expr2, expr3: expr4}
|
|---|
| 1129 | expr1 + expr2 * (expr3 - expr4)
|
|---|
| 1130 | func(expr1, expr2, *expr3, **expr4)
|
|---|
| 1131 | expr3, expr4 = expr1, expr2
|
|---|
| 1132 | \end{verbatim}
|
|---|
| 1133 |
|
|---|
| 1134 | \section{Summary\label{summary}}
|
|---|
| 1135 |
|
|---|
| 1136 | The following table summarizes the operator
|
|---|
| 1137 | precedences\indexii{operator}{precedence} in Python, from lowest
|
|---|
| 1138 | precedence (least binding) to highest precedence (most binding).
|
|---|
| 1139 | Operators in the same box have the same precedence. Unless the syntax
|
|---|
| 1140 | is explicitly given, operators are binary. Operators in the same box
|
|---|
| 1141 | group left to right (except for comparisons, including tests, which all
|
|---|
| 1142 | have the same precedence and chain from left to right --- see section
|
|---|
| 1143 | \ref{comparisons} -- and exponentiation, which groups from right to left).
|
|---|
| 1144 |
|
|---|
| 1145 | \begin{tableii}{c|l}{textrm}{Operator}{Description}
|
|---|
| 1146 | \lineii{\keyword{lambda}} {Lambda expression}
|
|---|
| 1147 | \hline
|
|---|
| 1148 | \lineii{\keyword{or}} {Boolean OR}
|
|---|
| 1149 | \hline
|
|---|
| 1150 | \lineii{\keyword{and}} {Boolean AND}
|
|---|
| 1151 | \hline
|
|---|
| 1152 | \lineii{\keyword{not} \var{x}} {Boolean NOT}
|
|---|
| 1153 | \hline
|
|---|
| 1154 | \lineii{\keyword{in}, \keyword{not} \keyword{in}}{Membership tests}
|
|---|
| 1155 | \lineii{\keyword{is}, \keyword{is not}}{Identity tests}
|
|---|
| 1156 | \lineii{\code{<}, \code{<=}, \code{>}, \code{>=},
|
|---|
| 1157 | \code{<>}, \code{!=}, \code{==}}
|
|---|
| 1158 | {Comparisons}
|
|---|
| 1159 | \hline
|
|---|
| 1160 | \lineii{\code{|}} {Bitwise OR}
|
|---|
| 1161 | \hline
|
|---|
| 1162 | \lineii{\code{\^}} {Bitwise XOR}
|
|---|
| 1163 | \hline
|
|---|
| 1164 | \lineii{\code{\&}} {Bitwise AND}
|
|---|
| 1165 | \hline
|
|---|
| 1166 | \lineii{\code{<<}, \code{>>}} {Shifts}
|
|---|
| 1167 | \hline
|
|---|
| 1168 | \lineii{\code{+}, \code{-}}{Addition and subtraction}
|
|---|
| 1169 | \hline
|
|---|
| 1170 | \lineii{\code{*}, \code{/}, \code{\%}}
|
|---|
| 1171 | {Multiplication, division, remainder}
|
|---|
| 1172 | \hline
|
|---|
| 1173 | \lineii{\code{+\var{x}}, \code{-\var{x}}} {Positive, negative}
|
|---|
| 1174 | \lineii{\code{\~\var{x}}} {Bitwise not}
|
|---|
| 1175 | \hline
|
|---|
| 1176 | \lineii{\code{**}} {Exponentiation}
|
|---|
| 1177 | \hline
|
|---|
| 1178 | \lineii{\code{\var{x}.\var{attribute}}} {Attribute reference}
|
|---|
| 1179 | \lineii{\code{\var{x}[\var{index}]}} {Subscription}
|
|---|
| 1180 | \lineii{\code{\var{x}[\var{index}:\var{index}]}} {Slicing}
|
|---|
| 1181 | \lineii{\code{\var{f}(\var{arguments}...)}} {Function call}
|
|---|
| 1182 | \hline
|
|---|
| 1183 | \lineii{\code{(\var{expressions}\ldots)}} {Binding or tuple display}
|
|---|
| 1184 | \lineii{\code{[\var{expressions}\ldots]}} {List display}
|
|---|
| 1185 | \lineii{\code{\{\var{key}:\var{datum}\ldots\}}}{Dictionary display}
|
|---|
| 1186 | \lineii{\code{`\var{expressions}\ldots`}} {String conversion}
|
|---|
| 1187 | \end{tableii}
|
|---|