Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'builtins': 0.07; 'function,': 0.07; 'python': 0.08; 'globals': 0.09; 'sake': 0.09; 'programmer': 0.10; 'def': 0.13; 'meaningful': 0.13; 'accident"': 0.16; "doesn't.": 0.16; 'encouraged,': 0.16; 'fruit': 0.16; 'function;': 0.16; 'locally,': 0.16; 'namespace,': 0.16; 'naming': 0.16; 'scope.': 0.16; 'subject:Overriding': 0.16; 'subject:global': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'cc:addr:python-list': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'lines,': 0.18; 'cc:no real name:2**0': 0.20; 'programming': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; '+0100,': 0.23; 'accidentally': 0.23; 'globally': 0.23; 'cc:2**0': 0.24; "python's": 0.24; 'function': 0.27; 'variable': 0.28; 'not.': 0.28; 'affected': 0.29; 'intellectual': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'example': 0.29; 'lines': 0.30; 'anyone': 0.31; "i'll": 0.31; 'does': 0.32; 'adds': 0.32; 'actual': 0.32; "can't": 0.32; "isn't": 0.33; 'header:User- Agent:1': 0.33; 'named': 0.33; 'that,': 0.33; 'there': 0.33; 'career': 0.34; 'rules': 0.34; 'loop': 0.34; 'too': 0.34; 'conventions': 0.34; 'languages': 0.35; 'apply': 0.35; 'especially': 0.35; 'variables': 0.37; 'but': 0.37; 'using': 0.38; 'steven': 0.38; 'etc.)': 0.39; 'should': 0.39; 'called': 0.40; 'happens': 0.40; 'more': 0.61; '2011': 0.61; 'your': 0.61; 'free': 0.64; 'received:62': 0.70; 'risk': 0.71; 'design.': 0.73; 'suited': 0.73; 'topic,': 0.77; "'foo'": 0.84; 'complex,': 0.84; 'distinguish': 0.84; 'expressive': 0.84; 'irrelevant.': 0.84; 'mcdonalds': 0.84; 'unique.': 0.84; 'prone': 0.91; 'worrying': 0.91; 'surprise': 0.97 X-IronPort-AV: E=Sophos;i="4.71,352,1320620400"; d="scan'208";a="41647" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Wed, 14 Dec 2011 14:35:14 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: Steven D'Aprano Subject: Re: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323869724 news.xs4all.nl 6868 [2001:888:2000:d::a6]:39626 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17213 Steven D'Aprano wrote: > On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: > > >> Bad ideas : >> >> i = 5 >> >> def spam(): >> for i,v in enumerate([1,2,3,4]): >> for i,v in enumerate(['a','b', 'c']): >> print i, v >> print i,v # bad surprise >> > > The bad surprise happens because you are using the same name twice in > *one* namespace, the local scope. This example has nothing to do with > local/global name clashes: the existence of global i is irrelevant. > Python's scoping rules work correctly, and global i is not affected by > the local i. > > Programming languages use multiple namespaces so that you don't need to > make your variable names globally unique. There are languages that don't > distinguish between local and global. Python is not one of them. The > programmer should feel free to use local names without worrying too much > if they accidentally use a global name. > > Having said that, re-using names isn't *entirely* risk free, because if > you use a global name locally, and then try to *also* access the global > name, you will fail. This is called shadowing, and the problem with > shadowing is when you do it by accident. (Newbies are particularly prone > to this, especially when they call variables "str", "list", etc.) But it > is the "by accident" part that is dangerous: there is nothing wrong with > shadowing globals or builtins when you do it by design. > > > >> good ideas : >> >> # global >> nameThatWillNotBeUsedlocally = 'foo' >> > > Oh please. Names can be too long as well as too short. > > > >> def spam(): >> for qtyIndex, quantity in enumerate([5,6,3,1]): >> for fruitIndex, fruit in enumerate(['orange', 'banana']): >> print fruitIndex, fruit >> print qtyIndex, quantity >> > > More sensible naming conventions are to be encouraged, but verbose names > just for the sake of verbosity is not. spam() is a five line function; if > the programmer can't keep track of the meaning of loop variables i and j > over five lines, perhaps they should consider a change of career and get > a job more suited to their intellectual prowess. I hear McDonalds is > hiring. > > If spam() were larger and more complex, then more expressive names would > be valuable. But in the simple example you give, it just adds noise. > > The next time I'll illustrate meaningful names, I'll write a 3000 lines function, just to be sure no one states that my point does'nt apply to a function named spam which only counts from 1 to 3. And don't answer that the spam function above does not count from 1 to 3, I know it doesn't. For anyone interested in the actual topic, a good reading is http://tottinge.blogsome.com/meaningfulnames/#Mult_Meanings JM