Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subsequent': 0.05; 'tree': 0.05; 'expressions': 0.07; 'nested': 0.07; '"if': 0.09; '22,': 0.09; 'inserts': 0.09; 'mess': 0.09; 'moreover,': 0.09; 'okay': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separately': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '*never*': 0.16; '23,': 0.16; 'comp': 0.16; 'hmm.': 0.16; 'limiting': 0.16; 'loops': 0.16; 'nodes': 0.16; 'ought': 0.16; 'reasonably': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repetition': 0.16; 'separated': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'trying': 0.19; 'solution.': 0.20; '>>>': 0.22; 'memory': 0.22; 'example': 0.22; 'programming': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'subversion': 0.24; 'fine': 0.24; 'mon,': 0.24; 'equivalent': 0.26; 'references': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'array': 0.29; 'dec': 0.30; 'mix': 0.30; "i'm": 0.30; 'that.': 0.31; '>>>>': 0.31; 'bunch': 0.31; 'claiming': 0.31; "d'aprano": 0.31; 'horizontal': 0.31; 'node': 0.31; 'steven': 0.31; 'though.': 0.31; 'values.': 0.31; 'another': 0.32; 'programmers': 0.33; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'machine.': 0.36; 'ordered': 0.36; 'subject:List': 0.36; 'yield': 0.36; "i'll": 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'problems': 0.38; 'driving': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'functional': 0.39; 'use.': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'easy': 0.60; 'expression': 0.60; 'most': 0.60; 'subject:skip:C 10': 0.61; 'new': 0.61; 'course': 0.61; "you're": 0.61; 'save': 0.62; 'making': 0.63; 'hear': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'side': 0.67; 'respect': 0.70; 'physical': 0.72; 'records': 0.73; 'gain': 0.79; "'for'": 0.84; 'clearer': 0.84; 'effects,': 0.84; 'id,': 0.84; 'received:fios.verizon.net': 0.84; 'recursion;': 0.84; 'side- effects': 0.84; 'together,': 0.84; 'procedural': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: List Comprehensions Date: Mon, 22 Dec 2014 15:28:24 -0500 References: <5497e2a2$0$12978$c3e8da3$5496439d@news.astraweb.com> <5498362f$0$12994$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:31.0) Gecko/20100101 Thunderbird/31.3.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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419280133 news.xs4all.nl 2929 [2001:888:2000:d::a6]:40997 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82804 On 12/22/2014 12:10 PM, Chris Angelico wrote: > On Tue, Dec 23, 2014 at 2:18 AM, Steven D'Aprano > wrote: >> Chris Angelico wrote: >> >>> On Mon, Dec 22, 2014 at 8:21 PM, Steven D'Aprano >>> wrote: >>>> If the called function has side-effects, a list comp is not a good >>>> solution. >>> >>> Hmm. I'm not so sure about that. Side effects are fine in a list comp, >>> as long as you're making use of the return values. For instance, if >>> you have a function that inserts a record to a database and returns >>> the new ID, you could reasonably use a list comp to save a bunch of >>> records and get a list of IDs for subsequent use. >> >> I hear what you are saying, but a list comprehension is a functional idiom. In particular, it is a way to hide the mutational, non-functional, .append that is a necessary part of constructing an array of references in contiguous memory on a physical machine. >> To mix functional and procedural idioms in the one expression is rather >> icky. A list comp with side-effects in the target expression is definitely a subversion of the original intent. Whether Python programmers should respect that intent is another matter. Limiting the opinion/recommendation to single expressions makes it more defensible than something broader. >> Better to use one or the other but not both simultaneously. >> I'll accept that this is a weak recommendation though. > In my opinion, trying to separate functional and procedural idioms is > like trying to separate 'for' loops and recursion; they're two tools A for loop with a recursive call in its body is not 'one expression'. Moreover, it is easy to claim that driving multiple recursion with a for loop is clearer than using the recursive equivalent of a for loop to drive the 'horizontal' repetition. Untested example for preorder traversal of a tree where nodes have ordered children: def preorder(node): yield node for child in node.children(): # horizontal repetition yield from preorder(child) # vertically nested repetition I do not see any such gain over Ganesh Pal's original for-loop code. > that can be used separately or together, in whatever way makes the > most sense. Given that side-effecting functions are a mess in > functional programming anyway, of course they cause problems for > functional idioms; but if it's okay to have side effects at all, it > ought to be okay to have side effects of a list comp. I see this as equivalent to "If it is okay to have separated side effects, it ought to be okay to have mixed-in side effects." I am not claiming that I would *never* use side effects in a list comp, but I am sympathetic to the objection. -- Terry Jan Reedy