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:
commandStart = datetime.datetime.now()
... exucuting commands
print datetime.datetime.now() - command_start
isDebug = True
You can use a module to create variables that are shared. This is better than a global because it only affects code that is specifically looking for the variable, it doesn't pollute the global namespace. It also lets you define something without your main module needing to know about it.
This works because modules are shared objects in Python. Every
import gets back a reference to the same object, and modifications to the contents of that module get shared immediately, just like a global would.
isDebug = false
import my_debug def cmd_exec(cmd): if my_debug.isDebug: # ... def main(): # ... if args.debug: my_debug.isDebug = True