By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

truly global variables

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.