469,329 Members | 1,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Rebinding variable, despite global statement

I am using "from MyModule import *", (yes, yes, I know)

MyModule has a variable "g_my_var" at the "global" scope.

In the code that performs the import, I have a function that has the
statement
"global g_my_var". Despite this, when I try to assign to g_my_var it
appears I am rebound to a different object.
Beyond philosophical arguments about not using a "global" variable, is
there a real reason why I can't assign to the global "g_my_var". I'm
using python 2.3.2.

One workaround is to place getter/setters in MyModule, but I was still
surprised by this behavior.


----------------------- MyModule.py -----------------------------
g_my_var = 142

def UtilityFunction ():
print "Inside UtilityFunction", g_my_var
print "Inside UtilityFunction Id is", id (g_my_var)
return

----------------------- MyProgram.py -----------------------------
from MyModule import *

def ProgramFunction ():
global g_my_var
UtilityFunction ()
print "In ProgramFunction", g_my_var
print "In ProgramFunction Id is", id (g_my_var)
print "Now assigning a value of 42 in ProgramFunction."
g_my_var = 42
print "After assignment, in ProgramFunction", g_my_var
print "After assignment, in ProgramFunction Id is", id (g_my_var)
UtilityFunction ()
return

print "Excuting Main Program"
UtilityFunction ()
ProgramFunction ()

=============== Output ======================
Excuting Main Program
Inside UtilityFunction 142
Inside UtilityFunction Id is 7625912
Inside UtilityFunction 142
Inside UtilityFunction Id is 7625912
In ProgramFunction 142
In ProgramFunction Id is 7625912
Now assigning a value of 42 in ProgramFunction.
After assignment, in ProgramFunction 42
After assignment, in ProgramFunction Id is 8008553
Inside UtilityFunction 142
Inside UtilityFunction Id is 7625912
Jul 18 '05 #1
2 1405
Brian Leair wrote:
I am using "from MyModule import *", (yes, yes, I know)

MyModule has a variable "g_my_var" at the "global" scope.

In the code that performs the import, I have a function that has the
statement
"global g_my_var". Despite this, when I try to assign to g_my_var it
appears I am rebound to a different object.
Beyond philosophical arguments about not using a "global" variable, is
there a real reason why I can't assign to the global "g_my_var". I'm
using python 2.3.2.

One workaround is to place getter/setters in MyModule, but I was still
surprised by this behavior.


Let's assume the current module is the main module __main__. After

from MyModule import *

or bettter

from MyModule import g_my_var

you have two independent bindings to the same object. You can think of it as
the two-step process

import MyModule
g_my_var = MyModule.g_my_var
# del MyModule

That you are using the same name in both __main__ and MyModule has no effect
on the general structure. With

def f():
global g_my_var
g_my_var = "some value"
f()

you only rebind g_my_var in the current module's global namespace (__main__
in the example).

While you should always think twice before modifying a module from the
outside, a reasonably clean way is to do it like so:

import MyModule

def f():
MyModule.g_my_var = "some value
f()

Peter


Jul 18 '05 #2
> > I am using "from MyModule import *", (yes, yes, I know)

In the code that performs the import, I have a function that has the
statement
"global g_my_var". Despite this, when I try to assign to g_my_var it
appears I am rebound to a different object.


you have two independent bindings to the same object. You can think of it as
the two-step process

import MyModule
g_my_var = MyModule.g_my_var
# del MyModule

That you are using the same name in both __main__ and MyModule has no effect
on the general structure. With


Ah, thanks. This makes more sense now.

My module has a list of symbols that are availabel to it, the module's
dict. The keyword "global" is just a hint to assign to the symbol
stored in the module's dict instead of the function. I also didn't
quite realize that the "from X import " is really just adding entries
in my module's dict. In effect making copies. The g_my_var gets an
inital value from the import, but I really have MyPorgram.g_my_var and
MyModule.g_my_var and these names are independent.

A coworker commented that if g_my_var was bound to a modifiable object
(list, class, etc.) then the changes made to that object would be seen
in both places.

Thanks again.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Russ Chinoy | last post: by
11 posts views Thread by gg9h0st | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Florian Loitsch | last post: by
112 posts views Thread by istillshine | last post: by
4 posts views Thread by raylopez99 | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.