Traceback (most recent call last): File "err.py", line 13, in <module> main() File "err.py", line 11, in main bar('0') File "err.py", line 8, in bar return foo(s)*2 File "err.py", line 5, in foo return10/int(s) ZeroDivisionError: division by zero
defmain(): try: bar('0') except Exception as e: logging.exception(e)
main() print('END')
同样是出错,但程序打印完错误信息会继续执行并正常退出:
1 2 3 4 5 6 7 8 9 10
ERROR:root:division by zero Traceback (most recent call last): File "err_logging.py", line 14, in main bar('0') File "err_logging.py", line 10, in bar return foo(s)*2 File "err_logging.py", line 7, in foo return10/int(s) ZeroDivisionError: division by zero END
>>>n=0 Traceback (most recent call last): File "err.py", line 12, in <module> main() File "err.py", line 10, in main foo('0') File "err.py", line 7, in foo return10/int(s) ZeroDivisionError: division by zero
用print()最大的坏处是以后还要删掉他,运行结果中会包含很多垃圾信息。
断言
凡是用print()来辅助查看的地方,都可以用断言(assert)来替代:
1 2 3 4 5 6 7 8 9
deffoo(s): n=int(s) assert n!=0, 'n is zero' return10/int(s)
D:笔记PythonNotepad++>python err.py Traceback (most recent call last): File "err.py", line 12, in <module> main() File "err.py", line 10, in main foo('0') File "err.py", line 6, in foo assert n!=0, 'n is zero' AssertionError: n is zero
D:笔记PythonNotepad++>python mydict_test.py EE... ====================================================================== ERROR: test_attr (__main__.TestDict) ---------------------------------------------------------------------- Traceback (most recent call last): File "mydict_test.py", line 25, in test_attr self.assertEqual(d[key],'value') NameError: name 'key'isnot defined
====================================================================== ERROR: test_attrerror (__main__.TestDict) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:笔记PythonNotepad++mydict.py", line 11, in __getattr__ return self[key] KeyError: 'empty'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "mydict_test.py", line 35, in test_attrerror value=d.empty File "D:笔记PythonNotepad++mydict.py", line 13, in __getattr__ raise ArithmeticError(r"'Dict' object has no attrribute '%s'" %key) ArithmeticError: 'Dict' object has no attrribute 'empty'
---------------------------------------------------------------------- Ran 5 tests in0.004s
>>> import re >>> m=re.search('(?<=abc)def','abcdef') >>> m.group(0) 'def
可以把这些示例代码在Python的交互环境下输入并执行,结果与文档中的实例代码显示一致。
这些代码与其他说明可以写在注释中,然后由一些工具来自动生成文档。也可以自动执行写在注释中的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
>>> defabs(n): ... ''' ... Function to get absolute value of number. ... Exampel: ... >>>abs(1) ... 1 ... >>>abs(-1) ... 1 ... >>>abs(0) ... 0 ... ''' ... return n if n>=0else (-n) ...
D:笔记PythonNotepad++>python mydict2.py ********************************************************************** File "mydict2.py", line 23, in __main__.Dict Failed example: d2.empty Expected: Traceback (most recent call last): ... AttributeError: 'Dict' object has no attribute 'empty' Got: Traceback (most recent call last): File "mydict2.py", line 33, in __getattr__ return self[key] KeyError: 'empty' <BLANKLINE> During handling of the above exception, another exception occurred: <BLANKLINE> Traceback (most recent call last): File "C:Userscdxu0AppDataLocalProgramsPythonPython35libdoctest.py", line 1320, in __run compileflags, 1), test.globs) File "<doctest __main__.Dict[8]>", line 1, in <module> d2.empty File "mydict2.py", line 35, in __getattr__ raise ArithmeticError(r"'Dict' object has no attrribute '%s'" %key) ArithmeticError: 'Dict' object has no attrribute 'empty' ********************************************************************** 1 items had failures: 1 of 9in __main__.Dict ***Test Failed*** 1 failures.