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 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 1. A template for ensuring that a lock, acquired at the start of a
block, is released when the block is left: block, is released when the block is left:
def locking(lock): def locking(lock):
lock.acquire() lock.acquire()
try: try:
yield yield None
finally: finally:
lock.release() lock.release()
@ -421,7 +424,7 @@ Examples
def transactional(db): def transactional(db):
try: try:
yield yield None
except: except:
db.rollback() db.rollback()
raise raise
@ -433,7 +436,7 @@ Examples
def auto_retry(n=3, exc=Exception): def auto_retry(n=3, exc=Exception):
for i in range(n): for i in range(n):
try: try:
yield yield None
return return
except exc, err: except exc, err:
# perhaps log exception here # perhaps log exception here
@ -498,7 +501,7 @@ Examples
save_stdout = sys.stdout save_stdout = sys.stdout
try: try:
sys.stdout = new_stdout sys.stdout = new_stdout
yield yield None
finally: finally:
sys.stdout = save_stdout 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 are all illegal. (Some of the edge cases are motivated by the
current legality of "yield 12, 42".) 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 When __next__() is called with an argument that is not None, the
yield-expression that it resumes will return the argument. If it yield-expression that it resumes will return the argument. If it
resumes a yield-statement, the value is ignored (this is similar resumes a yield-statement, the value is ignored (this is similar