473,387 Members | 1,771 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,387 software developers and data experts.

"intermodule-global" variables

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
----
from helper import *
a 5 a=6
a 6 printa()

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

Jul 18 '05 #1
2 4221
ed**@netido.de (Eddy Ilg) wrote:
---- helper.py
a=5

def printa():
global a
print a
----
from helper import *
a 5 a=6
a 6 printa()

5


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
Jul 18 '05 #2

"Eddy Ilg" <ed**@netido.de> wrote in message
news:ma*************************************@pytho n.org...
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
----
from helper import *
a 5 a=6
a 6 printa()

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
Jul 18 '05 #3

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

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.