473,770 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"intermodul e-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 4251
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******** *************** **************@ python.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
4375
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 don't like the brackets.
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.