[Python-Dev] PEP 334 - Simple Coroutines via SuspendIteration
Jp Calderone
exarkun at divmod.com
Wed Sep 8 15:07:59 CEST 2004
Clark C. Evans wrote:
> Josiah Carlson kindly pointed out (off list), that my use of
> SuspendIteration violates the standard idiom of exceptions
> terminating the current function. This got past me, beacuse
> I think a generator not as a function, but rather as a shortcut
> to creating iterators. The offending code is,
>
> | def NonBlockingResource():
> | yield "one"
> | while True:
> | rand = randint(1,10)
> | if 2 == rand:
> | break
> | raise SuspendIteration()
> | yield "two"
>
> There are two solutions:
> (a) introduce a new keyword 'suspend'; or,
> (b) don't do that.
>
> It is not essential to the proposal that the generator syntax produce
> iterators that can SuspendIteration, it is only essential that the
> implementation of generators pass-through this exception. Most
> non-blocking resources will be low-level components from an async
> database or socket library; they can make iterators the old way.
>
What about this?
def somefunc():
raise SuspendIteration()
return 'foo'
def genfunc():
yield somefunc()
Jp
More information about the Python-Dev
mailing list