Python Iterables struggling using map() built-in

Roy Smith roy at panix.com
Mon Dec 8 19:02:00 EST 2014


In article <5485721c$0$2817$c3e8da3$76491128 at news.astraweb.com>,
 Steven D'Aprano <steve at pearwood.info> wrote:

> On Mon, 08 Dec 2014 11:35:36 +1100, Chris Angelico wrote:
> 
> > On Mon, Dec 8, 2014 at 11:27 AM, Roy Smith <roy at panix.com> wrote:
> >> 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))
> > 
> > Yes, I prefer this too. It's explicit and clear that passing no
> > arguments will yield no values.
> 
> The first version is explicit and clear too. I'm sorry to say this, but 
> it is true: if you (generic you) don't recognise that
> 
>     while iters:
>         ...
> 
> 
> skips the while block if iters is an empty list, then *you* have a 
> problem, not the code.

The problem is not that the while body is skipped if iters is falsey.  
Thats quite clear.

The problem is that the looping termination isn't actually controlled by 
the control variable being exhausted.  It's controlled by an exception 
getting thrown.  I'm OK with breaking out of a loop by throwing an 
exception, but if that's what you're going to do, then make it clear 
that's the case by doing "while 1" or "while True".  Those screams out, 
"Hey, look at me, I'm an infinite loop", which is your clue that there's 
something else going on.



More information about the Python-list mailing list