完成调试/编码后,如何处理 print()

2024-03-24

致Python专家: 我使用了很多 print() 来检查变量的值。 完成后,我需要删除 print()。它非常耗时并且容易出现人为错误。

想了解一下你们如何处理 print()。是编码时删除还是最后删除?或者有一种方法可以自动删除它或者您不使用 print() 来检查变量值?


Use logging https://docs.python.org/3/library/logging.html相反,这里是why http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/.
日志记录模块附带了许多简单方便的日志方法,例如debug, info, warning, error, critical
ex:- logging.debug(<debug stuff>)
EDIT我不确定我是否正确理解您需要在完成后禁用日志记录,但如果您想取消控制台日志记录,您可以尝试

#logger = logging.getLogger() # this gets the root logger
logger = logging.getLogger('my-logger')
logger.propagate = False
# now if you use logger it will not log to console.

EDIT
我认为一旦确保一切正常,您就想从代码中删除所有 print()/logging 语句!!!!
尝试使用正则表达式注释掉所有打印语句

find . -name '<filename>.py' -exec sed -ri "s/(^\s*)(print.*$)/#\1\2/g" {} \;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

完成调试/编码后,如何处理 print() 的相关文章

随机推荐