I have 10 or 15 very useful debugging print statements sprinkled throughout my program (in different functions and in main).
I won't always want or need the log file though. I have a config file in which I could add a parameter to toggle print statements on or off. But then, I'd have to add a guard check for the value of this parameter above every print statement.
What are some better approaches?
解决方案from __future__ import print_function
enable_print = 0
def print(*args, **kwargs):
if enable_print:
return __builtins__.print(*args, **kwargs)
print('foo') # doesn't get printed
enable_print = 1
print('bar') # gets printed
sadly you can't keep the py2 print syntax print 'foo'