Rebinding variable, despite global statement

B

Brian Leair

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
 
P

Peter Otten

Brian said:
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
 
B

Brian Leair

I am using "from MyModule import *", (yes, yes, I know)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top