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 turned off for a release build. It’s also imperative that it’s as transparent as possible.
My solution to this in the C code of the engine is having the printf function inside a vararg macro, and defining that to do nothing in a release build.
This is my current solution:
DebugPrints = True def PRINT (*args): global DebugPrints if DebugPrints: string = '' for arg in args: string += ' ' + str(arg) print string
It makes it easy to toggle print outs, but there is possibly a better way to format the string. My main issue is that this is actually adding a lot more function calls to the program.
I’m wondering if there is anything you can do to how the print keyword works?
yes, you can assign
sys.stdoutto whatever you want. Create a little class with awritemethod that does nothing:With the same technique you can also have your prints logged to a file by setting
sys.stdoutto an opened file object.