sort of a beginner question about globals

Steven Bethard steven.bethard at gmail.com
Wed Apr 13 11:05:35 EDT 2005


fred.dixon wrote:
> I want to use OPTIONS as a global var.
> In this particular case I am trying to set a global debug constant so I
> can have some debug logging happen when my program is run with a
> -debug option.
> what will actuall end up in OPTIONS is OPTIONS.debug = True as i am
> using optparse module.

Why not just make options a module?

Then you can do something like:

----- options.py -----
debug = False
----------------------

----- file1.py -----
import file2
import options

options.debug = True
a = file2.class1()
a.func1()
--------------------

----- file2.py -----
import options
class class1(object):
     def func1(self):
         if options.debug:
             print 'Debugging'
         pass
--------------------

Note that if you're trying to output debugging messges, you should 
really look into the logging module instead.

STeVe



More information about the Python-list mailing list