On Oct 20, 12:19 pm, Derek Martin <[EMAIL PROTECTED]> wrote: snip > > I'm specifically trying to avoid having to create a debug object and > pass it around... All modules should have visibility into the state of > whether DEBUG is turned on or off, and be able to use dprint(). Can > Python do this? > > I tried creating debug.py as such: > > ---- debug.py ---- > DEBUG = True > def dprint(msg): > if DEBUG: > print("DEBUG: %s" % msg) > ---- end ---- > > Then in the modules that wanted to use it, I did: > > from debug import DEBUG, dprint > > But I got some weird behavior. The imported copy of DEBUG is > read-only; if you update it, the name DEBUG points to a different > object which the other modules can't see. After doing some reading of > the docs, this behavior is explained and understood (though obviously > not what I want). It just occured to me that I might be able to get > around that by using a setter function in the module itself... I'll > try this later. > > The other weird behavior was, once I changed the value of DEBUG, > dprint() started to behave oddly. No matter what I passed as an > argument (and no matter what I set the value of DEBUG to be), it > started printing the exact literal string: > > DEBUG: %s > > whenever it was called. It was as if the function couldn't see the > parameter msg, which was passed via the call. Most unexpected, and > definitely undesirable. >
It should work if you set the attribute of the module directly. (untested) import debug debug.DEBUG= True debug.DEBUG= False Your idea of a module getter/setter should work too. def set_debug( val ): global DEBUG DEBUG= val Can't help with the other problem. Simplify a little with just print( msg ). -- http://mail.python.org/mailman/listinfo/python-list