"intermodule-global" variables

E

Eddy Ilg

Hi,


I am having a problem with an application I am writing:

I have 2 scripts called 'conf' and 'build'. Both define
a variable named 'root' and import a module named 'helper'.

In the helper module I want to access the root variable, that is
_either_ in conf _or_ build. How can I do this?
I just want the root variable to be global for all modules.
(I don't want to put root in helper, since that would make no sense at
all)


I also tried this:

---- helper.py
a=5

def printa():
global a
print a
----
5


Why does this not work? Why are there suddenly two variables a? One
for helper.py (stays 5) and a global one (became 6)? This is a bit
irritating. Didn't find it in any documentation


Thanks

Eddy
 
H

Hung Jung Lu

---- helper.py
a=5

def printa():
global a
print a

Change that to

---- helper.py
import sys
sys.a = 5
def printa():
print sys.a
----
from helper import *
sys.a = 6
printa()

----------------------------
There are three namespaces in Python at any given moment: local,
global, and built-in. You need to go through the built-in namespace,
for inter-module purposes. You could choose any other module instead
of sys.

A more drastic approach is to directly tweak the __builtin__ module.
That simplifies the reading (retrieval), but not the writing
(assignment).

import __builtin__
__builtin__.a = 4
print a

regards,

Hung Jung
 
H

Humpty Dumpty

Eddy Ilg said:
Hi,


I am having a problem with an application I am writing:

I have 2 scripts called 'conf' and 'build'. Both define
a variable named 'root' and import a module named 'helper'.

In the helper module I want to access the root variable, that is
_either_ in conf _or_ build. How can I do this?
I just want the root variable to be global for all modules.
(I don't want to put root in helper, since that would make no sense at
all)

There's probably something wrong with the design. If helper shouldn't know
about conf or build, then why should it know about a variable that conf or
build use? Rather, conf build and helper should all refer to a variable in a
fourth module.
I also tried this:

---- helper.py
a=5

def printa():
global a
print a
----

5


Why does this not work? Why are there suddenly two variables a? One
for helper.py (stays 5) and a global one (became 6)? This is a bit

Because you have rebinded a. This can be a confusing aspect of Python at
first, that you access objects through a reference rather than directly. So
when you did a=6, you rebinded the module-level a to a new value, but this
doesn't affect the other reference, in helper.py, that was also called 'a'.
Same thing would happen for two classes:


class A:
def __init__(self):
self.a =1

class B:
def __init(self, a):
self.b = a

aa = A()
bb = B(aa.a)

Now both bb.b and aa.a refer to the same value. Now if you do aa.a = 2,
that won't affect bb.b, rather bb.b and aa.a now refer to diferent
variables.

Oliver
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top