python assert 在正式产品里禁用的手法 直接-O即可
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__isTrueunder normal circumstances,Falsewhen optimization is requested (command line option-O).
From the usage docs:
Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.
and
If this is set to a non-empty string it is equivalent to specifying the
-Ooption. If set to an integer, it is equivalent to specifying-Omultiple times.
python assert 在正式产品里禁用的手法 直接-O即可的更多相关文章
- python assert的作用
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...
- python assert的用处
python assert 句语格式及用法很简单.通常程序在运行完之后抛出异常,使用assert可以在出现有异常的代码处直接终止运行. 而不用等到程序执行完毕之后抛出异常. python assert ...
- Python——assert(断言函数)
一.断言函数的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假.可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会 ...
- Python assert 断言函数
http://blog.csdn.net/hunyxv/article/details/52737339 使用assert断言是学习python一个非常好的习惯,python assert 断言句语格 ...
- Python assert(断言)
Python assert(断言)可以分别后面的判断是否正确,如果错误会报错 示例: a = 1 assert type(a) is int print('No problem') 输出结果: No ...
- Python assert作用
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前, 我们不知道程序在哪里会出错.与其让它在运行最后崩溃,不如在出现错误 ...
- python assert断言函数
python assert断言是声明布尔值必须为真的判定,如果发生异常就说明表达式为假. 可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常. self ...
- python assert用法
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...
- python assert使用说明
python assert断言的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假. assert断言语句的语法格式 判断a与1.b是否一致,msg类似备注 ...
随机推荐
- dva+umi+antd项目从搭建到使用
先创建一个新项目,具体步骤请参考https://www.cnblogs.com/darkbluelove/p/11338309.html 一.添加document.ejs文件(参考文档:https:/ ...
- Linux内核参数详解
所谓Linux服务器内核参数优化(适合Apache.Nginx.Squid等多种web应用,特殊的业务有可能需要做略微调整),主要是指在Linux系统中针对业务服务应用而进行的系统内核参数调整,优化并 ...
- 【LOJ3099】[SNOI2019]积木(搜索)
lca 学长出的我省省选的神仙题目 省强我菜系列 题目 LOJ3399 分析 我可能说不清楚,对着代码理解吧 -- 感觉这题的主要难点是:不要想他具体是怎么操作的,只要知道他一定存在一种操作方式能够实 ...
- 处理登录时,AJAX的状态码无权限情况
$.ajaxSetup({ complete: function(XMLHttpRequest, textStatus) { }, error:function(jqXHR,textStatus,er ...
- jQuery中$(this)与this的区别
经常在写jQuery的时候分不清this 和 $(this),为了方便起见尽量不用this,只用$(this).但是今天在别人的代码的基础上改一些东西,又遇到了这个this,不得不把它弄明白. $(t ...
- [转帖]Java升级那么快,多个版本如何灵活切换和管理?
Java升级那么快,多个版本如何灵活切换和管理? https://segmentfault.com/a/1190000021037771 前言 近两年,Java 版本升级频繁,感觉刚刚掌握 Java8 ...
- a++与++a,谈谈C++的参数传递
先看一段代码: #include<iostream> using namespace std; void func(int a, int b) { cout << a < ...
- LOJ2461 完美的队列 分块
传送门 如果对于每一个操作\(i\)找到这个操作中所有的数都被pop掉的时间\(ed_i\),那么剩下就直接差分覆盖一下就可以了. 那么考虑如何求出\(ed_i\).发现似乎并没有什么数据结构能够维护 ...
- MOOC C++笔记(六):多态
多态 虚函数 在类的定义中,前面有virtual关键字的成员函数就是虚函数. virtual关键字只用在类定义里的函数声明中,写函数体时不用. 构造函数和静态成员函数不能是虚函数. 多态的表现形式 基 ...
- 深度剖析java中JDK动态代理机制
https://www.jb51.net/article/110342.htm 本篇文章主要介绍了深度剖析java中JDK动态代理机制 ,动态代理避免了开发人员编写各个繁锁的静态代理类,只需简单地指定 ...