Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'subject:Python': 0.06; 'args)': 0.09; 'calls.': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'try:': 0.09; 'runs': 0.10; 'def': 0.12; 'jan': 0.12; 'agree.': 0.16; 'args:': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'res': 0.16; 'rewriting': 0.16; 'roy': 0.16; 'true:': 0.16; 'unneeded': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; '>>>': 0.22; 'putting': 0.22; 'header :User-Agent:1': 0.23; 'rid': 0.24; 'stopping': 0.24; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; "i'm": 0.30; 'work.': 0.31; 'gives': 0.31; 'code': 0.31; 'lines': 0.31; 'controlled': 0.31; "d'aprano": 0.31; 'indentation': 0.31; 'raised': 0.31; 'seemingly': 0.31; 'steven': 0.31; 'except': 0.35; 'yield': 0.36; 'next': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'simple,': 0.60; 'break': 0.61; "you're": 0.61; 'kind': 0.63; 'more': 0.64; 'temporary': 0.65; 'brain': 0.68; 'smith': 0.68; 'glad': 0.83; 'received:fios.verizon.net': 0.84; 'short,': 0.84; 'wall.': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python Iterables struggling using map() built-in Date: Sun, 07 Dec 2014 19:38:08 -0500 References: <004e01d01173$e1641c10$a42c5430$@gmail.com> <5484e3d8$0$13013$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417999114 news.xs4all.nl 2921 [2001:888:2000:d::a6]:45759 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82088 On 12/7/2014 7:12 PM, Roy Smith wrote: > Chris Angelico wrote: >>> I'm actually glad PEP 479 will break this kind of code. Gives a good >>> excuse for rewriting it to be more readable. > > Steven D'Aprano wrote: >> What kind of code is that? Short, simple, Pythonic and elegant? :-) >> >> Here's the code again, with indentation fixed: >> >> >> 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". That's not at all what's happening here, so my brain runs into a > wall. I agree. Too tricky. The code should have been def myzip(*args): if args: iters = map(iter, args) while True: res = [next(i) for i in iters] yield tuple(res) However, this 'beautiful' code has a trap. If one gets rid of the seemingly unneeded temporary list res by telescoping the last two lines into a bit too much into yield tuple(next(i) for i in iters) we now have an infinite generator, because tuple() swallows the StopIteration raised as a side-effect of next calls. def myzip(*args): if args: iters = map(iter, args) while True: try: result = [next(i) for i in iters] except StopIteration return yield tuple(res) makes the side-effect dependence of stopping clearer. Putting yield tuple([next(i) for i in iters]) in the try would also work. -- Terry Jan Reedy