Please comment on Draft PEP for Enhanced Generators

Andrae Muys amuys at
Sun Feb 3 21:06:46 EST 2002

"Raymond Hettinger" <othello at> wrote in message news:<a39cpr$ic1$1 at>...
> "Alex Martelli" <aleax at> wrote in message
> news:a3976j$qkt$1 at
> > def xmap( fun, *sequences ):
> >     ...
> >  for args in xzip( *sequences ):
> >      yield fun(*args)
> >
> > has the wrong behavior when the sequences have different lengths.
> > has
> > an implementation which I believe satisfies the specs (complicated,
> > alas, but those specs ARE tricky;-).
> 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):
  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