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><code>isDebug</code> is state that applies to the application of a function <code>cmd_exec</code>. Sounds like a use-case for a class to me.</p>

<pre><code>class CommandExecutor(object):

    def __init__(self, debug):
        self.debug = debug

    def execute(self, cmd):
        if self.debug:
            commandStart = datetime.datetime.now()
            print commandStart
            print cmd
        ...
        ... executing commands
        ...
        if self.debug:
            print datetime.datetime.now() - command_start

def main(args):
    ce = CommandExecutor(args.debug)
    ce.execute(cmd1)
    ce.execute(cmd2)
</code></pre>

python function arguments global-variables