PEP 255: Simple Generators
Tim Peters
tim.one at home.com
Tue Jun 19 20:19:29 EDT 2001
[Denys Duchier]
> The one thing that rubs me the wrong way with this proposal is that it
> is not compositional. Unless I misread the PEP, the entire generation
> logic must be defined in a single function (because the `yield'
> keyword must statically occur within the body of the generator
> function). This makes it impossible to use higher-order programming
> to assemble complex generators from smaller blocks.
Since higher-order programming is an aberration (meaning statistically rare)
in idiomatic Python, a specific example would help. If, e.g., you want to
build a generator that delivers the results from generators g and h
alternately, it's easy (although I personally don't want to <wink>):
def zipgen(g, h):
giter = g()
hiter = h()
while 1:
yield giter.next()
yield hiter.next()
def g():
i = 1
while 1:
yield i
i += 1
def h():
i = -1
while 1:
yield i
i -= 1
count = 0
z = zipgen(g, h)
while count < 10:
print z.next(),
count += 1
print
which prints
1 -1 2 -2 3 -3 4 -4 5 -5
It's the iteration protocol that's flexible, not generators per se.
> One way to address this issue would be to make the dynamic bracketing
> explicit, for example as an annotation on the function which is
> intended to wrap the generator (this has been proposed by many, albeit
> for different reasons).
The title of the PEP is "Simple Generators", and the adjective is important:
these will never make it in if they explode anyone's head, and this isn't
meant to be esoteric wizard stuff but useful to everyday programmers.
> I won't make a proposal of my own, but I'll just point out that the
> issue is very much related to the old duality control/prompt
> (Felleisen) or later shift/reset (Filinski) in the continuation world.
Heh. If you don't spell out what the concern is, I can pretty much
guarantee nobody else is going to <wink>. This flavor of generator is a
semi-coroutine, much the same as Icon's, Sather's and CLU's (although
they're called "iterators" in the latter two languages).
More information about the Python-list
mailing list