473,387 Members | 1,624 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.

Inter-module globals

Hi,

I want to use globals that are immediately visible in all modules. My
attempts to use "global" haven't worked. Suggestions?

Anton
Mar 9 '06 #1
3 1491
Anton81 <fo***@anton.e4ward.com> writes:
I want to use globals that are immediately visible in all modules. My
attempts to use "global" haven't worked. Suggestions?


Use a module and a class variables for that. Import your module and
read/update class variables as you need them.
--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 9 '06 #2
Anton81 wrote:
Hi,

I want to use globals that are immediately visible in all modules. My
attempts to use "global" haven't worked. Suggestions?

Anton


I think a dictionary would work here
as well as list but not strings and int's

# module1
settings = {
"release" : "1.0",
"blabla" : None,
}

# module2
import module1 as settings
print settings.settings["release"]
settings.settings["blabla"] = True
# module3
import module1 as settings
settings.settings["blabla"] = False

Regards

Mar 9 '06 #3
> Use a module and a class variables for that.

I think we could manage a little example too ;-) This one is a fictitious little game project where the user can define a custom graphics directory on the command line.

Three files: game.py, graphics.py and common.py.

The common.py file contains that class with class variables that jorge was talking about. Other modules import this as needed.

The contents of the files follow below.

So:
$ python game.py
/usr/local/mygame/data/gfx
$ python game.py --gfx-dir moo/cow
/usr/local/mygame/moo/cow
$ python game.py --gfx-dir /moo/cow
/moo/cow

Hope it helps!
/Joel Hedlund

main.py:
--------------------------------------------------------
#!/usr/bin/python

import sys

import graphics
from common import Settings

try:
i = sys.argv.index('--gfx-dir')
except ValueError:
pass
else:
Settings.graphics_dir = sys.argv[i + 1]

print graphics.graphics_dir()
--------------------------------------------------------

common.py:
--------------------------------------------------------
class Settings(object):
game_dir = '/usr/local/mygame'
graphics_dir = 'data/gfx'
--------------------------------------------------------

graphics.py:
--------------------------------------------------------
import os

from common import Settings

def graphics_dir():
return os.path.join(Settings.game_dir, Settings.graphics_dir)
--------------------------------------------------------



Jorge Godoy wrote:
Anton81 <fo***@anton.e4ward.com> writes:

I want to use globals that are immediately visible in all modules. My
attempts to use "global" haven't worked. Suggestions?

Use a module and a class variables for that. Import your module and
read/update class variables as you need them.

Mar 9 '06 #4

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

Similar topics

1
by: David M. Karr | last post by:
I've been asked to help debug a complex problem involving inter-frame references, so I just want to understand the elements involved with this. Apparently, there is a page with multiple frames,...
4
by: Frank Meng | last post by:
Hi. I am trying a csharp sample from http://www.codeproject.com/csharp/socketsincs.asp . (Sorry I didn't post all the source codes here, please get the codes from above link if you want to try)....
4
by: Viper Venom | last post by:
Dear All: I am trying to write an application that consist 2 executables 1) Server.exe 2) Client.exe I start the server.exe first and then start the two client exe by code and kill both of...
7
by: A.M | last post by:
Hi, What is the best way to implemet Inter Process Communication in .NET ? I developed two programs and I want to have them talk to each other. Thanks, Alan
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: les | last post by:
Here's a class which uses 2.0 generics to implement an inter-thread message queue in C#. Any number of threads can post and read from the queue simultaneously, and the message object can be any...
5
by: Joseph Geretz | last post by:
I need to communicate between two applications. The legacy application is in VB6. New development is in C#. Here's the scenario: The VB6 app will be pumping document files into a folder. We'll be...
0
by: Hugo Ferreira | last post by:
Hi everyone! Here's the current scenario: I have a program in Python that computes something very fast (<1s), but it takes a considerable amount of time to read the startup data (>90s). Since...
1
by: Laurence | last post by:
Hi folks, As I konw: database partition (aka data partition?), the database can span multiple machines; table partition, the data within a table can seperate by certain condition. How about...
0
by: dantz | last post by:
After reading all of the materials in msdn about interprocess communication now I am confused. I hope someone can give me some enlightment. I am developing a multithreaded client-server...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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.