Skip to content Skip to sidebar Skip to footer

Efficient Way Of Toggling On/off Print Statements In Python?

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

Solution 1:

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'


Post a Comment for "Efficient Way Of Toggling On/off Print Statements In Python?"