简体   繁体   English

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

[英]How do you deal with print() once you done with debugging/coding

To python experts: I put lots of print() to check the value of my variables. 对于python专家:我投入了大量print()来检查变量的值。 Once I'm done, I need to delete the print(). 完成后,我需要删除print()。 It quite time-consuming and prompt to human errors. 这非常耗时,并且容易引起人为错误。

Would like to learn how do you guys deal with print(). 想学习你们如何处理print()。 Do you delete it while coding or delete it at the end? 您在编码时将其删除还是最后将其删除? Or there is a method to delete it automatically or you don't use print()to check the variable value? 还是有一种自动删除它的方法,或者您不使用print()检查变量值?

Use logging instead and here is why . 请改用日志记录 ,这就是原因
logging module comes with many simple & handy log methods like debug, info, warning, error, critical 日志记录模块附带许多简单方便的日志记录方法,例如debug, info, warning, error, critical
ex:- logging.debug(<debug stuff>) 例如: logging.debug(<debug stuff>)
EDIT I am not sure I understood correctly about your need to disable logging once you are done, but if you want to do away with console logging you can try 编辑我不确定我是否正确理解您是否需要在完成后禁用日志记录,但是如果您想取消控制台日志记录,可以尝试

#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 编辑
I think you want to remove all your print()/loggging statements from your code once you make sure everything is working fine!!!! 我认为一旦确定一切正常,您想从代码中删除所有print()/ logging语句!
Try using a regx to comment out all print statements 尝试使用regx注释掉所有打印语句

find . -name '<filename>.py' -exec sed -ri "s/(^\s*)(print.*$)/#\1\2/g" {} \;

You can use logging with debug level and once the debugging is completed, change the level to info. 您可以将日志记录与调试级别配合使用,一旦调试完成,请将级别更改为info。 So any statements with logger.debug() will not be printed. 因此,不会显示带有logger.debug()的任何语句。

What I do is put print statements in with with a special text marker in the string. 我要做的是在字符串中添加带有特殊文本标记的打印语句。 I usually use print("XXX", thething) . 我通常使用print("XXX", thething) Then I just search for and delete the line with that string. 然后,我只是搜索并删除带有该字符串的行。 It's also easier to spot in the output. 在输出中也更容易发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当你调试它们并完成时,你如何避免在python开源应用程序中留下调试器print / pdb语句? - How do you avoid leaving debugger print/pdb statements in python open source apps when you're debugging them and just finish? 在代码搜索完列表并且找不到列表中的单词从而答复错误后,如何打印? - How do you print after the code is done searching through the list and it can't find the word in the list so it replies error? 在python中,您如何处理域名中的其他编码 - In python how do you deal with other encodings in domain names 在 Tkinter 中需要很长时间的 function 如何处理? - how do you deal with the function that takes a long time in Tkinter? 您如何打印输入的内容? - How do you print out the input you put in? Django:您如何处理无法扩展的查询? - Django: how do you deal with queries that can not scale? 你如何使用 numpy/scipy 处理丢失的数据? - How do you deal with missing data using numpy/scipy? 您如何处理芹菜引发的异常(不是您的代码)? - How do you deal with an exception raised by celery (not your code)? 在评估 model 时如何处理随机性? - How do you deal with randomness when evaluating a model? 如何在python中打印唯一行 - How do you print unique rows in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM