[Python-ideas] Yield-From: Finalization guarantees
Jacob Holm
jh at improva.dk
Thu Mar 26 01:50:37 CET 2009
Greg Ewing wrote:
> Jacob Holm wrote:
>
>> But if you throw another exception and it is converted to a
>> StopIteration by the subiterator, this should definitely stop the
>> subiterator and get a return value.
>
> Not if it simply raises a StopIteration from the
> throw call. It would have to mark itself as
> completed, return normally from the throw and
> then raise StopIteration on the next call to
> next() or send().
>
One of us must be missing something... If the subiterator is exhausted
before the throw, there won't *be* a value to return from the call so
the only options for the throw method are to raise StopIteraton, or to
raise some other exception. Example:
def inner():
try:
yield 1
except ValueError:
pass
return 2
def outer():
v = yield from inner()
yield v
g = outer()
print g.next() # prints 1
print g.throw(ValueError) # prints 2
In your expansion, the StopIteration raised by inner escapes the outer
generator as well, so we get a StopIteration instead of the second print
that I would expect. Can you explain in a little more detail how the
inlining argument makes you want to not catch a StopIteration escaping
from throw?
- Jacob
More information about the Python-ideas
mailing list