【发布时间】:2016-10-13 14:21:31
【问题描述】:
在接下来的情况下需要帮助。我想通过在具有命令执行名称和经过时间的函数中打印小的完成报告来在我的脚本中实现调试模式,例如:
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)
...
如何将 isDebug 变量简单地传递给函数? 我应该使用“全局 isDebug”吗?
因为
...
cmd_exec(cmd1, isDebug)
...
cmd_exec(cmd2, isDebug)
...
看起来很糟糕。请帮我找到更优雅的方式。
【问题讨论】:
标签: python function arguments global-variables