title: How to pass global debug flag variable throughout my code; should I use argparse?
body: <p>Let's say I have a main program (test.py) and a little utilities program (test_utils.py) that has helper functions called by the main program.
  I would like to turn on debug statements in the code by passing a <code>debug_flag</code> boolean, which is read in via <code>argparse</code>.</p>
  
  <p>Now I would like functions within my <code>test_utils.py</code> program to print debug statements as well, according to the value of <code>debug_flag</code>. I could always add <code>debug_flag</code> as a parameter to each function definition in <code>test_utils.py</code> and pass the parameter when the function is called, but is there a better way here, like make  <code>debug_flag</code> a global variable? But if I do declare <code>debug_flag</code> to be global from  <code>test.py</code>, how would that be imported into  <code>test_utils.py</code>?</p>
  
  <p>What would be the most elegant/Pythonic approach here?</p>
  
  <p>test.py:</p>
  
  <pre><code>import argparse
  from test_utils import summation
  
  def main():
      args = get_args()
      debug_flag = True if args[debug] == 'True' else False
      print summation(5, 6, 7)
  
  def get_args():
      parser = argparse.ArgumentParser(description='Test program')
      parser.add_argument('-d','--debug', help='Debug True/False', default=False)
      args = vars(parser.parse_args())
      return args
  </code></pre>
  
  <p>test_utils.py:</p>
  
  <pre><code>from test import debug_flag
  
  def summation(x, y, z):
      if debug_flag:
          print 'I am going to add %s %s and %s' % (x, y, z)
      return x + y + z
  </code></pre>
  
  <p>EDIT1: To clarify - if I pass in the debug flag through argparse and thereby do set debug_flag to True - how would this be propagated to functions within <code>test_utils.py</code>?</p>
  
  <p>EDIT2: Based on the suggestion by @joran-beasley, here's what I've got.</p>
  
  <p>test.py:</p>
  
  <pre><code>import argparse
  import logging
  from test_utils import summation
  
  def main():
      args = get_args()
      logging.getLogger("my_logger").setLevel(logging.DEBUG if args['debug'] == 'True' else logging.WARNING)
      print summation(5, 6, 7)
  
  def get_args():
      parser = argparse.ArgumentParser(description='Test program')
      parser.add_argument('-d','--debug', help='Debug True/False', required=True)
      args = vars(parser.parse_args())
      return args
  
  main()
  </code></pre>
  
  <p>test_utils.py</p>
  
  <pre><code>import logging
  
  log = logging.getLogger('my_logger')
  
  def summation(x, y, z):
      log.debug('I am going to add %s %s and %s' % (x, y, z))
      return x + y + z
  </code></pre>
  
  <p>When I run test.py, I get:</p>
  
  <pre><code>$ python test.py -d True
  No handlers could be found for logger "my_logger"
  18
  </code></pre>