Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.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.141 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.75; '*S*': 0.03; 'def': 0.12; 'closure.': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'personally,': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'creating': 0.23; 'header:In-Reply-To:1': 0.27; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'too': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'new': 0.61; 'talking': 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:date:message-id:subject:from:to :content-type; bh=NH98CyOTpTBtIFjFzvMNzdvWEjMwa+QC0Uw8RFevtnQ=; b=WnGGcz/VqJPibM8VI7vGXXb4/8cWkgzdXxN4ai+EWSEpV/XzyT0NXmI2Gk8P1vKBoX 6Bh+GBdxYtFxyfnS6vmNLIm8Ya29URYJcYrxYDxUMbm+4IFx41rMRoM1G/3qRLizU5iu yi8HQfB2KVlzjNc96Sa8ZhKNIVV+nWLKNA1VO/9q+NGVQ3/s16beXNx752Z5UjDtILgO wixjdiP6V1ku+oU8Zr5XAHtfpqAjNW+sdWIHrHvdcW6Scq0CP9T3842iQDxAbXvcOgrx 8l3R0j7JTpO1FZCXVhsSbU2AB1g3AFRWNBCdOBHfh/LEQfGbovCMZIM/spKYV2WxG9t8 2yDQ== MIME-Version: 1.0 X-Received: by 10.52.16.105 with SMTP id f9mr1381872vdd.28.1372912580556; Wed, 03 Jul 2013 21:36:20 -0700 (PDT) In-Reply-To: References: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 4 Jul 2013 14:36:20 +1000 Subject: Re: Default scope of variables From: Chris Angelico To: python-list@python.org 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372912583 news.xs4all.nl 15864 [2001:888:2000:d::a6]:54870 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49809 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. ChrisA