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


Groups > comp.lang.python > #17200

Re: Overriding a global

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

Show all headers | View raw


Chris Angelico wrote:
> On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant
> <jeanmichel@sequans.com> wrote:
>   
>> The problem makes little sense when using names like x or func1. Besides
>> namespace issues, naming 2 *different objects* with the same meaningful name
>> is usually a bad idea and points the fact that your names are no that
>> meaningful.
>>     
>
> 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 (one of Python's great lacks,
> imho), I've often had three different variables with the same names,
> all perfectly valid, and all doing what they should. It's not just
> loop indices - I used to have a piece of code in which 'res' was a
> MySQL resource being processed in a loop, and I had three nested
> loops. Each time I referenced 'res', it used the innermost available
> resource, which was precisely what I wanted. If I'd arbitrarily had to
> guarantee that all variable names were unique, I would have had
> completely unnecessary fiddling around.
>
> Python wouldn't let you do that with three nested 'res'es in one
> function, but you can perfectly reasonably have a global and a local.
> It makes perfect sense... which is a good reason for keeping it legal.
>
> ChrisA
>   

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


good ideas :

# global
nameThatWillNotBeUsedlocally = 'foo'

def spam():
  for qtyIndex, quantity in enumerate([5,6,3,1]):
    for fruitIndex, fruit in enumerate(['orange', 'banana']):
      print fruitIndex, fruit
    print qtyIndex, quantity

While a lot of people still use i,j,k,v to handler values and indexes, I 
think it's a bad idea. I'm just stating an opinion from my personnal 
python experience. I know some people can successfully use the hard way.

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