[Python-Dev] Re: PEP 255: Simple Generators
Jeff Epler
jepler@inetnebr.com
Tue, 19 Jun 2001 08:25:23 -0500
On Tue, Jun 19, 2001 at 06:21:14PM +1200, Greg Ewing wrote:
> Something is bothering me about this. In fact,
> it's bothering me a LOT. In the following, will
> f() work as a generator-function:
>
> def f():
> for i in range(5):
> g(i)
>
> def g(i):
> for j in range(10):
> yield i,j
>
> If I understand PEP255 correctly, this will *not*
> work. But it seems entirely reasonable to me that
> it *should* work. It *has* to work, otherwise how
> am I to write generators that are too complicated
> to fit into a single function?
The following similar code seems to produce the results you have in
mind.
def f():
for i in range(5):
#g(i)
#yield g(i)
for x in g(i): yield x
def g(i):
for j in range(10):
yield i, j
It would be nice to have a succinct way to say
'for dummy in iterator: yield dummy'. Maybe 'yield from iterator'?
Then f would become:
def f():
for i in range(5):
yield from g(i)
Jeff
PS I noticed that the generator branch got merged into the trunk. Cool!