Changeset 391 for python/trunk/Doc/reference/expressions.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/reference/expressions.rst
r2 r391 66 66 atom: `identifier` | `literal` | `enclosure` 67 67 enclosure: `parenth_form` | `list_display` 68 : | `generator_expression` | `dict_display` 68 : | `generator_expression` | `dict_display` | `set_display` 69 69 : | `string_conversion` | `yield_atom` 70 70 … … 97 97 or more underscores, it is considered a :dfn:`private name` of that class. 98 98 Private names are transformed to a longer form before code is generated for 99 them. The transformation inserts the class name in front of the name, with 100 leading underscores removed, and a single underscore inserted in front of the 101 class name. For example, the identifier ``__spam`` occurring in a class named 102 ``Ham`` will be transformed to ``_Ham__spam``. This transformation is 103 independent of the syntactical context in which the identifier is used. If the 104 transformed name is extremely long (longer than 255 characters), implementation 105 defined truncation may happen. If the class name consists only of underscores, 106 no transformation is done. 99 them. The transformation inserts the class name, with leading underscores 100 removed and a single underscore inserted, in front of the name. For example, 101 the identifier ``__spam`` occurring in a class named ``Ham`` will be transformed 102 to ``_Ham__spam``. This transformation is independent of the syntactical 103 context in which the identifier is used. If the transformed name is extremely 104 long (longer than 255 characters), implementation defined truncation may happen. 105 If the class name consists only of underscores, no transformation is done. 107 106 108 107 … … 186 185 list_for: "for" `target_list` "in" `old_expression_list` [`list_iter`] 187 186 old_expression_list: `old_expression` [("," `old_expression`)+ [","]] 187 old_expression: `or_test` | `old_lambda_expr` 188 188 list_iter: `list_for` | `list_if` 189 189 list_if: "if" `old_expression` [`list_iter`] … … 206 206 207 207 208 .. _comprehensions: 209 210 Displays for sets and dictionaries 211 ---------------------------------- 212 213 For constructing a set or a dictionary Python provides special syntax 214 called "displays", each of them in two flavors: 215 216 * either the container contents are listed explicitly, or 217 218 * they are computed via a set of looping and filtering instructions, called a 219 :dfn:`comprehension`. 220 221 Common syntax elements for comprehensions are: 222 223 .. productionlist:: 224 comprehension: `expression` `comp_for` 225 comp_for: "for" `target_list` "in" `or_test` [`comp_iter`] 226 comp_iter: `comp_for` | `comp_if` 227 comp_if: "if" `expression_nocond` [`comp_iter`] 228 229 The comprehension consists of a single expression followed by at least one 230 :keyword:`for` clause and zero or more :keyword:`for` or :keyword:`if` clauses. 231 In this case, the elements of the new container are those that would be produced 232 by considering each of the :keyword:`for` or :keyword:`if` clauses a block, 233 nesting from left to right, and evaluating the expression to produce an element 234 each time the innermost block is reached. 235 236 Note that the comprehension is executed in a separate scope, so names assigned 237 to in the target list don't "leak" in the enclosing scope. 238 239 208 240 .. _genexpr: 209 241 … … 212 244 213 245 .. index:: pair: generator; expression 246 object: generator 214 247 215 248 A generator expression is a compact generator notation in parentheses: 216 249 217 250 .. productionlist:: 218 generator_expression: "(" `expression` `genexpr_for` ")" 219 genexpr_for: "for" `target_list` "in" `or_test` [`genexpr_iter`] 220 genexpr_iter: `genexpr_for` | `genexpr_if` 221 genexpr_if: "if" `old_expression` [`genexpr_iter`] 222 223 .. index:: object: generator 224 225 A generator expression yields a new generator object. It consists of a single 226 expression followed by at least one :keyword:`for` clause and zero or more 227 :keyword:`for` or :keyword:`if` clauses. The iterating values of the new 228 generator are those that would be produced by considering each of the 229 :keyword:`for` or :keyword:`if` clauses a block, nesting from left to right, and 230 evaluating the expression to yield a value that is reached the innermost block 231 for each iteration. 232 233 Variables used in the generator expression are evaluated lazily in a separate 234 scope when the :meth:`next` method is called for the generator object (in the 235 same fashion as for normal generators). However, the :keyword:`in` expression 236 of the leftmost :keyword:`for` clause is immediately evaluated in the current 237 scope so that an error produced by it can be seen before any other possible 251 generator_expression: "(" `expression` `comp_for` ")" 252 253 A generator expression yields a new generator object. Its syntax is the same as 254 for comprehensions, except that it is enclosed in parentheses instead of 255 brackets or curly braces. 256 257 Variables used in the generator expression are evaluated lazily when the 258 :meth:`__next__` method is called for generator object (in the same fashion as 259 normal generators). However, the leftmost :keyword:`for` clause is immediately 260 evaluated, so that an error produced by it can be seen before any other possible 238 261 error in the code that handles the generator expression. Subsequent 239 :keyword:`for` and :keyword:`if` clauses cannot be evaluated immediately since240 the y may depend on the previous :keyword:`for` loop. For example:241 ``(x*y for x in range(10) for yin bar(x))``.242 243 The parentheses can be omitted on calls with only one argument. See section262 :keyword:`for` clauses cannot be evaluated immediately since they may depend on 263 the previous :keyword:`for` loop. For example: ``(x*y for x in range(10) for y 264 in bar(x))``. 265 266 The parentheses can be omitted on calls with only one argument. See section 244 267 :ref:`calls` for the detail. 245 246 268 247 269 .. _dict: … … 251 273 252 274 .. index:: pair: dictionary; display 253 254 .. index:: 255 single: key 256 single: datum 257 single: key/datum pair 275 key, datum, key/datum pair 276 object: dictionary 258 277 259 278 A dictionary display is a possibly empty series of key/datum pairs enclosed in … … 261 280 262 281 .. productionlist:: 263 dict_display: "{" [`key_datum_list` ] "}"282 dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}" 264 283 key_datum_list: `key_datum` ("," `key_datum`)* [","] 265 284 key_datum: `expression` ":" `expression` 266 267 .. index:: object: dictionary 285 dict_comprehension: `expression` ":" `expression` `comp_for` 268 286 269 287 A dictionary display yields a new dictionary object. 270 288 271 The key/datum pairs are evaluated from left to right to define the entries of 272 the dictionary: each key object is used as a key into the dictionary to store 273 the corresponding datum. 289 If a comma-separated sequence of key/datum pairs is given, they are evaluated 290 from left to right to define the entries of the dictionary: each key object is 291 used as a key into the dictionary to store the corresponding datum. This means 292 that you can specify the same key multiple times in the key/datum list, and the 293 final dictionary's value for that key will be the last one given. 294 295 A dict comprehension, in contrast to list and set comprehensions, needs two 296 expressions separated with a colon followed by the usual "for" and "if" clauses. 297 When the comprehension is run, the resulting key and value elements are inserted 298 in the new dictionary in the order they are produced. 274 299 275 300 .. index:: pair: immutable; object 301 hashable 276 302 277 303 Restrictions on the types of the key values are listed earlier in section … … 282 308 283 309 310 .. _set: 311 312 Set displays 313 ------------ 314 315 .. index:: pair: set; display 316 object: set 317 318 A set display is denoted by curly braces and distinguishable from dictionary 319 displays by the lack of colons separating keys and values: 320 321 .. productionlist:: 322 set_display: "{" (`expression_list` | `comprehension`) "}" 323 324 A set display yields a new mutable set object, the contents being specified by 325 either a sequence of expressions or a comprehension. When a comma-separated 326 list of expressions is supplied, its elements are evaluated from left to right 327 and added to the set object. When a comprehension is supplied, the set is 328 constructed from the elements resulting from the comprehension. 329 330 An empty set cannot be constructed with ``{}``; this literal constructs an empty 331 dictionary. 332 333 284 334 .. _string-conversions: 285 335 … … 297 347 298 348 .. productionlist:: 299 string_conversion: " '" `expression_list` "'"349 string_conversion: "`" `expression_list` "`" 300 350 301 351 A string conversion evaluates the contained expression list and converts the … … 367 417 suspended. The only difference is that a generator function cannot control 368 418 where should the execution continue after it yields; the control is always 369 transfer ed to the generator's caller.419 transferred to the generator's caller. 370 420 371 421 .. index:: object: generator 372 422 373 The following generator's methods can be used to control the execution of a 374 generator function: 423 424 Generator-iterator methods 425 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 426 427 This subsection describes the methods of a generator iterator. They can 428 be used to control the execution of a generator function. 429 430 Note that calling any of the generator methods below when the generator 431 is already executing raises a :exc:`ValueError` exception. 375 432 376 433 .. index:: exception: StopIteration 434 .. class:: generator 377 435 378 436 … … 388 446 raised. 389 447 448 .. class:: . 390 449 391 450 .. method:: generator.send(value) … … 604 663 that is an expression is that expression. The conversion of an ellipsis slice 605 664 item is the built-in ``Ellipsis`` object. The conversion of a proper slice is a 606 slice object (see section :ref:`types`) whose :attr:`start`, :attr:`stop` and 607 :attr:`step` attributes are the values of the expressions given as lower bound, 608 upper bound and stride, respectively, substituting ``None`` for missing 609 expressions. 610 665 slice object (see section :ref:`types`) whose :attr:`~slice.start`, 666 :attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the 667 expressions given as lower bound, upper bound and stride, respectively, 668 substituting ``None`` for missing expressions. 669 670 671 .. index:: 672 object: callable 673 single: call 674 single: argument; call semantics 611 675 612 676 .. _calls: … … 615 679 ----- 616 680 617 .. index:: single: call 618 619 .. index:: object: callable 620 621 A call calls a callable object (e.g., a function) with a possibly empty series 622 of arguments: 681 A call calls a callable object (e.g., a :term:`function`) with a possibly empty 682 series of :term:`arguments <argument>`: 623 683 624 684 .. productionlist:: … … 639 699 does not affect the semantics. 640 700 701 .. index:: 702 single: parameter; call semantics 703 641 704 The primary must evaluate to a callable object (user-defined functions, built-in 642 705 functions, methods of built-in objects, class objects, methods of class … … 644 707 define additional callable object types). All argument expressions are 645 708 evaluated before the call is attempted. Please refer to section :ref:`function` 646 for the syntax of formal parameterlists.709 for the syntax of formal :term:`parameter` lists. 647 710 648 711 If keyword arguments are present, they are first converted to positional … … 669 732 do not have names, even if they are 'named' for the purpose of documentation, 670 733 and which therefore cannot be supplied by keyword. In CPython, this is the 671 case for functions implemented in C that use :c func:`PyArg_ParseTuple` to734 case for functions implemented in C that use :c:func:`PyArg_ParseTuple` to 672 735 parse their arguments. 673 736 … … 685 748 there were no excess keyword arguments. 686 749 750 .. index:: 751 single: *; in function calls 752 687 753 If the syntax ``*expression`` appears in the function call, ``expression`` must 688 evaluate to a sequence. Elements from this sequence are treated as if they were689 additional positional arguments; if there are positional arguments *x1*,..., 690 *x N*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, thisis691 equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, ...,692 *yM*.754 evaluate to an iterable. Elements from this iterable are treated as if they 755 were additional positional arguments; if there are positional arguments 756 *x1*, ..., *xN*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, this 757 is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, 758 ..., *yM*. 693 759 694 760 A consequence of this is that although the ``*expression`` syntax may appear … … 711 777 used in the same call, so in practice this confusion does not arise. 712 778 779 .. index:: 780 single: **; in function calls 781 713 782 If the syntax ``**expression`` appears in the function call, ``expression`` must 714 783 evaluate to a mapping, the contents of which are treated as additional keyword … … 950 1019 counts raise a :exc:`ValueError` exception. 951 1020 1021 .. note:: 1022 1023 In the current implementation, the right-hand operand is required 1024 to be at most :attr:`sys.maxsize`. If the right-hand operand is larger than 1025 :attr:`sys.maxsize` an :exc:`OverflowError` exception is raised. 952 1026 953 1027 .. _bitwise: … … 987 1061 .. _comparisons: 988 1062 .. _is: 989 .. _is not:1063 .. _is not: 990 1064 .. _in: 991 .. _not in:1065 .. _not in: 992 1066 993 1067 Comparisons … … 1137 1211 pair: Boolean; operation 1138 1212 1139 Boolean operations have the lowest priority of all Python operations: 1140 1141 .. productionlist:: 1142 expression: `conditional_expression` | `lambda_form` 1143 old_expression: `or_test` | `old_lambda_form` 1144 conditional_expression: `or_test` ["if" `or_test` "else" `expression`] 1213 .. productionlist:: 1145 1214 or_test: `and_test` | `or_test` "or" `and_test` 1146 1215 and_test: `not_test` | `and_test` "and" `not_test` … … 1158 1227 The operator :keyword:`not` yields ``True`` if its argument is false, ``False`` 1159 1228 otherwise. 1160 1161 The expression ``x if C else y`` first evaluates *C* (*not* *x*); if *C* is1162 true, *x* is evaluated and its value is returned; otherwise, *y* is evaluated1163 and its value is returned.1164 1165 .. versionadded:: 2.51166 1229 1167 1230 .. index:: operator: and … … 1184 1247 1185 1248 1249 Conditional Expressions 1250 ======================= 1251 1252 .. versionadded:: 2.5 1253 1254 .. index:: 1255 pair: conditional; expression 1256 pair: ternary; operator 1257 1258 .. productionlist:: 1259 conditional_expression: `or_test` ["if" `or_test` "else" `expression`] 1260 expression: `conditional_expression` | `lambda_expr` 1261 1262 Conditional expressions (sometimes called a "ternary operator") have the lowest 1263 priority of all Python operations. 1264 1265 The expression ``x if C else y`` first evaluates the condition, *C* (*not* *x*); 1266 if *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is 1267 evaluated and its value is returned. 1268 1269 See :pep:`308` for more details about conditional expressions. 1270 1271 1186 1272 .. _lambdas: 1187 1273 .. _lambda: … … 1192 1278 .. index:: 1193 1279 pair: lambda; expression 1194 pair: lambda; form1195 1280 pair: anonymous; function 1196 1281 1197 1282 .. productionlist:: 1198 lambda_ form: "lambda" [`parameter_list`]: `expression`1199 old_lambda_ form: "lambda" [`parameter_list`]: `old_expression`1200 1201 Lambda forms (lambda expressions) have the same syntactic position as1283 lambda_expr: "lambda" [`parameter_list`]: `expression` 1284 old_lambda_expr: "lambda" [`parameter_list`]: `old_expression` 1285 1286 Lambda expressions (sometimes called lambda forms) have the same syntactic position as 1202 1287 expressions. They are a shorthand to create anonymous functions; the expression 1203 1288 ``lambda arguments: expression`` yields a function object. The unnamed object … … 1208 1293 1209 1294 See section :ref:`function` for the syntax of parameter lists. Note that 1210 functions created with lambda forms cannot contain statements.1295 functions created with lambda expressions cannot contain statements. 1211 1296 1212 1297 … … 1259 1344 .. _operator-summary: 1260 1345 1261 Summary 1262 ======= 1346 Operator precedence 1347 =================== 1263 1348 1264 1349 .. index:: pair: operator; precedence … … 1277 1362 | :keyword:`lambda` | Lambda expression | 1278 1363 +-----------------------------------------------+-------------------------------------+ 1364 | :keyword:`if` -- :keyword:`else` | Conditional expression | 1365 +-----------------------------------------------+-------------------------------------+ 1279 1366 | :keyword:`or` | Boolean OR | 1280 1367 +-----------------------------------------------+-------------------------------------+ 1281 1368 | :keyword:`and` | Boolean AND | 1282 1369 +-----------------------------------------------+-------------------------------------+ 1283 | :keyword:`not` *x*| Boolean NOT |1284 +-----------------------------------------------+-------------------------------------+ 1285 | :keyword:`in`, :keyword:`not ` :keyword:`in`,| Comparisons, including membership |1286 | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests ,|1370 | :keyword:`not` ``x`` | Boolean NOT | 1371 +-----------------------------------------------+-------------------------------------+ 1372 | :keyword:`in`, :keyword:`not in`, | Comparisons, including membership | 1373 | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests | 1287 1374 | ``<=``, ``>``, ``>=``, ``<>``, ``!=``, ``==`` | | 1288 1375 +-----------------------------------------------+-------------------------------------+ … … 1298 1385 +-----------------------------------------------+-------------------------------------+ 1299 1386 | ``*``, ``/``, ``//``, ``%`` | Multiplication, division, remainder | 1387 | | [#]_ | 1300 1388 +-----------------------------------------------+-------------------------------------+ 1301 1389 | ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT | … … 1308 1396 | ``(expressions...)``, | Binding or tuple display, | 1309 1397 | ``[expressions...]``, | list display, | 1310 | ``{key: datum...}``,| dictionary display, |1398 | ``{key: value...}``, | dictionary display, | 1311 1399 | ```expressions...``` | string conversion | 1312 1400 +-----------------------------------------------+-------------------------------------+ … … 1316 1404 .. [#] In Python 2.3 and later releases, a list comprehension "leaks" the control 1317 1405 variables of each ``for`` it contains into the containing scope. However, this 1318 behavior is deprecated, and relying on it will not work in Python 3. 01406 behavior is deprecated, and relying on it will not work in Python 3. 1319 1407 1320 1408 .. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be … … 1322 1410 a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 % 1323 1411 1e100`` have the same sign as ``1e100``, the computed result is ``-1e-100 + 1324 1e100``, which is numerically exactly equal to ``1e100``. Function :func:`fmod`1325 in the :mod:`math` modulereturns a result whose sign matches the sign of the1412 1e100``, which is numerically exactly equal to ``1e100``. The function 1413 :func:`math.fmod` returns a result whose sign matches the sign of the 1326 1414 first argument instead, and so returns ``-1e-100`` in this case. Which approach 1327 1415 is more appropriate depends on the application. … … 1336 1424 strings ``u"\u00C7"`` and ``u"\u0043\u0327"`` compare differently, 1337 1425 even though they both represent the same unicode character (LATIN 1338 CAP TITAL LETTER C WITH CEDILLA). To compare strings in a human1426 CAPITAL LETTER C WITH CEDILLA). To compare strings in a human 1339 1427 recognizable way, compare using :func:`unicodedata.normalize`. 1340 1428 … … 1353 1441 methods, or constants. Check their documentation for more info. 1354 1442 1443 .. [#] The ``%`` operator is also used for string formatting; the same 1444 precedence applies. 1445 1355 1446 .. [#] The power operator ``**`` binds less tightly than an arithmetic or 1356 1447 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.
Note:
See TracChangeset
for help on using the changeset viewer.