python关闭程序语句_在Python中打开/关闭打印语句的高效方法?

I have 10 or 15 very useful debugging print statements sprinkled throughout my program (in different functions and in main).

I won't always want or need the log file though. I have a config file in which I could add a parameter to toggle print statements on or off. But then, I'd have to add a guard check for the value of this parameter above every print statement.

What are some better approaches?

解决方案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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值