[英]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.