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


Groups > comp.lang.python > #17192

Re: Overriding a global

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.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.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; '(except': 0.05; 'used.': 0.07; 'block.': 0.09; 'builtin': 0.09; 'namespace': 0.09; 'though:': 0.09; 'def': 0.13; 'meaningful': 0.13; 'dir"': 0.16; 'error).': 0.16; 'guessing': 0.16; 'logger.': 0.16; 'naming': 0.16; 'objects*': 0.16; 'subject:Overriding': 0.16; 'subject:global': 0.16; 'warn': 0.16; 'wrong)': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'fine,': 0.23; 'implicit': 0.23; 'cc:2**0': 0.24; 'besides': 0.24; "i'm": 0.26; 'expect': 0.26; 'import': 0.27; 'fact': 0.27; 'code,': 0.27; 'fix': 0.27; 'variable': 0.28; 'fine.': 0.29; 'looks': 0.29; "skip:' 10": 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'example': 0.29; '"in': 0.30; 'usually': 0.31; 'idea': 0.32; 'header:User-Agent:1': 0.33; 'this.': 0.33; 'named': 0.33; 'it?': 0.33; 'object': 0.33; 'points': 0.34; 'someone': 0.34; 'but,': 0.34; "we're": 0.34; 'statement,': 0.34; 'changing': 0.35; 'something': 0.35; 'problem.': 0.36; 'thread': 0.37; 'skip:" 10': 0.37; "there's": 0.37; 'think': 0.37; 'could': 0.37; 'using': 0.38; 'prepared': 0.39; 'difficult': 0.39; 'option': 0.39; 'should': 0.39; "it's": 0.40; 'might': 0.40; 'within': 0.60; 'more': 0.61; 'quick': 0.61; '2011': 0.61; 'matter': 0.61; 'total': 0.61; 'your': 0.61; 'back': 0.62; 'our': 0.64; 'believe': 0.65; 'here': 0.65; 'stated': 0.68; 'received:62': 0.70; 'funny': 0.76; 'why?': 0.77; 'assignement': 0.84; 'put,': 0.84
X-IronPort-AV E=Sophos;i="4.71,351,1320620400"; d="scan'208";a="41402"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Wed, 14 Dec 2011 11:14:53 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To Joshua Landau <joshua.landau.ws@gmail.com>
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>
In-Reply-To <CAN1F8qWRN8_ktQP1WRgPPQaG=mMV=TizcTpoRb=+1RyhznnvOg@mail.gmail.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.3631.1323857696.27778.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323857696 news.xs4all.nl 6878 [2001:888:2000:d::a6]:33235
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17192

Show key headers only | View raw


Joshua Landau wrote:
> On 13 December 2011 13:30, Jean-Michel Pichavant 
> <jeanmichel@sequans.com <mailto:jeanmichel@sequans.com>> wrote:
>
>     writing
>
>     x = 1
>
>     def spam():
>       x = 2
>
>     is in general a bad idea. That was my point.
>
>
> Why? I have a few (probably wrong) guesses.
>
> Because you expect it to be the same every time you use it?
> Well, then this should be "in general a bad idea":
> x = 1; print(x); x = 2; print(x)
you're changing the value of x, that's fine. In the example above, the 
assignement is not the problem. The problem is that you create 2 
different 'x', one in globals(), and one in locals(). Using the same 
name within 2 implicit namespaces is a bad idead in general because it 
can be difficult to know which one is used.

If you want to have fun, try this code, prepared to be amazed. There's 
something funny with the global statement, it's applied on the whole 
block no matter where it is stated in the block.
x=1 # global

def spam():
    x = 2 # local (or so you may think)
    print x
    global x # I need to use the global one now
    print x
    print locals()

For more fun you could create a 'x' name in __builtin__ and import it so 
that people never know which x you are using.

> Even though it makes total sense to me.
>
> Is it because it's used to different purpose between similarly-looking 
> functions?
> This looks fine, though:
> def func1(): x=1; print(x)
> def func2(): x=2; print(x)
>
> Is it because it looks like a reassignment of the more global x?
> I don't have an example here but, simply put, I don't believe this. We 
> can use "id" as our own local variable without thinking that we're 
> tampering with "__builtins__.id". I don't see it as much of a leap 
> from builtin to global (except that you /*can*/ do "dir = 1; del dir; 
> dir" without error).
>
> That said, I'm sorta' just guessing the reason you might think it's a 
> bad idea.

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. To go back to the original post, having a 'logger' that 
may name 2 different logger object during the execution is a bad idea. 
One quick way to fix it is to name the logger 'currentLogger', this way 
you warn the reader that the logger named by curentLogger may change 
over time.

As someone sugggested in this thread one other option is to use a 
different name for the second logger.

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