Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/reference/expressions.rst

    r2 r391  
    6666   atom: `identifier` | `literal` | `enclosure`
    6767   enclosure: `parenth_form` | `list_display`
    68             : | `generator_expression` | `dict_display`
     68            : | `generator_expression` | `dict_display` | `set_display`
    6969            : | `string_conversion` | `yield_atom`
    7070
     
    9797or more underscores, it is considered a :dfn:`private name` of that class.
    9898Private 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.
     99them.  The transformation inserts the class name, with leading underscores
     100removed and a single underscore inserted, in front of the name.  For example,
     101the identifier ``__spam`` occurring in a class named ``Ham`` will be transformed
     102to ``_Ham__spam``.  This transformation is independent of the syntactical
     103context in which the identifier is used.  If the transformed name is extremely
     104long (longer than 255 characters), implementation defined truncation may happen.
     105If the class name consists only of underscores, no transformation is done.
    107106
    108107
     
    186185   list_for: "for" `target_list` "in" `old_expression_list` [`list_iter`]
    187186   old_expression_list: `old_expression` [("," `old_expression`)+ [","]]
     187   old_expression: `or_test` | `old_lambda_expr`
    188188   list_iter: `list_for` | `list_if`
    189189   list_if: "if" `old_expression` [`list_iter`]
     
    206206
    207207
     208.. _comprehensions:
     209
     210Displays for sets and dictionaries
     211----------------------------------
     212
     213For constructing a set or a dictionary Python provides special syntax
     214called "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
     221Common 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
     229The 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.
     231In this case, the elements of the new container are those that would be produced
     232by considering each of the :keyword:`for` or :keyword:`if` clauses a block,
     233nesting from left to right, and evaluating the expression to produce an element
     234each time the innermost block is reached.
     235
     236Note that the comprehension is executed in a separate scope, so names assigned
     237to in the target list don't "leak" in the enclosing scope.
     238
     239
    208240.. _genexpr:
    209241
     
    212244
    213245.. index:: pair: generator; expression
     246           object: generator
    214247
    215248A generator expression is a compact generator notation in parentheses:
    216249
    217250.. 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
     253A generator expression yields a new generator object.  Its syntax is the same as
     254for comprehensions, except that it is enclosed in parentheses instead of
     255brackets or curly braces.
     256
     257Variables used in the generator expression are evaluated lazily when the
     258:meth:`__next__` method is called for generator object (in the same fashion as
     259normal generators).  However, the leftmost :keyword:`for` clause is immediately
     260evaluated, so that an error produced by it can be seen before any other possible
    238261error in the code that handles the generator expression.  Subsequent
    239 :keyword:`for` and :keyword:`if` clauses cannot be evaluated immediately since
    240 they may depend on the previous :keyword:`for` loop.  For example:
    241 ``(x*y for x in range(10) for y in bar(x))``.
    242 
    243 The parentheses can be omitted on calls with only one argument. See section
     262:keyword:`for` clauses cannot be evaluated immediately since they may depend on
     263the previous :keyword:`for` loop. For example: ``(x*y for x in range(10) for y
     264in bar(x))``.
     265
     266The parentheses can be omitted on calls with only one argument.  See section
    244267:ref:`calls` for the detail.
    245 
    246268
    247269.. _dict:
     
    251273
    252274.. 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
    258277
    259278A dictionary display is a possibly empty series of key/datum pairs enclosed in
     
    261280
    262281.. productionlist::
    263    dict_display: "{" [`key_datum_list`] "}"
     282   dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}"
    264283   key_datum_list: `key_datum` ("," `key_datum`)* [","]
    265284   key_datum: `expression` ":" `expression`
    266 
    267 .. index:: object: dictionary
     285   dict_comprehension: `expression` ":" `expression` `comp_for`
    268286
    269287A dictionary display yields a new dictionary object.
    270288
    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.
     289If a comma-separated sequence of key/datum pairs is given, they are evaluated
     290from left to right to define the entries of the dictionary: each key object is
     291used as a key into the dictionary to store the corresponding datum.  This means
     292that you can specify the same key multiple times in the key/datum list, and the
     293final dictionary's value for that key will be the last one given.
     294
     295A dict comprehension, in contrast to list and set comprehensions, needs two
     296expressions separated with a colon followed by the usual "for" and "if" clauses.
     297When the comprehension is run, the resulting key and value elements are inserted
     298in the new dictionary in the order they are produced.
    274299
    275300.. index:: pair: immutable; object
     301           hashable
    276302
    277303Restrictions on the types of the key values are listed earlier in section
     
    282308
    283309
     310.. _set:
     311
     312Set displays
     313------------
     314
     315.. index:: pair: set; display
     316           object: set
     317
     318A set display is denoted by curly braces and distinguishable from dictionary
     319displays by the lack of colons separating keys and values:
     320
     321.. productionlist::
     322   set_display: "{" (`expression_list` | `comprehension`) "}"
     323
     324A set display yields a new mutable set object, the contents being specified by
     325either a sequence of expressions or a comprehension.  When a comma-separated
     326list of expressions is supplied, its elements are evaluated from left to right
     327and added to the set object.  When a comprehension is supplied, the set is
     328constructed from the elements resulting from the comprehension.
     329
     330An empty set cannot be constructed with ``{}``; this literal constructs an empty
     331dictionary.
     332
     333
    284334.. _string-conversions:
    285335
     
    297347
    298348.. productionlist::
    299    string_conversion: "'" `expression_list` "'"
     349   string_conversion: "`" `expression_list` "`"
    300350
    301351A string conversion evaluates the contained expression list and converts the
     
    367417suspended.  The only difference is that a generator function cannot control
    368418where should the execution continue after it yields; the control is always
    369 transfered to the generator's caller.
     419transferred to the generator's caller.
    370420
    371421.. index:: object: generator
    372422
    373 The following generator's methods can be used to control the execution of a
    374 generator function:
     423
     424Generator-iterator methods
     425^^^^^^^^^^^^^^^^^^^^^^^^^^
     426
     427This subsection describes the methods of a generator iterator.  They can
     428be used to control the execution of a generator function.
     429
     430Note that calling any of the generator methods below when the generator
     431is already executing raises a :exc:`ValueError` exception.
    375432
    376433.. index:: exception: StopIteration
     434.. class:: generator
    377435
    378436
     
    388446   raised.
    389447
     448.. class:: .
    390449
    391450.. method:: generator.send(value)
     
    604663that is an expression is that expression.  The conversion of an ellipsis slice
    605664item 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 
     665slice object (see section :ref:`types`) whose :attr:`~slice.start`,
     666:attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the
     667expressions given as lower bound, upper bound and stride, respectively,
     668substituting ``None`` for missing expressions.
     669
     670
     671.. index::
     672   object: callable
     673   single: call
     674   single: argument; call semantics
    611675
    612676.. _calls:
     
    615679-----
    616680
    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:
     681A call calls a callable object (e.g., a :term:`function`) with a possibly empty
     682series of :term:`arguments <argument>`:
    623683
    624684.. productionlist::
     
    639699does not affect the semantics.
    640700
     701.. index::
     702   single: parameter; call semantics
     703
    641704The primary must evaluate to a callable object (user-defined functions, built-in
    642705functions, methods of built-in objects, class objects, methods of class
     
    644707define additional callable object types).  All argument expressions are
    645708evaluated before the call is attempted.  Please refer to section :ref:`function`
    646 for the syntax of formal parameter lists.
     709for the syntax of formal :term:`parameter` lists.
    647710
    648711If keyword arguments are present, they are first converted to positional
     
    669732   do not have names, even if they are 'named' for the purpose of documentation,
    670733   and which therefore cannot be supplied by keyword.  In CPython, this is the
    671    case for functions implemented in C that use :cfunc:`PyArg_ParseTuple` to
     734   case for functions implemented in C that use :c:func:`PyArg_ParseTuple` to
    672735   parse their arguments.
    673736
     
    685748there were no excess keyword arguments.
    686749
     750.. index::
     751   single: *; in function calls
     752
    687753If the syntax ``*expression`` appears in the function call, ``expression`` must
    688 evaluate to a sequence.  Elements from this sequence are treated as if they were
    689 additional positional arguments; if there are positional arguments *x1*,...,
    690 *xN*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, this is
    691 equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, ...,
    692 *yM*.
     754evaluate to an iterable.  Elements from this iterable are treated as if they
     755were additional positional arguments; if there are positional arguments
     756*x1*, ..., *xN*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, this
     757is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*,
     758..., *yM*.
    693759
    694760A consequence of this is that although the ``*expression`` syntax may appear
     
    711777used in the same call, so in practice this confusion does not arise.
    712778
     779.. index::
     780   single: **; in function calls
     781
    713782If the syntax ``**expression`` appears in the function call, ``expression`` must
    714783evaluate to a mapping, the contents of which are treated as additional keyword
     
    9501019counts raise a :exc:`ValueError` exception.
    9511020
     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.
    9521026
    9531027.. _bitwise:
     
    9871061.. _comparisons:
    9881062.. _is:
    989 .. _isnot:
     1063.. _is not:
    9901064.. _in:
    991 .. _notin:
     1065.. _not in:
    9921066
    9931067Comparisons
     
    11371211   pair: Boolean; operation
    11381212
    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::
    11451214   or_test: `and_test` | `or_test` "or" `and_test`
    11461215   and_test: `not_test` | `and_test` "and" `not_test`
     
    11581227The operator :keyword:`not` yields ``True`` if its argument is false, ``False``
    11591228otherwise.
    1160 
    1161 The expression ``x if C else y`` first evaluates *C* (*not* *x*); if *C* is
    1162 true, *x* is evaluated and its value is returned; otherwise, *y* is evaluated
    1163 and its value is returned.
    1164 
    1165 .. versionadded:: 2.5
    11661229
    11671230.. index:: operator: and
     
    11841247
    11851248
     1249Conditional 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
     1262Conditional expressions (sometimes called a "ternary operator") have the lowest
     1263priority of all Python operations.
     1264
     1265The expression ``x if C else y`` first evaluates the condition, *C* (*not* *x*);
     1266if *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
     1267evaluated and its value is returned.
     1268
     1269See :pep:`308` for more details about conditional expressions.
     1270
     1271
    11861272.. _lambdas:
    11871273.. _lambda:
     
    11921278.. index::
    11931279   pair: lambda; expression
    1194    pair: lambda; form
    11951280   pair: anonymous; function
    11961281
    11971282.. 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 as
     1283   lambda_expr: "lambda" [`parameter_list`]: `expression`
     1284   old_lambda_expr: "lambda" [`parameter_list`]: `old_expression`
     1285
     1286Lambda expressions (sometimes called lambda forms) have the same syntactic position as
    12021287expressions.  They are a shorthand to create anonymous functions; the expression
    12031288``lambda arguments: expression`` yields a function object.  The unnamed object
     
    12081293
    12091294See section :ref:`function` for the syntax of parameter lists.  Note that
    1210 functions created with lambda forms cannot contain statements.
     1295functions created with lambda expressions cannot contain statements.
    12111296
    12121297
     
    12591344.. _operator-summary:
    12601345
    1261 Summary
    1262 =======
     1346Operator precedence
     1347===================
    12631348
    12641349.. index:: pair: operator; precedence
     
    12771362| :keyword:`lambda`                             | Lambda expression                   |
    12781363+-----------------------------------------------+-------------------------------------+
     1364| :keyword:`if` -- :keyword:`else`              | Conditional expression              |
     1365+-----------------------------------------------+-------------------------------------+
    12791366| :keyword:`or`                                 | Boolean OR                          |
    12801367+-----------------------------------------------+-------------------------------------+
    12811368| :keyword:`and`                                | Boolean AND                         |
    12821369+-----------------------------------------------+-------------------------------------+
    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            |
    12871374| ``<=``, ``>``, ``>=``, ``<>``, ``!=``, ``==`` |                                     |
    12881375+-----------------------------------------------+-------------------------------------+
     
    12981385+-----------------------------------------------+-------------------------------------+
    12991386| ``*``, ``/``, ``//``, ``%``                   | Multiplication, division, remainder |
     1387|                                               | [#]_                                |
    13001388+-----------------------------------------------+-------------------------------------+
    13011389| ``+x``, ``-x``, ``~x``                        | Positive, negative, bitwise NOT     |
     
    13081396| ``(expressions...)``,                         | Binding or tuple display,           |
    13091397| ``[expressions...]``,                         | list display,                       |
    1310 | ``{key:datum...}``,                           | dictionary display,                 |
     1398| ``{key: value...}``,                          | dictionary display,                 |
    13111399| ```expressions...```                          | string conversion                   |
    13121400+-----------------------------------------------+-------------------------------------+
     
    13161404.. [#] In Python 2.3 and later releases, a list comprehension "leaks" the control
    13171405   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.0
     1406   behavior is deprecated, and relying on it will not work in Python 3.
    13191407
    13201408.. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be
     
    13221410   a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 %
    13231411   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` module returns a result whose sign matches the sign of the
     1412   1e100``, which is numerically exactly equal to ``1e100``.  The function
     1413   :func:`math.fmod` returns a result whose sign matches the sign of the
    13261414   first argument instead, and so returns ``-1e-100`` in this case. Which approach
    13271415   is more appropriate depends on the application.
     
    13361424   strings ``u"\u00C7"`` and ``u"\u0043\u0327"`` compare differently,
    13371425   even though they both represent the same unicode character (LATIN
    1338    CAPTITAL LETTER C WITH CEDILLA). To compare strings in a human
     1426   CAPITAL LETTER C WITH CEDILLA). To compare strings in a human
    13391427   recognizable way, compare using :func:`unicodedata.normalize`.
    13401428
     
    13531441   methods, or constants.  Check their documentation for more info.
    13541442
     1443.. [#] The ``%`` operator is also used for string formatting; the same
     1444   precedence applies.
     1445
    13551446.. [#] The power operator ``**`` binds less tightly than an arithmetic or
    13561447   bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.
Note: See TracChangeset for help on using the changeset viewer.