Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17213
| 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 | <prvs=322fd6406=jeanmichel@sequans.com> |
| 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 <jeanmichel@sequans.com> |
| User-Agent | Mozilla-Thunderbird 2.0.0.24 (X11/20100328) |
| MIME-Version | 1.0 |
| To | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
| Subject | Re: Overriding a global |
| References | <roy-B236C9.15475310122011@news.panix.com> <mailman.3500.1323551240.27778.python-list@python.org> <roy-EC9997.16105310122011@news.panix.com> <mailman.3542.1323688423.27778.python-list@python.org> <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <mailman.3583.1323770094.27778.python-list@python.org> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <CAN1F8qWRN8_ktQP1WRgPPQaG=mMV=TizcTpoRb=+1RyhznnvOg@mail.gmail.com> <4EE8771D.1050902@sequans.com> <CAPTjJmqiWTvu4pVW7i28KS1bToAnArUj24rMGBkdtzzGJFW=vQ@mail.gmail.com> <mailman.3638.1323864323.27778.python-list@python.org> <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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3642.1323869724.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Overriding a global Roy Smith <roy@panix.com> - 2011-12-10 15:47 -0500
Re: Overriding a global MRAB <python@mrabarnett.plus.com> - 2011-12-10 21:07 +0000
Re: Overriding a global Roy Smith <roy@panix.com> - 2011-12-10 16:10 -0500
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-12 12:13 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-12 21:28 +0000
Re: Overriding a global Dave Angel <d@davea.name> - 2011-12-12 16:43 -0500
Re: Overriding a global Ben Finney <ben+python@benfinney.id.au> - 2011-12-13 09:27 +1100
Re: Overriding a global Dave Angel <d@davea.name> - 2011-12-12 20:46 -0500
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 01:48 +0000
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-12 22:50 -0700
Re: Overriding a global Joshua Landau <joshua.landau.ws@gmail.com> - 2011-12-13 08:34 +0000
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-13 12:34 -0700
Re: Overriding a global Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-13 12:54 -0700
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-13 10:54 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-13 11:15 +0000
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-13 14:30 +0100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 11:14 +0100
Re: Overriding a global Chris Angelico <rosuav@gmail.com> - 2011-12-14 21:32 +1100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 13:05 +0100
Re: Overriding a global Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-14 12:53 +0000
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 14:35 +0100
Re: Overriding a global Chris Angelico <rosuav@gmail.com> - 2011-12-14 23:21 +1100
Re: Overriding a global Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-12-14 18:06 +0100
Re: Overriding a global Terry Reedy <tjreedy@udel.edu> - 2011-12-10 19:14 -0500
Re: Overriding a global Terry Reedy <tjreedy@udel.edu> - 2011-12-10 19:19 -0500
Re: Overriding a global Peter Otten <__peter__@web.de> - 2011-12-11 09:14 +0100
Re: Overriding a global Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2011-12-13 10:15 +0100
csiph-web