Python Iterables struggling using map() built-in

Roy Smith roy at panix.com
Sun Dec 7 19:27:49 EST 2014


In article <mailman.16689.1417996247.18130.python-list at python.org>,
 Chris Angelico <rosuav at gmail.com> wrote:

> On Mon, Dec 8, 2014 at 10:33 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
> > How would we re-write this to work in the future Python 3.7? Unless I have
> > missed something, I think we could write it like this:
> >
> > def myzip37(*args):
> >     iters = list(map(iter, args))
> >     while iters:
> >         try:
> >             yield tuple([next(i) for i in iters])
> >         except StopIteration:
> >             return

I'm still not liking this use of while.  Yes, of course, it handles the 
special case of no arguments, but I'd be in-your-face about that (not 
tested):

def myzip37(*args):
    iters = list(map(iter, args))
    if not iters:
        return None
    while True:
        try:
            yield tuple([next(i) for i in iters])
        except StopIteration:
            return

This makes it really obvious that there's something going on inside the 
loop other than exhausting the control variable to cause it to exit.

Although, to be honest, I'm wondering if this is more straight-forward 
(also not tested):

def myzip37(*args):
    if not args:
        return
    iters = list(map(iter, args))
    while True:
        try:
            yield tuple(map(next, iters))
        except StopIteration:
            return



More information about the Python-list mailing list