如何使用日志启用或禁用 [=10=] 中的许多打印语句?
how to enable or disable many print statements in python with log?
代码看起来像这样 - 有许多用于调试目的的打印件。我想要一些切换 'button' ,一个 enables/disables 那些打印语句的衬垫。不评论 out/in.
#age
if (row['age'] < 25.0):
print()
print('age < 25')
score += -45.0
print('score changed to : %d ' % score)
elif (row['age'] >= 25.0) and (row['age'] < 29.0):
print()
print('25 < age < 29')
score += -22.0
print('score changed to : %d ' % score)
elif (row['age'] >= 29.0) and (row['age'] < 35.0):
print()
print('29 < age < 35')
score += 1.0
print('score changed to : %d ' % score)
elif (row['age'] >= 35.0):
print()
print('age > 35')
score += 19.0
print('score changed to : %d ' % score)
#f27
if (row['f27']== ''):
print()
print('f27 missing found')
score += -18.0
print('score changed to : %d ' % score)
希望下面的代码对您有所帮助。
from __future__ import print_function
debug = True
def print(*args, **kwargs):
if(debug):
return __builtin__.print(*args, **kwargs)
以上代码将覆盖内置打印功能。
如果 debug 设置为 True,它会打印,否则它会跳过打印。
代码看起来像这样 - 有许多用于调试目的的打印件。我想要一些切换 'button' ,一个 enables/disables 那些打印语句的衬垫。不评论 out/in.
#age
if (row['age'] < 25.0):
print()
print('age < 25')
score += -45.0
print('score changed to : %d ' % score)
elif (row['age'] >= 25.0) and (row['age'] < 29.0):
print()
print('25 < age < 29')
score += -22.0
print('score changed to : %d ' % score)
elif (row['age'] >= 29.0) and (row['age'] < 35.0):
print()
print('29 < age < 35')
score += 1.0
print('score changed to : %d ' % score)
elif (row['age'] >= 35.0):
print()
print('age > 35')
score += 19.0
print('score changed to : %d ' % score)
#f27
if (row['f27']== ''):
print()
print('f27 missing found')
score += -18.0
print('score changed to : %d ' % score)
希望下面的代码对您有所帮助。
from __future__ import print_function
debug = True
def print(*args, **kwargs):
if(debug):
return __builtin__.print(*args, **kwargs)
以上代码将覆盖内置打印功能。 如果 debug 设置为 True,它会打印,否则它会跳过打印。