Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.133 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.74; '*S*': 0.00; 'cc:addr:python-list': 0.11; 'def': 0.12; 'closure.': 0.16; 'foo():': 0.16; 'included,': 0.16; 'personally,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'people,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'class': 0.32; 'stuff': 0.32; 'guess': 0.33; 'skip:_ 10': 0.34; 'agree': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'functions.': 0.36; 'too': 0.37; 'pm,': 0.38; 'skip:u 10': 0.60; 'new': 0.61; "you're": 0.61; 'july': 0.63; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'reads': 0.68; 'jul': 0.74; '2:30': 0.84; 'hardly': 0.84; '2013': 0.98 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 :cc:content-type; bh=/+cVQg84Uheeb8twgaCCkfz4T9cVjjr09VM4LN3kpk8=; b=qlGwZJAYCyB5gK3Wlm7P123JQ3ZmVrwoeUahyHTHO5qkzRwVJgp5q35kM2mt/k9+Lw sEc5oMRCtR3dcWXq4kRdgWskoJ9PhGGFLGoB7LUbxSsw1/44h4+eOsDvacmM6j3DK2it eR/M70FFJvScqNUBi83OSH1Nt+mof8TTRcQZ1QGzGEcXcuADojkq5BWpSThhFFsj3LAS x2yjDjW7coCSbvFzggGVrAPe4FDx+WJE3fif1yZ7Htws9cnib85T12ORpePLtcL6aILi k4FriorYBS9r78KH04w7E8l3cm5aUazNmsj0kzTz+yTsmwZ6/+QZmhjKhoNln08bCerk GWzw== X-Received: by 10.112.5.199 with SMTP id u7mr2661962lbu.67.1372914584280; Wed, 03 Jul 2013 22:09:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> From: Joshua Landau Date: Thu, 4 Jul 2013 06:09:04 +0100 Subject: Re: Default scope of variables To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Cc: python-list 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372914587 news.xs4all.nl 16012 [2001:888:2000:d::a6]:36517 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49811 On 4 July 2013 05:36, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau > wrote: >> That said, I'm not too convinced. Personally, the proper way to do >> what you are talking about is creating a new closure. Like: >> >> for i in range(100): >> with new_scope(): >> for i in range(100): >> func(i) >> func(i) # Using i from original loop >> >> But it's not like Python'll ever support that. >> > > def foo(): > for i in range(3): > print("outer",i) > def inner(): > for i in range(4): > print("inner",i) > inner() > print("outer",i) > > That works, but you then have to declare all your nonlocals, and it > hardly reads well. Unfortunately that's what people, I included, end up doing. Stuff like: def paranoia(...): def safe_recursive(...): safe_recursive(...) return safe_recursive safe_recursive = paranoia() is blimmin ugly. Then you're only really left with class safe_recursive: def __call__(self, ...): self(...) which only solves it for recursive functions. I guess this means I actually agree with your sentiment, just not the specifics.