easy verbose in functions
Duncan Booth
me at privacy.net
Mon Mar 29 05:16:16 EST 2004
pascal.parent at free.fr (Pascal) wrote in
news:e567c03a.0403290150.727c9f63 at posting.google.com:
> def myfunction(data, verbose = False):
> dothis...
> if verbose: print 'I do this'
> dothisother
> if verbose: print 'I do this other'
> ...
>
> How can I do to exclude eachtime the `if verbose` statement?
>
If it offends you that much:
def log_verbose(s):
print s
def log_dummy(s):
pass
def myfunction(data, verbose = False):
if verbose:
log = log_verbose
else:
log = log_dummy
dothis...
log('I do this')
dothisother
log('I do this other')
...
or even pass the log function around as the parameter.
A more general solution might be to use Python's logging facility. That
gives you the opportunity to classify your log messages by different levels
(debug, info, warning, error, critical) and also by the section of program
in a hierarchical arrangement. This gives you the ability to, say, turn on
debug logging for a class, or a module by editing a config file.
More information about the Python-list
mailing list