Verbosity Check Style

Christopher T King squirrel at WPI.EDU
Thu Aug 12 17:48:02 CEST 2004


On Thu, 12 Aug 2004 brianc at temple.edu wrote:

> I would like to get people's views on the best way to
> implement verbosity (or any command-line option) into python
> scripts. 

I'd suggest one of the following:

1) Use a dedicated verbose() function.  This will likely be the fastest 
and cleanest method:

 if VERBOSE:
     def verbose(t):
         print t
 else:
     def verbose(t):
         pass

 <some code>
 verbose("I'm doing something here")
 <more code>

2) Use a dedicated verbose file handle.  This is as fast as and possibly
(depending on your taste) cleaner than the above method:

 import sys
 # Pythonic version of /dev/null
 class devnull(object):
     def write(self,d): pass

 if VERBOSE:
     verbose = sys.stdout
 else:
     verbose = devnull()

 <some code>
 print >>verbose, "I'm doing something here"
 <more code>

Hope this helps.




More information about the Python-list mailing list