Python Iterables struggling using map() built-in

Terry Reedy tjreedy at udel.edu
Tue Dec 9 00:03:33 EST 2014


On 12/8/2014 9:50 PM, Steven D'Aprano wrote:
> Roy Smith wrote:
>
>> Chris Angelico wrote:

>>> def myzip(*args):
>>>      iters = map(iter, args)
>>>      while iters:
>>>          res = [next(i) for i in iters]
>>>          yield tuple(res)
>>
>> Ugh.  When I see "while foo", my brain says, "OK, you're about to see a
>> loop which is controlled by the value of foo being changed inside the
>> loop".
>
> Yes. Me too. 99% of the time when you see "while foo", that's what you'll
> get, so it's the safe assumption. But it's only an assumption, not a
> requirement. When you read a bit more of the code and see that iters isn't
> being modified, your reaction ought to be closer "oh wow, that's neat"

To me it is a code smell.  iters is empty if and only if args is empty. 
  If args is empty, iters should not be created.

if args:
   iters = ...
   while True
     ... (return on exception)

makes the logic clear.

-- 
Terry Jan Reedy




More information about the Python-list mailing list