Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17213

Re: Overriding a global

Date 2011-12-14 14:35 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Overriding a global
References (8 earlier) <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>
Newsgroups comp.lang.python
Message-ID <mailman.3642.1323869724.27778.python-list@python.org> (permalink)

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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