Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.06; 'compiler': 0.07; 'level,': 0.07; 'none:': 0.07; 'think,': 0.07; '22,': 0.09; 'expectation': 0.09; 'implements': 0.09; 'subject:language': 0.09; 'try:': 0.09; 'will,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'creates': 0.14; '*a,': 0.16; 'assignments.': 0.16; 'expensive,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'keyword,': 0.16; 'lookups': 0.16; 'name):': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'subject: \n ': 0.16; 'terribly': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'module': 0.19; 'cheap': 0.19; 'fit': 0.20; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'module,': 0.24; 'skip': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'handling': 0.26; 'this:': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "d'aprano": 0.31; 'keyerror:': 0.31; 'steven': 0.31; 'class': 0.32; 'handled': 0.32; 'implemented': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'except': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'are,': 0.36; 'starting': 0.37; 'being': 0.38; 'pm,': 0.38; 'bad': 0.39; 'sure': 0.39; 'enough': 0.39; 'how': 0.40; 'new': 0.61; 'more': 0.64; 'different': 0.65; 'series': 0.66; 'mar': 0.68; 'subject:this': 0.83; 'imagine,': 0.84; "it'd": 0.84; 'trick,': 0.84; 'to:none': 0.92 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=MtrqoxC897RVC0UxjvNTBcWNA10jFUbDb9DL/NKFzcg=; b=XRXqDmYQ47gcnyw54CWk09XOu8HYhPv0Hv52fGkOc5qitG1nQf2KvJqqOu4yVBs0b6 hHdOMI9n11yPAFQHUc6oLieVYxABdpAgB+0NLPpu5ZT38U+AT2pHc5r3aAeioLtt38Zx korlg0BMTjf5eHY2I/ulRN4zYBe9IauJ75daihuja6e88xQpiOGWa+7K20Jj+8tqF+5/ cy+pYHm66jGT6FcpnvxKJXWH5FIYSowzIK69+SNzWaBO9QTqt5tYKYMA1iGGyglilT+/ WfSvVSwAMrKB9SDHeSIkQAqnod8yoj4jU8nnQbouDy53pHut4E9xTSLNoG7lcl2VK0up E41g== MIME-Version: 1.0 X-Received: by 10.68.143.231 with SMTP id sh7mr58597951pbb.7.1395464729568; Fri, 21 Mar 2014 22:05:29 -0700 (PDT) In-Reply-To: <532d15e8$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d15e8$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 22 Mar 2014 16:05:29 +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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395464733 news.xs4all.nl 2870 [2001:888:2000:d::a6]:47296 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68748 On Sat, Mar 22, 2014 at 3:47 PM, Steven D'Aprano wrote: > Now I'm not sure precisely how Haskell implements this trick, but it > suggests to me that it creates a different closure each time around the > loop of the comprehension. That could end up being very expensive. It needn't be terribly expensive, if you make a "scope stack" or "scope chain". Imagine, if you will, a system of scopes like this: current_scope = None class Scope: def __init__(self, *a, **kw): self.names = dict(*a, **kw) global current_scope self.parent = current_scope current_scope = self def __getitem__(self, name): try: return self.names[name] except KeyError: if self.parent is None: raise return self.parent[name] def __setitem__(self, name, value): self.names[name] = value def exit_scope(): global current_scope current_scope = current_scope.parent Creating a new scope would be fairly cheap (and a lot more so if this were implemented in C without the object overhead, of course). Lookups would scan up through a series of namespaces, same as they currently do (local, module, builtins), there'd just be more of them. And the compiler could be smart enough to skip creating a scope if there are no assignments. There'd need to be some handling in there for the 'nonlocal' keyword, but my expectation is that 'global' is handled by retaining a reference to the current_scope at module level, and starting the search there for a LOAD_GLOBAL. Each iteration of the loop could begin with Scope() and end with exit_scope(), and there you are, each iteration in its own closable scope. I'm not saying it would be a good thing to do - and it'd be a bad fit for Python, I think, as I said in my other post - but it could be done. ChrisA