generators and exceptions
Greg Ewing (using news.cis.dfn.de)
me at privacy.net
Mon Mar 17 22:30:43 EST 2003
Andrew Bennetts wrote:
> But from which would you expect it to resume? Just before the exception was
> raised? Just after? What about try/except blocks? What if the exception
> was raised inside a function called by a generator? What about an exception
> thrown by a generator that your generator calls?
It seems to me that what Mr. Evans wants is some way for
a generator to cause an exception to be raised in its
caller, without having to propagate the exception up
through its own stack frame first.
Suppose there were a hypothetical "yield raise" statement
which did this, then he could write
def mygen(val):
while val > 0:
if val % 2:
yield raise MyExc
else:
yield val
val -= 1
The "yield raise" statement wouldn't disturb the control
flow of the generator at all -- it would be just like a
normal yield, except that whatever called the generator-
iterator's next() would get an exception instead of a
return value.
As for how *useful* such a thing would be, I don't know.
I won't be leaping up to write a PEP, though...
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list