I write CLI-executable Python 3 scripts regularly to do data compilation and some maintenance tasks, and try to adhere to PEP 8 and the Google Python Style Guide when doing so. Google's guide says to avoid using global variables, while PEP 8 offers naming conventions but doesn't really encourage or discourage use of them, so I don't use them.
However, I've lately found myself using a "verbose" option/switch a lot along with a "
printv" function to use while debugging. It usually looks like this:
def printv(opts, msg, **kwargs): if opts.verbose: print(msg, **kwargs)
As incredibly lazy as I am, I'd like to make it so I don't need to pass the
opts argument (which is an
OptionParser object) to
printv. The simplest method would be to use a global variable, sure. Another thought I had would be to set a "VERBOSE" variable in
os.environ, since the switch is, conceptually, making a change to the execution environment. Obviously it's counter-intuitive, but is it pythonic?