What is the best way to toggle python prints?

前端 未结 6 1121
一个人的身影
一个人的身影 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 06:57

    I've answered this question on a different post but was the question was marked duplicate:

    anyhow, here's what I would do:

    from __future__ import print_function
    DebugPrints = 0
    
    def print(*args, **kwargs):
        if DebugPrints:
            return __builtins__.print(*args, **kwargs)
    
    print('foo') # doesn't get printed
    DebugPrints = 1
    print('bar') # gets printed
    

    sadly you can't keep the py2 print syntax print 'foo'

提交回复
热议问题