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

Another software design question: program-level globals

OK, here's another software design question, one that's been bugging me
for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?

Here's one approach:

--- main.py ---
import my_globals as g
import somefuncs
import morefuncs
from optparse import OptionParser

def main():
parser = OptionParser()
parser.add_option( ... )
g.opts, g.args = parser.parse_args()

somefuncs.do_setup()
morefuncs.do_some_more_setup()

somefuncs.do_some_work()

if __name__ == '__main__':
main()
--- somefuncs.py ---
import my_globals as g

def do_setup():
if g.opts.some_option:
do_it_one_way()
else:
do_it_a_different_way()
--- morefuncs.py ---
import my_globals as g

def do_some_more_setup():
for arg in g.args:
do_something_with(arg)
--- my_globals.py ---
(empty file)
This approach relies on the fact that modules only get imported once.
Thus when each module imports my_globals, it gets the *same* object
(which is then given the name g to make it easier to write later). So
when the attributes of g are modified in the main() function, they can
then be read by do_setup() and do_some_more_setup(), because the object
that somefuncs.py and morefuncs.py call "g" is the same module object
that main.py also calls "g".

This is the best approach I've found so far, but I would welcome
comments and suggestions for improvements.

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #1
2 1531
On Fri, 13 Feb 2004 18:43:54 GMT, Robin Munn wrote:
OK, here's another software design question, one that's been bugging
me for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?


The first thing to do is: don't use globals. The interface between
disparate parts of your code (functions, classes, modules, packages)
should be as explicit and discoverable as possible.

Often the simplest first step in eliminating globals is to wrap them up
in a class (such as ConfigOptions) as class attributes. Other, more
elegant solutions may require refactoring the code.

--
\ "I like to go to art museums and name the untitled paintings. |
`\ 'Boy With Pail'. 'Kitten On Fire'." -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
"Robin Munn" <rm***@pobox.com> wrote in message
news:KV****************@newssvr26.news.prodigy.com ...
OK, here's another software design question, one that's been bugging me
for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?
You've basically got it: modules are inherently singletons.
I would, of course, dress up your solution by putting the
configuration reader and so forth into a class, and possibly
putting that behind some kind of proxy so I could easily
slide in mock objects for testing.

John Roth
--
Robin Munn
rm***@pobox.com

Jul 18 '05 #3

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

Similar topics

24
by: Uwe Mayer | last post by:
Hi, I have the following inter-class relationships: __main__: (in file LMCMain.py) imports module FileIO defines class LMCMain instanciats main = LMCMain(...) FileIO.py:
0
by: Dana Morris | last post by:
Call for Participation OMG's First Annual Software-Based Communications (SBC) Workshop: From Mobile to Agile Communications http://www.omg.org/news/meetings/SBC2004/call.htm September 13-16,...
6
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that...
8
by: David | last post by:
and now there's Visual C# 2005 Express Edition http://lab.msdn.microsoft.com/express/vcsharp/default.aspx sigh I find it harder to choose my environment then picking up on C# !!!
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
14
by: shuisheng | last post by:
Dear All, I want to start a c++ software project. It is used to simulate the propagation of sound waves in complex media. It may contain 4 parts: GUI, preprocession, simulation and...
62
by: Ajinkya | last post by:
As "spawnl" executes an exe through C code., similarly how can we compile a C program through another C program (on Windows platform) ?
3
MrMancunian
by: MrMancunian | last post by:
Ok, I'm stuck on a design problem and I need some feedback how to go around it. CASE: Pathologists can request stains on certain tissues. It's possible to request more than one stain a time....
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
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
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
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
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.