简体   繁体   中英

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. Once I'm done, I need to delete the print(). It quite time-consuming and prompt to human errors.

Would like to learn how do you guys deal with 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?

Use logging instead and here is why .
logging module comes with many simple & handy log methods like debug, info, warning, error, critical
ex:- 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!!!!
Try using a regx to comment out all print statements

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. So any statements with logger.debug() will not be printed.

What I do is put print statements in with with a special text marker in the string. I usually use print("XXX", thething) . Then I just search for and delete the line with that string. It's also easier to spot in the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM