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

truly global variables

Hi
Global variables are not truly global in Python ; they are only global
within a module namespace. I want a variable such that if i change its
value in any module then it should be reflected in other modules too.
What I mean is I wanta varibale say i whose initial value is 1.
If I change value of i in foo.py to say 5 and now I print value of i
in bar.py ,it should print 5.How to do this??
--
Jul 18 '05 #1
4 1465
On 19 Feb 2004 00:47:13 -0800, ke***********@yahoo.com wrote:
I want a variable such that if i change its value in any module then
it should be reflected in other modules too.


Fortunately, this nightmare is not possible in Python.

What you should be doing is implementing the variable in a module that
is imported wherever that variable is needed. This allows you more
control over how the variable is handled; you know there is only one
place where the variable is declared, and any usage of the variable is
explicitly preceded by the module name.

One common place where people think they want global variables but
really want a common module, is configuration variables. Simply make a
single module whose specific purpose is to hold the configuration
variables for your application, then use the config variables via that
module:

import AppConfig
open_screen_window( AppConfig.window_size )
AppConfig.preferred_hostname = None

The next step would be to make an AppConfig class that handles things
like persistence of config changes, but that may be more complexity than
you need.

--
\ "As the most participatory form of mass speech yet developed, |
`\ the Internet deserves the highest protection from governmental |
_o__) intrusion." -- U.S. District Court Judge Dalzell |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
ke***********@yahoo.com wrote:

Global variables are not truly global in Python ; they are only global
within a module namespace. I want a variable such that if i change its
value in any module then it should be reflected in other modules too.
What I mean is I wanta varibale say i whose initial value is 1.
If I change value of i in foo.py to say 5 and now I print value of i
in bar.py ,it should print 5.How to do this??


Provided you're willing to have a dot in the name of the variable,
this is actually quite possible, as Ben shows even though he markets
his approach poorly by saying what you want is not possible. :-)

If you can explain why you wouldn't want to just use a module to
contain your globals, we'll be able to suggest alternatives (and
there are some alternatives, they're just not usually as clean or
simple).

-Peter
Jul 18 '05 #3
On Thu, 19 Feb 2004 10:31:44 -0500, Peter Hansen wrote:
Provided you're willing to have a dot in the name of the variable,


Variable names don't have dots in them. A dot separates the variable
name from its namespace.

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #4
Ben Finney wrote:

On Thu, 19 Feb 2004 10:31:44 -0500, Peter Hansen wrote:
Provided you're willing to have a dot in the name of the variable,


Variable names don't have dots in them. A dot separates the variable
name from its namespace.


I know that. You know that. From the point of view of a newbie who
wants "true globals", it's a moot point...

-Peter
Jul 18 '05 #5

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(); ...
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...
114
by: Andy | last post by:
Dear Python dev community, I'm CTO at a small software company that makes music visualization software (you can check us out at www.soundspectrum.com). About two years ago we went with decision...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.