My suggestion is to use a function. But instead of putting if in a function that you could tempt, do this:
if verbose: def verboseprint(*args):
(Yes, you can define a function in an if , and it will be defined only if the condition is true!)
If you are using Python 3 where print already a function (or if you want to use print as a function in 2.x using from __future__ import print_function ), this is even simpler:
verboseprint = print if verbose else lambda *a, **k: None
Thus, a function is defined as do-nothing if verbose mode is turned off (using lambda) instead of constantly testing the verbose flag.
If the user could change the verbosity mode while starting your program, this would be the wrong approach (you will need if in the function), but since you set it using the command line flag, you only need to make a decision once.
Then you use, for example. verboseprint("look at all my verbosity!", object(), 3) whenever you want to print a "verbose" message.
kindall May 12 '11 at 15:09 2011-05-12 15:09
source share