Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.018 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'syntax': 0.04; 'explicitly': 0.05; 'javascript,': 0.07; 'variables': 0.07; 'coders': 0.09; 'global,': 0.09; 'wrong,': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; '"a"': 0.16; '"b"': 0.16; 'accident.': 0.16; 'closure.': 0.16; 'declaration': 0.16; 'func': 0.16; 'local.': 0.16; 'ought': 0.16; 'personally,': 0.16; 'say.': 0.16; 'skip:j 30': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'thu,': 0.19; 'coding': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'lets': 0.24; 'cc:2**0': 0.24; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; "doesn't": 0.30; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; 'easier': 0.31; 'that.': 0.31; 'bad.': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'quite': 0.32; 'problem': 0.35; 'agree': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'functions.': 0.36; 'var': 0.36; 'should': 0.36; 'too': 0.37; 'desirable': 0.38; 'pm,': 0.38; 'even': 0.60; 'around.': 0.60; 'is.': 0.60; 'free,': 0.61; 'new': 0.61; 'matter': 0.61; 'name': 0.63; 'kind': 0.63; 'july': 0.63; 'more': 0.64; 'talking': 0.65; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'default': 0.69; 'respect': 0.70; 'safe': 0.72; 'jul': 0.74; 'careless': 0.84; 'common,': 0.84; 'compiles': 0.84; 'safer,': 0.84; 'shadow': 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=0SQ2NdwHbpZ3h97YU4bs2AX/WPAL7l50Vjj2t/p2wyI=; b=Hzl5upiOS0wQKuwtVPgrDnLw35odAOweIkZM9YiVtJy6lpAPS/x3IQBk10kHwNw/NS bPZHjj+oj8NSL8uObcJzv5HGE9BY7yy0Mt48k8V/aeLLGmi93/+YyN51W++Yoy77mjcv HvP6otfgepRmMn28nhZR1OW0mlBNrFlULd7exmYUP5qfmlCKZwgW32F3LZbGi00y7W7s tuHO6bnXu1SxY92FEogI25F5N8hgNnvS+UC+Yb7u1Wsw4+2KLRk+59fdfdfUAwEwiOay uGG/8lIh/+FjsxPyPs8mqOlIaAOBn7YqnQukwniJi5Yj7uCJrEai/5HKKfMKXKgx9G5D lVFw== X-Received: by 10.152.20.40 with SMTP id k8mr1978613lae.25.1372912244253; Wed, 03 Jul 2013 21:30: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 05:30:03 +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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372912251 news.xs4all.nl 15899 [2001:888:2000:d::a6]:50345 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49807 On 4 July 2013 05:07, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 1:27 PM, Steven D'Aprano > wrote: >> With respect to the Huffman coding of declarations, Javascript gets it >> backwards. Locals ought to be more common, but they require more typing. >> Locals are safer, better, more desirable than globals, and so it should >> be easier to use locals than globals, not the other way around. Having to >> declare "give me the safe kind of variable", while getting the harmful[1] >> kind of variable for free, strikes me as arse-backwards. Lazy, naive or >> careless coders get globals[2] by default or accident. That's bad. > > I agree that Javascript has it wrong, but not quite for the reason you > say. The advantage of declaring locals is a flexibility: you can have > multiple unique variables with the same name in the same function. > Python lets you do that across but not within functions. > > But Javascript/ECMAScript/whatever doesn't give you that. A var > declaration makes it function-local, no matter where the declaration > is. Coffeescript, which compiles to Javascript, "fixes" the problem Steven brought up by automatically declaring variables so that you don't have to. But what do you think this does?: a = 1 func = -> a = 2 b = 2 The "a" in "func" is global, the "b" is local. And Coffeescript *doesn't let* you shadow even if you explicitly want to. There just isn't syntax for it. 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.