在Python中禁用断言 [英] Disable assertions in Python

查看:921
本文介绍了在Python中禁用断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中禁用断言?

How do I disable assertions in Python?

也就是说,如果断言失败,我不希望它抛出一个 AssertionError ,但要继续下去。

That is, if an assertion fails, I don't want it to throw an AssertionError, but to keep going.

推荐答案

使用-O标志调用Python:

Call Python with the -O flag:

test.py:

assert(False)
print 'Done'

输出:

C:\temp\py>C:\Python26\python.exe test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    assert(False)
AssertionError

C:\temp\py>C:\Python26\python.exe -O test.py
Done

这篇关于在Python中禁用断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆