Python: good way to pass variable to multiple function calls
<p>Need a help with the next situation. I want to implement debug mode in my script through printing small completion report in functions with command executed name and ellapsed time like:</p>

<pre><code>def cmd_exec(cmd):
    if isDebug:
        commandStart = datetime.datetime.now()
        print commandStart
        print cmd
    ...
    ... exucuting commands
    ...
    if isDebug:
        print datetime.datetime.now() - command_start
    return

def main():
    ...
    if args.debug:
        isDebug = True
    ...
    cmd_exec(cmd1)
    ...
    cmd_exec(cmd2)
    ...
</code></pre>

<p>How can isDebug variable be simply passed to functions?
Should I use "global isDebug"? </p>

<p>Because </p>

<pre><code>    ...
    cmd_exec(cmd1, isDebug)
    ...
    cmd_exec(cmd2, isDebug)
    ...
</code></pre>

<p>looks pretty bad. Please help me find more elegant way.</p>

<p>Python has a built-in <code>__debug__</code> variable that could be useful.</p>

<pre><code>if __debug__:
    print 'information...'
</code></pre>

<p>When you run your program as <code>python test.py</code>, <code>__debug__</code> is <code>True</code>. If you run it as <code>python -O test.py</code>, it will be <code>False</code>.</p>

<p>Another option which I do in my projects is set a global <code>DEBUG</code> var at the beginning of the file, after importing:</p>

<pre><code>DEBUG = True
</code></pre>

<p>You can then reference this <code>DEBUG</code> var in the scope of the function.</p>

python function arguments global-variables