Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'interpreter': 0.05; 'that?': 0.05; 'subject:Python': 0.06; 'args)': 0.09; 'means,': 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; 'python': 0.11; 'def': 0.12; 'args:': 0.16; 'duplicates': 0.16; 'master.': 0.16; 'nope,': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'res': 0.16; 'rewriting': 0.16; 'roy': 0.16; 'true:': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'bit': 0.19; 'trying': 0.19; '>>>': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; 'rid': 0.24; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'correct': 0.29; 'chris': 0.29; 'thus': 0.29; "doesn't": 0.30; 'raymond': 0.30; 'along': 0.30; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'easier': 0.31; 'controlled': 0.31; "d'aprano": 0.31; 'implicit': 0.31; 'indentation': 0.31; 'steven': 0.31; 'probably': 0.32; 'supposed': 0.32; "we're": 0.32; 'another': 0.32; 'call.': 0.33; 'sense': 0.34; 'except': 0.35; 'test': 0.35; 'but': 0.35; 'yield': 0.36; 'doing': 0.36; 'next': 0.36; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'easy': 0.60; 'guy': 0.60; 'simple,': 0.60; 'solve': 0.60; 'most': 0.60; 'tell': 0.60; 'break': 0.61; 'entire': 0.61; "you're": 0.61; 'back': 0.62; 'save': 0.62; 'kind': 0.63; 'happen': 0.63; 'map': 0.64; 'more': 0.64; 'temporary': 0.65; 'minutes': 0.67; 'beautiful': 0.68; 'brain': 0.68; 'smith': 0.68; 'goal': 0.75; 'yourself': 0.78; 'glad': 0.83; 'beats': 0.84; 'experiment': 0.84; 'expresses': 0.84; 'instantly': 0.84; 'kiss': 0.84; 'short,': 0.84; 'tricky': 0.84; 'wall.': 0.84; 'masters': 0.91; 'mystery': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Python Iterables struggling using map() built-in Date: Sun, 07 Dec 2014 20:06:35 -0500 References: <004e01d01173$e1641c10$a42c5430$@gmail.com> <5484e3d8$0$13013$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; 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: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418000807 news.xs4all.nl 2860 [2001:888:2000:d::a6]:33536 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82091 On 12/7/14 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. > > Next problem, what the heck is "res"? We're not back in the punch-card > days. We don't have to abbreviate variable names to save columns. > Variable names are supposed to describe what they hold, and thus help > you understand the code. I have no idea what "res" is supposed to be. > Residue? Result? Rest_of_items? Response? None of these make much > sense here, so I'm just left befuddled. > >> It would be even more beautiful if we get rid of the unnecessary temporary >> variable: >> >> def myzip(*args): >> iters = map(iter, args) >> while iters: >> yield tuple([next(i) for i in iters]) > > Well, that's one way to solve the mystery of what "res" means, but it > doesn't actually make it easier to understand. > >> I think this function makes a good test to separate the masters from the >> apprentices. > > The goal of good code is NOT to separate the masters from the > apprentices. The goal of good code is to be correct and easy to > understand by the next guy who comes along to maintain it. > >> If you can read this function and instantly tell how it works, that it is >> bug-free and duplicates the behaviour of the built-in zip(), you're >> probably Raymond Hettinger. If you can tell what it does but you have to >> think about it for a minute or two before you understand why it works, you >> can call yourself a Python master. If you have to sit down with the >> interactive interpreter and experiment for a bit to understand it, you're >> doing pretty well. > > That pretty much is the point I'm trying to make. If the code is so > complicated that masters can only understand it after a couple of > minutes of thought, and those of us who are just "doing pretty well" > need to sit down and puzzle it out in the REPL, then it's too > complicated for most people to understand. KISS beats elegant. > Now that I understand all the intricacies (thanks everyone!), this is how I would write it: def zip(*args): if not args: return iters = list(map(iter, args)) while True: try: result = [next(it) for it in iters] except StopIteration: return yield tuple(result) The implicit use of StopIteration to end the entire generator is far too implicit for my taste. This code expresses the intent much better. And good call on not being able to use: tuple(next(it) for it in iters) Again, the tricky implicit hidden StopIterations are confusing. One last tweak: why do we use map to make iters, but a list comprehension to make result? OK, let's try this: result = map(next, iters) Oops, another infinite loop on Py3, right, because map is lazy, so the StopIteration doesn't happen until the tuple() call. OK, try this: result = list(map(next, iters)) Nope, still infinite because now list() consumes the StopIteration again. Ugh. -- Ned Batchelder, http://nedbatchelder.com