• Home
  • Raw
  • Download

Lines Matching +full:is +full:- +full:generator +full:- +full:function

36 coercion rules listed at  :ref:`coercion-rules`.  If both arguments are standard
39 * If either argument is a complex number, the other is converted to complex;
41 * otherwise, if either argument is a floating point number, the other is
44 * otherwise, if either argument is a long integer, the other is converted to
47 * otherwise, both must be plain integers and no conversion is necessary.
63 atoms is:
72 .. _atom-identifiers:
75 -------------------
81 An identifier occurring as an atom is a name. See section :ref:`identifiers`
87 When the name is bound to an object, evaluation of the atom yields that object.
88 When a name is not bound, an attempt to evaluate it raises a :exc:`NameError`
97 or more underscores, it is considered a :dfn:`private name` of that class.
98 Private names are transformed to a longer form before code is generated for
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
105 If the class name consists only of underscores, no transformation is done.
109 .. _atom-literals:
112 --------
132 is less important than its value. Multiple evaluations of literals with the
141 -------------------
145 A parenthesized form is an optional expression list enclosed in parentheses:
165 comma operator. The exception is the empty tuple, for which parentheses *are*
166 required --- allowing unparenthesized "nothing" in expressions would cause
173 -------------
179 A list display is a possibly empty series of expressions enclosed in square
198 comma-separated list of expressions is supplied, its elements are evaluated from
200 comprehension is supplied, it consists of a single expression followed by at
205 list element each time the innermost block is reached [#]_.
211 ----------------------------------
234 each time the innermost block is reached.
236 Note that the comprehension is executed in a separate scope, so names assigned
242 Generator expressions
243 ---------------------
245 .. index:: pair: generator; expression
246 object: generator
248 A generator expression is a compact generator notation in parentheses:
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
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
261 error in the code that handles the generator expression. Subsequent
272 -------------------
278 A dictionary display is a possibly empty series of key/datum pairs enclosed in
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
297 When the comprehension is run, the resulting key and value elements are inserted
313 ------------
318 A set display is denoted by curly braces and distinguishable from dictionary
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
334 .. _string-conversions:
337 ------------------
343 single: back-quotes
345 A string conversion is an expression list enclosed in reverse (a.k.a. backward)
354 If the object is a string, a number, ``None``, or a tuple, list or dictionary
355 containing only objects whose type is one of these, the resulting string is a
356 valid Python expression which can be passed to the built-in function
374 The built-in function :func:`repr` performs exactly the same conversion in its
375 argument as enclosing it in parentheses and reverse quotes does. The built-in
376 function :func:`str` performs a similar but more user-friendly conversion.
382 -----------------
387 pair: generator; function
395 The :keyword:`yield` expression is only used when defining a generator function,
396 and can only be used in the body of a function definition. Using a
397 :keyword:`yield` expression in a function definition is sufficient to cause that
398 definition to create a generator function instead of a normal function.
400 When a generator function is called, it returns an iterator known as a
401 generator. That generator then controls the execution of a generator function.
402 The execution starts when one of the generator's methods is called. At that
404 is suspended again, returning the value of :token:`expression_list` to
405 generator's caller. By suspended we mean that all local state is retained,
407 the internal evaluation stack. When the execution is resumed by calling one of
408 the generator's methods, the function can proceed exactly as if the
415 All of this makes generator functions quite similar to coroutines; they yield
417 suspended. The only difference is that a generator function cannot control
418 where should the execution continue after it yields; the control is always
419 transferred to the generator's caller.
421 .. index:: object: generator
424 Generator-iterator methods
427 This subsection describes the methods of a generator iterator. They can
428 be used to control the execution of a generator function.
430 Note that calling any of the generator methods below when the generator
431 is already executing raises a :exc:`ValueError` exception.
436 .. method:: generator.next()
438 Starts the execution of a generator function or resumes it at the last executed
439 :keyword:`yield` expression. When a generator function is resumed with a
440 :meth:`~generator.next` method, the current :keyword:`yield` expression
443 expression, where the generator is suspended again, and the value of the
444 :token:`expression_list` is returned to :meth:`~generator.next`'s caller.
445 If the generator
446 exits without yielding another value, a :exc:`StopIteration` exception is
449 .. method:: generator.send(value)
451 Resumes the execution and "sends" a value into the generator function. The
454 generator, or raises :exc:`StopIteration` if the generator exits without
455 yielding another value. When :meth:`send` is called to start the generator, it
456 must be called with :const:`None` as the argument, because there is no
460 .. method:: generator.throw(type[, value[, traceback]])
462 Raises an exception of type ``type`` at the point where generator was paused,
463 and returns the next value yielded by the generator function. If the generator
464 exits without yielding another value, a :exc:`StopIteration` exception is
465 raised. If the generator function does not catch the passed-in exception, or
471 .. method:: generator.close()
473 Raises a :exc:`GeneratorExit` at the point where the generator function was
474 paused. If the generator function then raises :exc:`StopIteration` (by exiting
476 catching the exception), close returns to its caller. If the generator yields a
477 value, a :exc:`RuntimeError` is raised. If the generator raises any other
478 exception, it is propagated to the caller. :meth:`close` does nothing if the
479 generator has already exited due to an exception or normal exit.
481 Here is a simple example that demonstrates the behavior of generators and
482 generator functions::
485 ... print "Execution starts when 'next()' is called for the first time."
493 ... print "Don't forget to clean up when 'close()' is called."
495 >>> generator = echo(1)
496 >>> print generator.next()
497 Execution starts when 'next()' is called for the first time.
499 >>> print generator.next()
501 >>> print generator.send(2)
503 >>> generator.throw(TypeError, "spam")
505 >>> generator.close()
506 Don't forget to clean up when 'close()' is called.
511 :pep:`342` - Coroutines via Enhanced Generators
524 syntax is:
530 .. _attribute-references:
533 --------------------
537 An attribute reference is a primary followed by a period and a name:
548 references, e.g., a module, list, or an instance. This object is then asked to
549 produce the attribute whose name is the identifier. If this attribute is not
550 available, the exception :exc:`AttributeError` is raised. Otherwise, the type
551 and value of the object produced is determined by the object. Multiple
558 -------------
579 If the primary is a mapping, the expression list must evaluate to an object
580 whose value is one of the keys of the mapping, and the subscription selects the
581 value in the mapping that corresponds to that key. (The expression list is a
584 If the primary is a sequence, the expression list must evaluate to a plain
585 integer. If this value is negative, the length of the sequence is added to it
586 (so that, e.g., ``x[-1]`` selects the last item of ``x``.) The resulting value
588 the subscription selects the item whose index is that value (counting from
595 A string's items are characters. A character is not a separate data type but a
602 --------
634 There is ambiguity in the formal syntax here: anything that looks like an
636 interpreted as a slicing. Rather than further complicating the syntax, this is
638 takes priority over the interpretation as a slicing (this is the case if the
646 respectively. If either bound is negative, the sequence's length is added to
649 empty sequence. It is not an error if *i* or *j* lie outside the range of valid
658 to a mapping object, and it is indexed with a key that is constructed from the
660 is a tuple containing the conversion of the slice items; otherwise, the
661 conversion of the lone slice item is the key. The conversion of a slice item
662 that is an expression is that expression. The conversion of an ellipsis slice
663 item is the built-in ``Ellipsis`` object. The conversion of a proper slice is a
678 -----
680 A call calls a callable object (e.g., a :term:`function`) with a possibly empty
703 The primary must evaluate to a callable object (user-defined functions, built-in
704 functions, methods of built-in objects, class objects, methods of class
707 evaluated before the call is attempted. Please refer to section :ref:`function`
711 arguments, as follows. First, a list of unfilled slots is created for the
713 first N slots. Next, for each keyword argument, the identifier is used to
714 determine the corresponding slot (if the identifier is the same as the first
715 formal parameter name, the first slot is used, and so on). If the slot is
716 already filled, a :exc:`TypeError` exception is raised. Otherwise, the value of
717 the argument is placed in the slot, filling it (even if the expression is
720 function definition. (Default values are calculated, once, when the function is
724 slots for which no default value is specified, a :exc:`TypeError` exception is
725 raised. Otherwise, the list of filled slots is used as the argument list for
728 .. impl-detail::
730 An implementation may provide built-in functions whose positional parameters
732 and which therefore cannot be supplied by keyword. In CPython, this is the
737 :exc:`TypeError` exception is raised, unless a formal parameter using the syntax
738 ``*identifier`` is present; in this case, that formal parameter receives a tuple
743 :exc:`TypeError` exception is raised, unless a formal parameter using the syntax
744 ``**identifier`` is present; in this case, that formal parameter receives a
750 single: *; in function calls
752 If the syntax ``*expression`` appears in the function call, ``expression`` must
756 is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*,
759 A consequence of this is that although the ``*expression`` syntax may appear
760 *after* some keyword arguments, it is processed *before* the keyword arguments
761 (and the ``**expression`` argument, if any -- see below). So::
775 It is unusual for both keyword arguments and the ``*expression`` syntax to be
779 single: **; in function calls
781 If the syntax ``**expression`` appears in the function call, ``expression`` must
784 explicit keyword argument, a :exc:`TypeError` exception is raised.
790 the argument value is assigned to the sublist using the usual tuple assignment
791 rules after all other parameter processing is done.
794 exception. How this value is computed depends on the type of the callable
797 If it is---
799 a user-defined function:
801 pair: function; call
802 triple: user-defined; function; call
803 object: user-defined function
804 object: function
806 The code block for the function is executed, passing it the argument list. The
807 first thing the code block will do is bind the formal parameters to the
808 arguments; this is described in section :ref:`function`. When the code block
810 function call.
812 a built-in function or method:
814 pair: function; call
815 pair: built-in function; call
817 pair: built-in method; call
818 object: built-in method
819 object: built-in function
821 object: function
823 The result is up to the interpreter; see :ref:`built-in-funcs` for the
824 descriptions of built-in functions and methods.
831 A new instance of that class is returned.
839 The corresponding user-defined function is called, with an argument list that is
848 The class must define a :meth:`__call__` method; the effect is then the same as
858 less tightly than unary operators on its right. The syntax is:
865 for the operands): ``-1**2`` results in ``-1``.
867 The power operator has the same semantics as the built-in :func:`pow` function,
870 type. The result type is that of the arguments after coercion.
874 operands (after coercion) unless the second argument is negative; in that case,
875 all arguments are converted to float and a float result is delivered. For
876 example, ``10**2`` returns ``100``, but ``10**-2`` returns ``0.01``. (This last
897 u_expr: `power` | "-" `u_expr` | "+" `u_expr` | "~" `u_expr`
903 The unary ``-`` (minus) operator yields the negation of its numeric argument.
912 long integer argument. The bitwise inversion of ``x`` is defined as
913 ``-(x+1)``. It only applies to integral numbers.
918 :exc:`TypeError` exception is raised.
929 that some of these operations also apply to certain non-numeric types. Apart
936 a_expr: `m_expr` | `a_expr` "+" `m_expr` | `a_expr` "-" `m_expr`
944 sequence repetition is performed; a negative repetition factor yields an empty
953 Plain or long integer division yields an integer of the same type; the result is
954 that of mathematical division with the 'floor' function applied to the result.
965 the result is strictly smaller than the absolute value of the second operand
970 connected with the built-in function :func:`divmod`: ``divmod(x, y) == (x/y,
972 identities hold approximately where ``x/y`` is replaced by ``floor(x/y)`` or
973 ``floor(x/y) - 1`` [#]_.
975 In addition to performing the modulo operation on numbers, the ``%`` operator is
977 known as interpolation). The syntax for string formatting is described in the
978 Python Library Reference, section :ref:`string-formatting`.
982 function are no longer defined for complex numbers. Instead, convert to a
983 floating point number using the :func:`abs` function if appropriate.
994 The ``-`` (subtraction) operator yields the difference of its arguments. The
1016 A right shift by *n* bits is defined as division by ``pow(2, n)``. A left shift
1017 by *n* bits is defined as multiplication with ``pow(2, n)``. Negative shift
1022 In the current implementation, the right-hand operand is required
1023 to be at most :attr:`sys.maxsize`. If the right-hand operand is larger than
1024 :attr:`sys.maxsize` an :exc:`OverflowError` exception is raised.
1069 Unlike C, all comparison operations in Python have the same priority, which is
1071 C, expressions like ``a < b < c`` have the interpretation that is conventional
1077 : | "is" ["not"] | ["not"] "in"
1083 Comparisons can be chained arbitrarily, e.g., ``x < y <= z`` is equivalent to
1084 ``x < y and y <= z``, except that ``y`` is evaluated only once (but in both
1085 cases ``z`` is not evaluated at all when ``x < y`` is found to be false).
1088 *opN* are comparison operators, then ``a op1 b op2 c ... y opN z`` is equivalent
1089 to ``a op1 b and b op2 c and ... y opN z``, except that each expression is
1093 *c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not
1096 The forms ``<>`` and ``!=`` are equivalent; for consistency with C, ``!=`` is
1097 preferred; where ``!=`` is mentioned below ``<>`` is also accepted. The ``<>``
1098 spelling is considered obsolescent.
1101 -----------------
1107 and identity). The value of an object is a rather abstract notion in Python:
1108 For example, there is no canonical access method for an object's value. Also,
1109 there is no requirement that the value of an object should be constructed in a
1111 implement a particular notion of what the value of an object is. One can think
1120 The default behavior for equality comparison (``==`` and ``!=``) is based on
1124 behavior is the desire that all objects should be reflexive (i.e. ``x is y``
1137 have a sensible definition of object value and value-based equality. Such
1139 of built-in types have done that.
1142 built-in types.
1144 * Numbers of built-in numeric types (:ref:`typesnumeric`) and of the standard
1153 result of the built-in function :func:`ord`) of their characters. [#]_
1154 When comparing an 8-bit string and a Unicode string, the 8-bit string
1155 is converted to Unicode. If the conversion fails, the strings
1164 elements, whereby reflexivity of the elements is enforced.
1167 that for a collection element ``x``, ``x == x`` is always true. Based on
1168 that assumption, element identity is compared first, and element comparison
1169 is performed only for distinct elements. This approach yields the same
1171 reflexive. For non-reflexive elements, the result is different than for
1174 Lexicographical comparison between built-in collections works as follows:
1176 - For two collections to compare equal, they must be of the same type, have
1178 equal (for example, ``[1,2] == (1,2)`` is false because the type is not the
1181 - Collections are ordered the same as their
1184 shorter collection is ordered first (for example, ``[1,2] < [1,2,3]`` is
1194 * Most other objects of built-in types compare unequal unless they are the same
1195 object; the choice whether one object is considered smaller or larger than
1196 another one is made arbitrarily but consistently within one execution of a
1199 User-defined classes that customize their comparison behavior should follow
1205 ``x is y`` implies ``x == y``
1219 The following (non-exhaustive) examples illustrate that:
1247 .. _membership-test-details:
1250 --------------------------
1253 s`` evaluates to ``True`` if *x* is a member of *s*, and ``False`` otherwise.
1254 ``x not in s`` returns the negation of ``x in s``. All built-in sequences and
1257 set, frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent
1258 to ``any(x is e or x == e for e in y)``.
1260 For the string and bytes types, ``x in y`` is ``True`` if and only if *x* is a
1261 substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are
1265 For user-defined classes which define the :meth:`__contains__` method, ``x in
1269 For user-defined classes which do not define :meth:`__contains__` but do define
1270 :meth:`__iter__`, ``x in y`` is ``True`` if some value ``z`` with ``x == z`` is
1271 produced while iterating over ``y``. If an exception is raised during the
1272 iteration, it is as if :keyword:`in` raised that exception.
1274 Lastly, the old-style iteration protocol is tried: if a class defines
1275 :meth:`__getitem__`, ``x in y`` is ``True`` if and only if there is a non-negative
1277 raise :exc:`IndexError` exception. (If any other exception is raised, it is as
1286 The operator :keyword:`not in` is defined to have the inverse true value of
1290 operator: is
1291 operator: is not
1299 --------------------
1301 The operators :keyword:`is` and :keyword:`is not` test for object identity: ``x
1302 is y`` is true if and only if *x* and *y* are the same object. ``x is not y``
1332 The operator :keyword:`not` yields ``True`` if its argument is false, ``False``
1337 The expression ``x and y`` first evaluates *x*; if *x* is false, its value is
1338 returned; otherwise, *y* is evaluated and the resulting value is returned.
1342 The expression ``x or y`` first evaluates *x*; if *x* is true, its value is
1343 returned; otherwise, *y* is evaluated and the resulting value is returned.
1347 argument. This is sometimes useful, e.g., if ``s`` is a string that should be
1348 replaced by a default value if it is empty, the expression ``s or 'foo'`` yields
1371 if *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
1372 evaluated and its value is returned.
1385 pair: anonymous; function
1393 ``lambda parameters: expression`` yields a function object. The unnamed object
1394 behaves like a function object defined with ::
1399 See section :ref:`function` for the syntax of parameter lists. Note that
1416 the tuple is the number of expressions in the list. The expressions are
1421 The trailing comma is required only to create a single tuple (a.k.a. a
1422 *singleton*); it is optional in all other cases. A single expression without a
1436 assignment, the right-hand side is evaluated before the left-hand side.
1444 expr1 + expr2 * (expr3 - expr4)
1449 .. _operator-summary:
1458 the same box have the same precedence. Unless the syntax is explicitly given,
1461 left to right --- see section :ref:`comparisons` --- and exponentiation, which
1464 +-----------------------------------------------+-------------------------------------+
1468 +-----------------------------------------------+-------------------------------------+
1469 | :keyword:`if` -- :keyword:`else` | Conditional expression |
1470 +-----------------------------------------------+-------------------------------------+
1472 +-----------------------------------------------+-------------------------------------+
1474 +-----------------------------------------------+-------------------------------------+
1476 +-----------------------------------------------+-------------------------------------+
1478 | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests |
1480 +-----------------------------------------------+-------------------------------------+
1482 +-----------------------------------------------+-------------------------------------+
1484 +-----------------------------------------------+-------------------------------------+
1486 +-----------------------------------------------+-------------------------------------+
1488 +-----------------------------------------------+-------------------------------------+
1489 | ``+``, ``-`` | Addition and subtraction |
1490 +-----------------------------------------------+-------------------------------------+
1493 +-----------------------------------------------+-------------------------------------+
1494 | ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |
1495 +-----------------------------------------------+-------------------------------------+
1497 +-----------------------------------------------+-------------------------------------+
1500 +-----------------------------------------------+-------------------------------------+
1505 +-----------------------------------------------+-------------------------------------+
1511 behavior is deprecated, and relying on it will not work in Python 3.
1513 .. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be
1515 a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 %
1516 1e100`` have the same sign as ``1e100``, the computed result is ``-1e-100 +
1517 1e100``, which is numerically exactly equal to ``1e100``. The function
1519 first argument instead, and so returns ``-1e-100`` in this case. Which approach
1520 is more appropriate depends on the application.
1522 .. [#] If x is very close to an exact integer multiple of y, it's possible for
1523 ``floor(x/y)`` to be one larger than ``(x-x%y)/y`` due to rounding. In such
1530 code point, there is a number of abstract characters that can in addition be
1539 points. This may be counter-intuitive to humans. For example,
1540 ``u"\u00C7" == u"\u0043\u0327"`` is ``False``, even though both strings
1543 To compare strings at the level of abstract characters (that is, in a way
1552 .. [#] Due to automatic garbage-collection, free lists, and the dynamic nature of
1554 the :keyword:`is` operator, like those involving comparisons between instance
1557 .. [#] The ``%`` operator is also used for string formatting; the same
1561 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.