在Python中切换打印/关闭打印语句的有效方法?

时间:2015-09-09 19:44:35

标签: python python-2.7 debugging

我的程序中有10或15个非常有用的调试 Warning: parse_ini_file( ddddddddddffffffffffffff): failed to open stream: No such file or directory in E:\NEW1\xampp\htdocs\tests\purifier\htmlpurifier-4.7.0-lite\library\HTMLPurifier\Config.php on line 836 Warning: Invalid argument supplied for foreach() in E:\NEW1\xampp\htdocs\tests\purifier\htmlpurifier-4.7.0-lite\library\HTMLPurifier\Config.php on line 684 Catchable fatal error: Object of class mysqli could not be converted to string in E:\NEW1\xampp\htdocs\tests\purifier\htmlpurifier-4.7.0-lite\library\HTMLPurifier\Lexer.php on line 298 语句(在不同的函数和print中)。

我不会总是想要或需要日志文件。我有一个配置文件,我可以在其中添加一个参数来打开或关闭打开打印语句。但是,我必须在每个print语句上面添加一个对该参数值的保护检查。

有哪些更好的方法?

1 个答案:

答案 0 :(得分:6)

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

遗憾的是,您无法保留py2打印语法print 'foo'