Global variables..

I

Ishwar Rattan

Say I have two pythom modules in separate files:
main.py -> contains main() modules and uses a global variable (say a)
incr.py -> contains incre() that changes the value of global variable a
(called in main())

Is there a way to reflect the change in main()?
I tried it by placing variable a in a file of its own a.py
which imported in both main() and incr():
---main.py--
from a import *
from incr import *

def main():
global a
print a
incr()
print a
---incr.py--
from a import *

def incr():
global a
a = a + 100
print 'incr: ', a
---a.py--
a = 123
---

Second print in main() displayes 123 -> as if a was not updated by incr()

Any ideas to make it work?
-ishwar
 
J

Jeff Shannon

Ishwar said:
Say I have two pythom modules in separate files:
main.py -> contains main() modules and uses a global variable (say a)
incr.py -> contains incre() that changes the value of global variable a
(called in main())

Is there a way to reflect the change in main()?

Hm, second time today that this question has been asked. (And both
posts sport an .edu address, too.) Coincidence?

In answer to your question -- not with bare names, unless you're willing
to do (at a minimum) some nasty black magic (hacking the stack frame and
the like). (And no, I don't know offhand how to do this black magic,
nor would I wish to use it to allow such behavior. Globals are almost
always a bad idea.)

You can accomplish much the same ends in a much cleaner/clearer fashion
by avoiding the 'from a import *' in favor of 'import a', and referring
to the global variabler as an attribute of a: 'a.a = a.a + 100'. (This
is generally the preferred way of sharing variables across modules.)

If you insist on import * to get bare names (which I would strongly
recommend against), then you can still mutate shared objects and see the
effects of that mutation in different modules. For instance, you could
bind the name a in a.py to an object that would allow itself to be
incremented and decremented (using __iadd__(), etc), and which can be
coaxed to resolve to that value when used in an expression. This would
allow you to modify the value by using 'a += 100', for example.
However, the moment you rebind the name (as you do in your code, with 'a
= a + 100'), you are creating a new variable that is local to the
current scope, overriding the global a and potentially raising an
UnboundLocalError.

So the short answer to your question is no, and you probably don't want
to anyhow.

Jeff Shannon
Technician/Programmer
Credit International
 
I

Ishwar Rattan

Jeff Shannon said:
posts sport an .edu address, too.) Coincidence?
Recall the old adage that if one can't do it then one can always teach :)
Thanks for the answer.
-ishwar
 

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

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,687
Latest member
IngridXxj

Latest Threads

Top