<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
How about writing a function in a module that everyone can import called
like dprint or something like that that you can pass the string to print
to and it will decide whether or not to actually print it based on whether
or not --debug was in sys.argv, which could even be set in a variable there
by __main__ or something like that.
<p>nanotech@europa.com wrote:
<blockquote TYPE=CITE>All:
<p>When writing programs, I like to add print statements that are only
<br>displayed when the user provides a command line switch such as '--
<br>debug'.
<p> Ex: %myprog spam.txt ham
<br> Found the word 'ham' in 'spam.txt'
45 times
<p> %myprog --debug spam.txt ham
<br> -I-: Reading 'spam.txt'....has
213 lines....
<br> Found the word 'ham' in 'spam.txt'
45 times
<p>Now, say most of the work is done in a module 'spam.py'. If I wrote
<br>code in this module like:
<p> lines=open(filename).readlines()
<br> if (DEBUG):
<br> print "-I-: Reading '%s'....has %s lines...."%(filename,len
<br>(lines))
<p>DEBUG would be searched for at the module scope. But I want it to
<br>look for DEBUG back in __main__, like:
<p> if (sys.modules["__main__"].DEBUG):
<br> print "-I-: Reading '%s'....has %s lines...."%len(filename,lines)
<p>Is this the "best" way to access variable in the "__main__" scope?
<br>How else might I solve this (in the python way)??
<p>Quentin Crain
<p>Note: I might load many modules all wanting to know if DEBUG is
<br>turned on. I do not want to pass DEBUG to functions or classes and
I
<br>do not want to set a module variable DEBUG to the same value
<br>(module1.DEBUG=DEBUG, module2.DEBUG=DEBUG, etc. in __main__). Unless
<br>these are "better" solutions! :)</blockquote>
<pre>--
Eric Renouf
Software Engineer
Opticom Inc.
www.getiview.com</pre>
</html>