需要下一種情況的幫助。我想通過在打印小完工報告中的功能與命令執行的名稱和ellapsed時間等,以實現調試模式在我的腳本:Python:將變量傳遞給多個函數調用的好方法
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)
...
看起來很糟糕。請幫我找到更優雅的方式。
感謝您提供解決方案。問題是我經常切換模式。只需在腳本的開頭添加'-d'選項,swich調試模式更方便 –
@VimimK我不確定什麼是異議。我在這裏呈現的代碼位直接從您的問題中複製。 –
是的,我的意思是,打開/關閉調試模式,我將需要每次編輯_my_debug.py_文件 –