Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17192
| Date | 2011-12-14 11:14 +0100 |
|---|---|
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
| Subject | Re: Overriding a global |
| References | (4 earlier) <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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3631.1323857696.27778.python-list@python.org> (permalink) |
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 | 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