How do I disable assertions in Python?

There are multiple approaches that affect a single process, the environment, or a single line of code.

I demonstrate each.

For the whole process

Using the -O flag (capital O) disables all assert statements in a process.

For example:

$ python -Oc "assert False"

$ python -c "assert False"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AssertionError

Note that by disable I mean it also does not execute the expression that follows it:

$ python -Oc "assert 1/0"

$ python -c "assert 1/0"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

For the environment

You can use an environment variable to set this flag as well.

This will affect every process that uses or inherits the environment.

E.g., in Windows, setting and then clearing the environment variable:

C:\>python -c "assert False"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AssertionError
C:\>SET PYTHONOPTIMIZE=TRUE C:\>python -c "assert False" C:\>SET PYTHONOPTIMIZE= C:\>python -c "assert False"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AssertionError

Same in Unix (using set and unset for respective functionality)

Single point in code

You continue your question:

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

If you want the code that fails to be exercised, you can catch an assertion error:

try:
assert False, "we know this fails"
except AssertionError as e:
print(repr(e))

which prints:

AssertionError('we know this fails',)

and you'll keep going from the point you handled the AssertionError.

References

From the assert documentation:

An assert statement like this:

assert expression #, optional_message

Is equivalent to

if __debug__:
if not expression: raise AssertionError #(optional_message)

And,

the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O).

From the usage docs:

-O

Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.

and

PYTHONOPTIMIZE

If this is set to a non-empty string it is equivalent to specifying the -O option. If set to an integer, it is equivalent to specifying -O multiple times.

最新文章

  1. 在 .NET 中开发基于 Chrome 内核的浏览器-创建一个简单浏览器
  2. DevExpress VCL 13.1.4支持Delphi /C++Builder XE5
  3. position之absolute与relative 详解
  4. mysql查询结果导出到文件
  5. 项目中关于ajax jsonp的使用
  6. 深入学习golang(1)—数组与切片
  7. vs 2010 Cannot find or open the PDB file
  8. 第七章----pwm蜂鸣器
  9. BZOJ1264: [AHOI2006]基因匹配Match
  10. Android Permissions管理之用户拒绝授权
  11. MFC自绘控件学习总结第二贴---转
  12. group by和order by的错误
  13. call 与 apply的区别
  14. Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
  15. Swing-BoxLayout用法-入门
  16. 教你如何把openfire的muc聊天室改造为群
  17. 7.6 chcount.c -- 使用逻辑与运算符
  18. db2编目抽取
  19. 【Linux】-NO.8.Linux.4.Command.1.001-【Common Command】-
  20. 了解数据模型、以及MySQL使用的数据模型

热门文章

  1. IIS配置实现反向代理(图文)
  2. git中配置的.gitignore不生效的解决办法
  3. STL之iterator源码解析
  4. Idea 目录结构下有红色波浪线
  5. JPA中JpaRepository的使用
  6. day14——装饰器
  7. 法那科 三菱 CNC虚拟机
  8. 配置linux命令行界面的 文件显示颜色
  9. 简单端口映射、转发、重定向工具-Rinetd
  10. 【题解】Luogu P5304 [GXOI/GZOI2019]旅行者