Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"this': 0.03; 'c++,': 0.07; 'granted,': 0.07; 'variables': 0.07; 'coders': 0.09; 'coding,': 0.09; 'variables.': 0.09; 'wrong,': 0.09; 'python': 0.11; 'mostly': 0.14; '(int': 0.16; 'accident.': 0.16; 'declaration': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals': 0.16; 'it;': 0.16; 'ought': 0.16; 'redundant.': 0.16; 'say.': 0.16; 'scope,': 0.16; 'skip:j 30': 0.16; 'somewhere.': 0.16; 'with;': 0.16; 'flexibility': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'thu,': 0.19; 'coding': 0.22; 'lets': 0.24; 'visible': 0.24; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'easier': 0.31; 'that.': 0.31; 'bad.': 0.31; 'convenience': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'void': 0.31; 'file': 0.32; 'quite': 0.32; 'agree': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'c++': 0.36; 'functions.': 0.36; 'var': 0.36; 'should': 0.36; 'level': 0.37; 'system,': 0.38; 'desirable': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'around.': 0.60; 'is.': 0.60; 'free,': 0.61; 'matter': 0.61; 'further': 0.61; 'name': 0.63; 'kind': 0.63; 'more': 0.64; 'within': 0.65; 'default': 0.69; 'respect': 0.70; 'safe': 0.72; 'jul': 0.74; 'careless': 0.84; 'common,': 0.84; 'difference.': 0.84; "everything's": 0.84; 'safer,': 0.84; 'hand,': 0.93; '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=ObOknOlwDerafV0bI+Sul5ULSuZuID+YIiqeVo87Aso=; b=emZAFhA5hEzkJ050MDyrbfRcpLOWKc6miGuo0y74h/27RuQA4XSBml5dmtq72KBazZ JQthYZD02k2YcfIl3ZKB4qYfxoCXgOy9/BzpVaQUfOpTGHeU+u69N7mtWB+BYbF7jXXC lKOfpgmRPMJ+66T+O1oAQsgSmOYRZfXNMV02Af2XTw3dSTn2dMkYOrgH6M/6GehgrSpK 6uXBSJQdmtsY/o5Ab8CXVn098W/oTyaFo1XPPB/QkVcQhkclz/de8bURWh5PWBmchY14 gwDn27qYlixAZ1CI9qiHRKVH1SEY2thJTliANwA/hdtyAkKuTPUeR5cbnRbaAFwbIsA+ aZoQ== MIME-Version: 1.0 X-Received: by 10.52.16.105 with SMTP id f9mr1326803vdd.28.1372910875977; Wed, 03 Jul 2013 21:07:55 -0700 (PDT) In-Reply-To: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> References: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 4 Jul 2013 14:07:55 +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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372910878 news.xs4all.nl 15904 [2001:888:2000:d::a6]:38315 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49804 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. That's pointless. C++, on the other hand, lets you do this: void somefunc() { for (int i=0;i<10;++i) { // do something with outer i for (int i=0;i<4;++i) { // do something with inner i } // outer i is visible again } // neither i is visible } Also, C++ overlays the "this is local" declaration with the "this contains this type" declaration, which neither Python nor Javascript bothers with; that makes the declaration feel less redundant. Granted, this flexibility is mostly of value when writing huge functions with complex nesting, but it is something that I find convenient. In terms of Huffman coding, every C++ variable must be declared, somewhere. It's not a matter of declaring globals or declaring locals - you just declare variables. If you declare them at file scope, they're globals; if at function scope, they're locals. There's really no difference. Everything's visible at its own level and those further in, and not those further out. I do see the convenience of the Python system, and I do like it; but someone needs to speak up for the foolish and pathetic :) ChrisA