Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'result,': 0.07; '[1,': 0.09; 'literal': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"create': 0.16; '24,': 0.16; 'argument,': 0.16; 'bind': 0.16; 'callable': 0.16; 'comp': 0.16; 'foo,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'funcs': 0.16; 'imperative:': 0.16; 'list"': 0.16; 'notation': 0.16; 'reasonably': 0.16; 'structs': 0.16; 'subclass': 0.16; 'subject: \n ': 0.16; 'syntax,': 0.16; 'tuple': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; 'written': 0.21; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'mon,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; '(this': 0.29; "doesn't": 0.30; 'compared': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'though.': 0.31; 'stuff': 0.32; 'implemented': 0.33; 'actual': 0.34; 'maybe': 0.34; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'done,': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'pm,': 0.38; 'previous': 0.38; 'little': 0.38; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'read': 0.60; 'truly': 0.60; 'most': 0.60; 'new': 0.61; 'simple': 0.61; 'more': 0.64; 'series': 0.66; 'side': 0.67; 'mar': 0.68; 'promise': 0.68; 'manner.': 0.74; 'subject:this': 0.83; 'done;': 0.84; 'effects,': 0.84; 'precious': 0.84; 'to:none': 0.92; 'imagine': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=lR32vuTGn82KA0evdlCuuw1IvtapDvp4/dKYgFxiAYw=; b=bkset5gfStw4WRjtOVBhCIKet6GtfBBZzi1QFbXwjm91bRBFHFW66SEVIyIvx7n+3H 07Zm1ruPhR95Wbph2lAv9dLmoXmq62DgjpymIAvMBaqBqF6PXbmfl04/XM8/pLRsFN+S 0TJ0MghWPijmQUNXODIV3QuD372NPRKkynPjkrkJt06OZrmEO/uKYlNMTD/Pq7NmHa9w DlGbG4iQFOuAUu/DoedOFvhogOAewVP+/A/bkoPE/saSft2FwUD9nUXbtX/e/wZLtQBK cQe96L6zwsogxN/KvIo4Lcwa2UuIwck2/q4DQ/dJ7GGOn8nodvbEfFUk7djRxYEgOCUm 5cBg== MIME-Version: 1.0 X-Received: by 10.66.164.229 with SMTP id yt5mr69690980pab.67.1395631652124; Sun, 23 Mar 2014 20:27:32 -0700 (PDT) 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> Date: Mon, 24 Mar 2014 14:27:32 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395631654 news.xs4all.nl 2850 [2001:888:2000:d::a6]:35712 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68842 On Mon, Mar 24, 2014 at 1:35 PM, Rhodri James wrote: >> Would you not consider this to be declarative? >> >> x = [1, 2, 3] > > > I'm not sure I would. I look at that line of code and think of it as > "Create a list...", very much in an imperative manner. Then again, compared > with C structs and typedefs and actual honest-to-God type declarations, > there's precious little in Python I would consider truly declarative. I'm in the declarative group here. Yes, it has to be implemented as creating a list and adding three elements to it, but conceptually, it means "Bind x to a new list with these elements". And as long as that's the end result, I don't care how it's done; the interpreter's most welcome to have a "template list" that it copies, or maybe a literal tuple that gets passed to the list() constructor, or whatever's most efficient. It gets a bit messier when there's stuff with side effects, though. This has to be a bit more imperative: x = [foo(y), bar(y), quux(y)] That means "Call foo, bar, and quux, in that order, each with y as an argument, and bind x to a new list with their return values". And if Python had a simple notation for calling a series of functions, that could be written something like this: funcs = [foo, bar, quux] x = funcs(y) which is looking more declarative again. It's now "Take this list of functions and call them all, and bind x to a list of their return values". (This could be done, with a callable subclass of list. Call it a sort of "implicit map" if you like.) Python doesn't have that syntax, but it does have this: x = [f(y) for f in funcs] and I'd say that's reasonably declarative; it should be read like the previous one: "Take this list of funcs, call each one, and bind x to a list of their return values". And I could imagine a parallel-processing version of a list comp that functions like multiprocessing.Pool.map() and doesn't promise order, which would *definitely* be declarative. ChrisA