473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1550
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
4
by: raylopez99 | last post by:
Why is the same variable local inside a 'foreach' loop yet 'global' in scope (or to the class) outside it? RL class MyClass { int MyMemberArray1; //member variables, arrays, that are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.