I\'m running Python 2.4 in a game engine and I want to be able to turn off all prints if needed. For example I\'d like to have the prints on for a debug build, and then turn
I know an answer has already been marked as correct, but Python has a debug flag that provides a cleaner solution. You use it like this:
if __debug__:
print "whoa"
If you invoke Python with -O or -OO (as you normally would for a release build), __debug__
is set to False
. What's even better is that __debug__
is a special case for the interpreter; it will actually strip out that code when it writes the pyc/pyo
files, making the resulting code smaller/faster. Note that you can't assign values to __debug__
, so it's entirely based off those command-line arguments.