[Python-ideas] Proto-PEP on a 'yield from' statement
Bruce Frederiksen
dangyogi at gmail.com
Fri Feb 13 02:57:16 CET 2009
I would think that in addition to forwarding send values to the
subgenerator, that throw exceptions sent to the delegating generator
also be forwarded to the subgenerator. If the subgenerator does not
handle the exception, then it should be re-raised in the delegating
generator. Also, the subgenerator close method should be called by the
delegating generator. Thus, the new expansion code would look something
like:
_i = iter(iterable)
try:
_value_to_yield = next(_i)
while True:
try:
_value_to_send = yield _value_to_yield
except Exception as _throw_from_caller:
if hasattr(_i, 'throw'):
_value_to_yield = _i.throw(_throw_from_caller)
else:
raise
else:
if hasattr(_i, 'send'):
_value_to_yield = _i.send(_value_to_send)
else:
_value_to_yield = next(_i)
except StopIteration as _exc_from_i:
_a = _exc_from_i.args
if len(_a) > 0:
result = _a[0]
else:
result = None
finally:
if hasattr(_i, 'close'):
_i.close()
I'm also against using return as the syntax for a final value from the
subgenerator. I've accidentally used return inside generators many
times and appreciate getting an error for this. I would be OK with
removing the ability to return a final value from the subgenerator. Or
create a new syntax that won't be used accidentally. Perhaps: finally
return expr?
-bruce frederiksen
Greg Ewing wrote:
> Comments are invited on the following proto-PEP.
More information about the Python-ideas
mailing list