473,506 Members | 16,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globals between modules ?

Does Python allow a 'global' variable that can be used between different
modules used within an app ?

Pete

Jul 18 '05 #1
2 1280
In article <42**********************@news.newshosting.com>,
PGMoscatt <pg*******@optushome.com.au> wrote:
Does Python allow a 'global' variable that can be used between different
modules used within an app ?

Pete


You could explicitly import __main__ and use that as a global namespace
(see example below). Or (perhaps a better idea), you could have a module
dedicated to global storage, and import that in all your other modules
instead of importing __main__.

---------------------------------
Roy-Smiths-Computer:play$ cat global.py
#!/usr/bin/env python

import a
import b

a.set()
print "x = %s" % x
print "b.get() = %s" % b.get()
---------------------------------
Roy-Smiths-Computer:play$ cat a.py
import __main__

def set():
__main__.x = "hello"
---------------------------------
Roy-Smiths-Computer:play$ cat b.py
import __main__

def get():
return __main__.x
---------------------------------
Roy-Smiths-Computer:play$ ./global.py
x = hello
b.get() = hello
Jul 18 '05 #2
Roy Smith wrote:
In article <42**********************@news.newshosting.com>,
PGMoscatt <pg*******@optushome.com.au> wrote:
Does Python allow a 'global' variable that can be used between different
modules used within an app ?

Pete


You could explicitly import __main__ and use that as a global namespace
(see example below). Or (perhaps a better idea), you could have a module
dedicated to global storage, and import that in all your other modules
instead of importing __main__.

---------------------------------
Roy-Smiths-Computer:play$ cat global.py
#!/usr/bin/env python

import a
import b

a.set()
print "x = %s" % x
print "b.get() = %s" % b.get()
---------------------------------
Roy-Smiths-Computer:play$ cat a.py
import __main__

def set():
__main__.x = "hello"
---------------------------------
Roy-Smiths-Computer:play$ cat b.py
import __main__

def get():
return __main__.x
---------------------------------
Roy-Smiths-Computer:play$ ./global.py
x = hello
b.get() = hello


Thanks Roy.... yep, that makes sense.

Thanks again.

Pete
Jul 18 '05 #3

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

Similar topics

9
5342
by: Martin Drautzburg | last post by:
My wxPython program starts execution in mainFrame.py like this class MainApp(wxApp): def OnInit(self): self.mainFrame = MainFrame(None) self.mainFrame.Show() self.SetTopWindow(self.mainFrame)...
18
1371
by: fred.dixon | last post by:
i have read the book and searched the group too ---------------------------------------------- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2...
11
2829
by: Matt | last post by:
Hello, I'm rather new to C++ and have a question about globals. What is the big deal about them? They make things much easier to program because you don't have to worry about all the passing...
18
2926
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
5
1283
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
0
7218
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,...
0
7103
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...
0
7307
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
7370
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...
0
7478
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...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
409
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...

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.