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类似备注 ...
随机推荐
- Centos各版本系统ISO镜像下载地址
https://www.centos.org/download/mirrors/ 需要在里面一个个看,有些是没有旧版本镜像的 补充: 上面这个方法很难再找到旧版本了 更好的方法如下:以下载Centos ...
- 使用ReadtheDocs托管技术文档
ReadtheDocs Read the Docs非常适合写软件文档以及编写一些教程.电子书之类.对于一些一两篇文章就能写清楚的可以记笔记或写博客, 但是如果要写成一个系列的,不如写成一本书的形式,更 ...
- Word章 节标题自动编号并由此自动生成目录 -- 含视频教程(1)
1. 写论文之前事先调好 写论文的时候,难免要用到章节,如何实现下图的效果呢? 其实结合用好两个功能就行了: >> 视频教程链接:B站,速度快,清晰 事先调好,可以给后续工作带来很多便利, ...
- jdk的下载路径和环境变量的配置
一:jdk百度网盘的下载路径: 链接:https://pan.baidu.com/s/1pF41oGcTqouULsWKEBn3hw 提取码:p1y2 复制这段内容后打开百度网盘手机App,操作更方便 ...
- LeetCode 5198. 丑数 III(Java)容斥原理和二分查找
题目链接:5198. 丑数 III 请你帮忙设计一个程序,用来找出第 n 个丑数. 丑数是可以被 a 或 b 或 c 整除的 正整数. 示例 1: 输入:n = 3, a = 2, b = 3, c ...
- go编译运行说明
二 编译运行说明 1.1 编译 1)有了go源文件,通过编译器将其编译成机器可以识别的二进制码文件. 2)在该源文件目录下,通过 go build 对hello.go 文件进行编译.可以指定生成的可 ...
- Linux环境下进程的CPU占用率
阿里云服务器网站:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=qqwovx6h 文字来源:http://www.s ...
- golang 之 context包
概述 context是Go中广泛使用的程序包,由Google官方开发,在1.7版本引入.它用来简化在多个go routine传递上下文数据.(手动/超时)中止routine树等操作,比如,官方 ...
- 《三》大话 Typescript 接口
> 前言: 本文章为 TypeScript 系列文章. 旨在利用碎片时间快速入门 Typescript. 或重新温故 Typescript 查漏补缺.在官方 api 的基础上, 加上一些日常使用 ...
- maven安装本地jar到本地仓库
注册到本地仓库 mvn install:install-file -DgroupId=cn.endv -DartifactId=endv-api -Dversion=1.0.1 -Dpackaging ...