Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'syntax': 0.04; 'subject:Python': 0.06; 'nested': 0.07; '22,': 0.09; '[1,': 0.09; 'subject:language': 0.09; 'python': 0.11; 'def': 0.12; '(lambda': 0.16; 'equivalence': 0.16; 'introduces': 0.16; 'iterable': 0.16; 'literal,': 0.16; 'literals.': 0.16; 'loops': 0.16; 'subject: \n ': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'rules': 0.22; 'logical': 0.24; 'mind.': 0.24; 'sorry,': 0.24; 'question': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'object.': 0.31; 'not.': 0.33; 'plain': 0.33; 'moment': 0.34; 'except': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'similar': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'break': 0.61; "you've": 0.63; 'different': 0.65; 'relatively': 0.65; 'talking': 0.65; 'between': 0.67; 'mar': 0.68; 'therefore': 0.72; 'construction': 0.72; 'subject:this': 0.83; 'complexity': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=rxlA4JtAmOEqH9BtZ2bxS1GqhaDsrs0gwPh8KkSff4U=; b=TzGZJrVJUlXtQnBhNLdwsTe2t4jthJCPU/RaLxcYVQJnFmUqfrTH4Oq2Cf4AIgNJdq VwTkMeIt1C91jujdr0mWEfPzYSRMGKgAhZvz4dpaVf/1jNJO14O2H4Dqde64O0+7PwZF Sue8BwPPiF9a+wuYoZH7JFYJzyqQqyUmzBhonXICgoM9akqSX0Nj+RTnXDdiZkIF/i8U Du8c/aSSx0npirxwXYn/OGS6QH7qqiMqphViP9F/bZkuxcQvRfXnd6D+v45tSie5e4Kl Nh1BY5WoMeb0TmGEm4FP3ntlQXEaduFKKxN+p9R+bFU3gmKKVxjpx3QIWXkX8h2tkchj Pjdw== X-Received: by 10.68.129.5 with SMTP id ns5mr156716pbb.147.1395542836501; Sat, 22 Mar 2014 19:47:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <9edb4ea0-5faf-4369-8021-48afa9800a34@googlegroups.com> From: Ian Kelly Date: Sat, 22 Mar 2014 20:46:28 -0600 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395542839 news.xs4all.nl 2867 [2001:888:2000:d::a6]:47704 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68802 On Sat, Mar 22, 2014 at 6:32 PM, Rhodri James wrote: > On Sat, 22 Mar 2014 05:26:26 -0000, Rustom Mody > wrote: > >> Well almost... >> Except that the 'loop' I am talking of is one of >> def loop(): >> return [yield (lambda: x) for x in [1,2,3]] >> or >> return (yield (lambda: x) for x in [1,2,3]) >> or just plain ol >> (lambda x: for x in [1,2,3]) >> IOW loop is an imperative construct, comprehensions are declarative > > > I'm sorry, you've made a logical leap too far here. I understand loops > being imperative, but how are comprehensions declarative? What do they > declare that the loop equivalent doesn't. I'm with Rustom on this point. A list comprehension is a syntax for building a list by declaring a transformation from some other iterable object. Forget comprehensions for a moment and think of literals. Would you not consider this to be declarative? x = [1, 2, 3] A comprehension is syntactically similar to a literal, with just a different type of construction in mind. Where I disagree is on the question of whether Python should therefore break its established closure rules for lambdas that are nested inside comprehensions versus functions that are not. It breaks the equivalence between comprehensions and loops, and to my mind it introduces significant complexity for relatively little gain.