Please comment on Draft PEP for Enhanced Generators

Andrae Muys amuys at shortech.com.au
Sun Feb 3 21:06:46 EST 2002


"Raymond Hettinger" <othello at javanet.com> wrote in message news:<a39cpr$ic1$1 at bob.news.rcn.net>...
> "Alex Martelli" <aleax at aleax.it> wrote in message
> news:a3976j$qkt$1 at serv1.iunet.it...
> > def xmap( fun, *sequences ):
> >     ...
> >  for args in xzip( *sequences ):
> >      yield fun(*args)
> >
> > has the wrong behavior when the sequences have different lengths.
> > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66448 has
> > an implementation which I believe satisfies the specs (complicated,
> > alas, but those specs ARE tricky;-).
<snip>
> I have one misgiving -- padding shorter sequences with None makes sense
> when all of the sequences are expected to be finite, but may not be the
> desired
> behavior when one of the sequences is potentially infinite. I would like to
> xmap( None, xrange(sys.maxint), iter(f.readline,'') ) to return a finite
> sequence
> and respond to list(mygen) without crashing in an infinite loop.

As unpadding is impossible, while padding is trivial, this suggests
the existence of an additional generator to pad where required.

def xpad(iterator, default=None):
  try:
    yield iterator.next()
  except StopIteration:
    yield default

which would then allow you to pad shorter sequences with None in those
cases where it is desired.

Andrae Muys



More information about the Python-list mailing list