Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24609)

Delta Between Two Patch Sets: Doc/reference/simple_stmts.rst

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 7 years, 11 months ago
Right Patch Set: Created 7 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 1
2 .. _simple: 2 .. _simple:
3 3
4 ***************** 4 *****************
5 Simple statements 5 Simple statements
6 ***************** 6 *****************
7 7
8 .. index:: pair: simple; statement 8 .. index:: pair: simple; statement
9 9
10 Simple statements are comprised within a single logical line. Several simple 10 Simple statements are comprised within a single logical line. Several simple
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 418
419 :keyword:`return` leaves the current function call with the expression list (or 419 :keyword:`return` leaves the current function call with the expression list (or
420 ``None``) as return value. 420 ``None``) as return value.
421 421
422 .. index:: keyword: finally 422 .. index:: keyword: finally
423 423
424 When :keyword:`return` passes control out of a :keyword:`try` statement with a 424 When :keyword:`return` passes control out of a :keyword:`try` statement with a
425 :keyword:`finally` clause, that :keyword:`finally` clause is executed before 425 :keyword:`finally` clause, that :keyword:`finally` clause is executed before
426 really leaving the function. 426 really leaving the function.
427 427
428 In a generator function, the :keyword:`return` statement is not allowed to 428 In a generator function, the :keyword:`return` statement indicates that the
429 include an :token:`expression_list`. In that context, a bare :keyword:`return` 429 generator is done and will cause :exc:`StopIteration` to be raised. The returned
430 indicates that the generator is done and will cause :exc:`StopIteration` to be 430 value (if any) is used as an argument to construct :exc:`StopIteration` and
431 raised. 431 becomes the :attr:`StopIteration.value` attribute.
432 432
433 433
434 .. _yield: 434 .. _yield:
435 435
436 The :keyword:`yield` statement 436 The :keyword:`yield` statement
437 ============================== 437 ==============================
438 438
439 .. index:: 439 .. index::
440 statement: yield 440 statement: yield
441 single: generator; function 441 single: generator; function
442 single: generator; iterator 442 single: generator; iterator
443 single: function; generator 443 single: function; generator
444 exception: StopIteration 444 exception: StopIteration
445 445
446 .. productionlist:: 446 .. productionlist::
447 yield_stmt: `yield_expression` 447 yield_stmt: `yield_expression`
448 448
449 The :keyword:`yield` statement is only used when defining a generator function, 449 The :keyword:`yield` statement is only used when defining a generator function,
450 and is only used in the body of the generator function. Using a :keyword:`yield` 450 and is only used in the body of the generator function. Using a :keyword:`yield`
451 statement in a function definition is sufficient to cause that definition to 451 statement in a function definition is sufficient to cause that definition to
452 create a generator function instead of a normal function. 452 create a generator function instead of a normal function.
453
453 When a generator function is called, it returns an iterator known as a generator 454 When a generator function is called, it returns an iterator known as a generator
454 iterator, or more commonly, a generator. The body of the generator function is 455 iterator, or more commonly, a generator. The body of the generator function is
455 executed by calling the :func:`next` function on the generator repeatedly until 456 executed by calling the :func:`next` function on the generator repeatedly until
456 it raises an exception. 457 it raises an exception.
457 458
458 When a :keyword:`yield` statement is executed, the state of the generator is 459 When a :keyword:`yield` statement is executed, the state of the generator is
459 frozen and the value of :token:`expression_list` is returned to :meth:`next`'s 460 frozen and the value of :token:`expression_list` is returned to :meth:`next`'s
460 caller. By "frozen" we mean that all local state is retained, including the 461 caller. By "frozen" we mean that all local state is retained, including the
461 current bindings of local variables, the instruction pointer, and the internal 462 current bindings of local variables, the instruction pointer, and the internal
462 evaluation stack: enough information is saved so that the next time :func:`next` 463 evaluation stack: enough information is saved so that the next time :func:`next`
463 is invoked, the function can proceed exactly as if the :keyword:`yield` 464 is invoked, the function can proceed exactly as if the :keyword:`yield`
464 statement were just another external call. 465 statement were just another external call.
465 466
466 The :keyword:`yield` statement is allowed in the :keyword:`try` clause of a 467 The :keyword:`yield` statement is allowed in the :keyword:`try` clause of a
467 :keyword:`try` ... :keyword:`finally` construct. If the generator is not 468 :keyword:`try` ... :keyword:`finally` construct. If the generator is not
468 resumed before it is finalized (by reaching a zero reference count or by being 469 resumed before it is finalized (by reaching a zero reference count or by being
469 garbage collected), the generator-iterator's :meth:`close` method will be 470 garbage collected), the generator-iterator's :meth:`close` method will be
470 called, allowing any pending :keyword:`finally` clauses to execute. 471 called, allowing any pending :keyword:`finally` clauses to execute.
471 472
473 When ``yield from expression`` is used, it treats the supplied expression as
474 a subiterator, producing values from it until the underlying iterator is
475 exhausted.
476
477 For full details of :keyword:`yield` semantics, refer to the :ref:`yieldexpr`
478 section.
479
472 .. seealso:: 480 .. seealso::
473 481
474 :pep:`0255` - Simple Generators 482 :pep:`0255` - Simple Generators
475 The proposal for adding generators and the :keyword:`yield` statement to P ython. 483 The proposal for adding generators and the :keyword:`yield` statement to P ython.
476 484
477 :pep:`0342` - Coroutines via Enhanced Generators 485 :pep:`0342` - Coroutines via Enhanced Generators
478 The proposal that, among other generator enhancements, proposed allowing 486 The proposal to enhance the API and syntax of generators, making them
479 :keyword:`yield` to appear inside a :keyword:`try` ... :keyword:`finally` block. 487 usable as simple coroutines.
488
489 :pep:`0380` - Syntax for Delegating to a Subgenerator
490 The proposal to introduce the :token:`yield_from` syntax, making delegatio n
491 to sub-generators easy.
480 492
481 493
482 .. _raise: 494 .. _raise:
483 495
484 The :keyword:`raise` statement 496 The :keyword:`raise` statement
485 ============================== 497 ==============================
486 498
487 .. index:: 499 .. index::
488 statement: raise 500 statement: raise
489 single: exception 501 single: exception
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
981 enclosing scope (the scope in which a new binding should be created cannot 993 enclosing scope (the scope in which a new binding should be created cannot
982 be determined unambiguously). 994 be determined unambiguously).
983 995
984 Names listed in a :keyword:`nonlocal` statement must not collide with 996 Names listed in a :keyword:`nonlocal` statement must not collide with
985 pre-existing bindings in the local scope. 997 pre-existing bindings in the local scope.
986 998
987 .. seealso:: 999 .. seealso::
988 1000
989 :pep:`3104` - Access to Names in Outer Scopes 1001 :pep:`3104` - Access to Names in Outer Scopes
990 The specification for the :keyword:`nonlocal` statement. 1002 The specification for the :keyword:`nonlocal` statement.
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+