Overriding a global

Joshua Landau joshua.landau.ws at gmail.com
Tue Dec 13 14:01:42 EST 2011


On 13 December 2011 13:30, Jean-Michel Pichavant <jeanmichel at 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)
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111213/6118de6a/attachment.html>


More information about the Python-list mailing list