truly global variables

K

ketulp_baroda

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??


--
 
B

Ben Finney

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.
 
P

Peter Hansen

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
 
B

Ben Finney

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.
 
P

Peter Hansen

Ben said:
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
 

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
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top