PEP 255: Simple Generators

Guido van Rossum guido at python.org
Wed Jun 20 16:21:08 EDT 2001


Denys Duchier <Denys.Duchier at ps.uni-sb.de> writes:

[Quoting Tim Peters]
> >  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()
> 
> which I would write as follows:
> 
> def zipgen(g,h):
>   def result(yield):
>     g(yield)
>     h(yield)
>   return result

Well, that settles it -- I find Tim's version much easier to read than
Denys's version.  Thanks for playing. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list