[Python-Dev] Re: PEP 279

Tim Peters tim.one@comcast.net
2002年3月29日 15:38:34 -0500


[Raymond Hettinger, on generator exceptions]
> I need help from others on py-dev who can articulate the need clearly.
> For me, it's as plain as day and I don't know what to say to convey the
> message better than it is expressed in the PEP.

I can't: "simple generators" were deliberately not coroutines, and I
dislike trying to add 2-way control-flow gimmicks to them. Over the years
I've found that "resumable function" is easy to get across even to C
programmers <wink>, but anything fancier hits a wall. So I'd like to keep
generators simple.
Generator exceptions do less damage (IMO) to simplicity than some of the
other extensions, but in practice, in the very few cases I've wanted
something like that, this worked fine:
class Whatever:
 def __init__(self):
 self._stop = 0
 def stop(self):
 self._stop = 1
 def generator(self):
 while 1:
 ...
 yield something
 if self._stop:
 break
 cleanup
I agree a throw() would be easier to live with, the problem is that I've so
rarely needed it.
> ...
> It was meant to be a generic and oversimplified example of
> clean-up code in a generator being used as a data consumer.

Simple generators were designed to be data producers, and:
> ...
> Dr. Mertz's co-routine code does make a much better case,

Bingo. If you want coroutines, design a coroutine facility. Piling more
semantics on to "yield" takes a delightfully simple yet powerful gimmick and
hides it under ten kinds of obscurity.

AltStyle によって変換されたページ (->オリジナル) /