473,320 Members | 2,020 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,320 software developers and data experts.

Global variables..

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
Jul 18 '05 #1
2 1369
Ishwar Rattan wrote:
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

Jul 18 '05 #2
Jeff Shannon <je**@ccvcorp.com> wrote in message news:<10*************@corp.supernews.com>...
Ishwar Rattan wrote:
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?

Recall the old adage that if one can't do it then one can always teach :-)
Thanks for the answer.
-ishwar
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.