用print语句打印
我们可以用print语句打印我们想要的内容,然后在 输出中查看。
print "hah"
但是在调试之后,我们还需要手动删除print语句,比较麻烦。
assert
前面用print的地方,我们可以使用assert语句来替代。例如:
def foo(s): s = int(s) assert s != 0, "s is Zero" return 10.0 / s foo('0')
assert语句后紧跟着一句判断语句,再更着错误信息。如果判断语句不符合,则抛出一个AssertionError.例如:
Traceback (most recent call last): File "/Users/W/Code/Python/Demo/AssertDemo.py", line 7, in foo('0') File "/Users/W/Code/Python/Demo/AssertDemo.py", line 3, in foo assert s != 0, "s is Zero" AssertionError: s is Zero
我们可以在执行的时候带上参数-o统一关闭assert。关闭后,assert语句就不再生效。
logging
可以将print语句替换成logging。logging不会像assert那样抛出错误信息。logging的好处有很多,一个是可以制定 输出特定级别的信息。
Level: CRITICAL Numeric value: 50 Level: ERROR Numeric value: 40 Level: WARNING Numeric value: 30 Level: INFO Numeric value: 20 Level: DEBUG Numeric value: 10 Level: NOTSET Numeric value: 0
当然,很多现代化的IDE比如Pycharm等都提供了很多方便的可视化的debug工具,可以很方便的上手。
以上就是python学习笔记-python的调试的内容,更多相关内容请关注PHP中文网(HdhCmsTestgxlcms测试数据)!
查看更多关于python学习笔记-python的调试的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did86244