Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'python': 0.08; '#define': 0.09; '(int': 0.09; '__future__': 0.09; 'def': 0.13; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'illusion': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'subject:Overriding': 0.16; 'subject:global': 0.16; '\xa0for': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'int': 0.18; 'dec': 0.22; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'mask': 0.23; 'though.': 0.23; 'import': 0.27; 'code,': 0.27; 'do.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'odd': 0.29; 'print': 0.29; 'pm,': 0.29; 'construct': 0.30; 'kinda': 0.30; 'chris': 0.30; "i've": 0.31; 'does': 0.32; 'idea': 0.32; 'implement': 0.32; 'sort': 0.33; 'there': 0.33; 'to:addr :python-list': 0.34; 'nested': 0.34; 'visible': 0.34; 'languages': 0.35; 'variables': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'somewhat': 0.38; 'received:209.85': 0.38; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; '2011': 0.61; 'your': 0.61; 'cost': 0.63; 'safe': 0.70; 'surprise': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=knk5LYzoL/rWka5bZwI8AsrKvbBcoCVm9MKypW+Ytfk=; b=J4qb0BgDotvZzcyOet6AfgyT9IuS38671tHM3t8ChTn187dw5F0D6/XB/DAIGVD0co 69qFYOnrkDnuOpb3N0M9x6Pm212Akuu4QEJzoVkmnXCTdNPJYR9UHH16xjzNVeV5MSsA HoA48ReFdVsQSfLADn5vAB8JPHlelmHGfu1qY= MIME-Version: 1.0 In-Reply-To: <4EE890FF.3080708@sequans.com> 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> <4EE890FF.3080708@sequans.com> Date: Wed, 14 Dec 2011 23:21:29 +1100 Subject: Re: Overriding a global From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323865292 news.xs4all.nl 6861 [2001:888:2000:d::a6]:53600 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17201 On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant wrote: > Chris Angelico wrote: >> >> So... it's a bad idea for me to use 'i' many times in my code, with >> the same name having different meanings in different places? In >> languages with infinitely-nesting scopes... > Bad ideas : > > i =3D 5 > > def spam(): > =A0for i,v in enumerate([1,2,3,4]): > =A0 for i,v in enumerate(['a','b', 'c']): > =A0 =A0 print i, v > =A0 print i,v # bad surprise That's my point. It's not safe to do it in Python, because the "inner" local i is the same as the "outer" local i. Python doesn't have block scope the way most C-like languages do. int spam() { for (int i=3D0;i<5;++i) { for (int i=3D2;i<4;++i) write("inner "+i+"\n"); write("outer "+i+"\n"); } } Works perfectly, and the only cost is that variables must be formally decla= red. In Python, you can kinda fudge that sort of thing with nested functions. def spam(): q=3D2 # just to prove that the scopes really are nested for i in range(5): def ham(): for i in range(2,4): print("q =3D %d, i =3D %d"%(q,i)) ham() print("i =3D %d"%i) It's somewhat clunky, but it does give the illusion of block scope. Inners mask outers, outers are visible to inner functions. It's an odd construct though. Very odd. So... I think I've figured out how to implement from __future__ import brac= es. #define { def innerfunc(): #define } innerfunc() And there you are, out of your difficulty at once! ChrisA