python - 为什么不显示 AssertionError?

标签 python

作为实验,我 try catch 失败的断言。

try: assert 1==2
except Exception as e: print e

为什么没有显示?

最佳答案

>>> try: assert 1==2
... except Exception as e: print type(e)
...
<type 'exceptions.AssertionError'>

>>> try: assert 1==2, "They Are Not Equal!!"
... except Exception as e: print e
...
They Are Not Equal!!

至于为什么:当你调用 print 时它正在调用异常的 __str__ 方法......因为你没有在那里放任何文本,你的文本是空字符串...这是打印的内容。

关于python - 为什么不显示 AssertionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17372957/

相关文章:

python - 仍在为类范围而苦苦挣扎

python - 为(非系统)Python 安装 rpm 模块

python - 如何在 python 中访问 Facebook 墙提要?

python - 我应该使用 'not x' 还是 'x == 0' 来检查 python 中模运算的结果是否为零

python - 单行(或两行)中的 3 级嵌套字典中的挤压和求和值

Python:向极坐标图添加环形扇区或楔形

python - 如何获得图像凸出效果?

python - 正则表达式:在复杂的正则表达式中平衡 "{}"(python)

python - 如何防止 LIKE '%input%' 子句中的 SQL 注入(inject)?

python - 来自字典/汇总 DataFrame 的 Pandas 系列