What is the best way to toggle python prints?

前端 未结 6 1108
一个人的身影
一个人的身影 2021-01-12 06:28

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

6条回答
  •  温柔的废话
    2021-01-12 07:15

    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.

提交回复
热议问题