command-line args

Peter Hansen peter at engcorp.com
Sat Apr 24 08:55:56 EDT 2004


Michael wrote:

> What's the perfered method of passing command-line args throughout a 
> Python program given that globals don't seem to really exist? I thought 
> of writing them out to a python file and then importing them when 
> needed.. but that seems like it'd only work for a single process.. not 
> in instances where more than one user is running the program. There 
> isn't anything like a virtual file space that could be used for this? 
> Anyway to set a global variable?

Create an empty module called "globals" and import that wherever
needed.  Populate it with settings from the command-line parsing
stage.  Get fancier and put defaults in there to begin with, and
override them only if specified in the command-line parsing
area.  Flavour as needed...

(That's just one option, but in many ways the simplest.  Some of
us also use a simple "bag" type of object which we pass around
as required, but that's more awkward with really large applications.)

-Peter



More information about the Python-list mailing list