Note that 'yield' w/o an EXPR is currently illegal.

This commit is contained in:
Guido van Rossum 2005-05-11 22:09:37 +00:00
parent 9cb17e5426
commit 4f5abc4b4f
2 changed files with 13 additions and 4 deletions

View File

@ -383,13 +383,16 @@ Comparison to Thunks
Examples
(Several of these examples contain "yield None". If PEP 342 is
accepted, these can be changed to just "yield" of course.)
1. A template for ensuring that a lock, acquired at the start of a
block, is released when the block is left:
def locking(lock):
lock.acquire()
try:
yield
yield None
finally:
lock.release()
@ -421,7 +424,7 @@ Examples
def transactional(db):
try:
yield
yield None
except:
db.rollback()
raise
@ -433,7 +436,7 @@ Examples
def auto_retry(n=3, exc=Exception):
for i in range(n):
try:
yield
yield None
return
except exc, err:
# perhaps log exception here
@ -498,7 +501,7 @@ Examples
save_stdout = sys.stdout
try:
sys.stdout = new_stdout
yield
yield None
finally:
sys.stdout = save_stdout

View File

@ -140,6 +140,12 @@ Specification: Generators and Yield-Expressions
are all illegal. (Some of the edge cases are motivated by the
current legality of "yield 12, 42".)
Note that a yield-statement or yield-expression without an
expression is now legal. This makes sense: when the information
flow in the next() call is reversed, it should be possible to
yield without passing an explicit value ("yield" is of course
equivalent to "yield None").
When __next__() is called with an argument that is not None, the
yield-expression that it resumes will return the argument. If it
resumes a yield-statement, the value is ignored (this is similar