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

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

问题描述

如何在 Python 中禁用断言?

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

我该怎么做?

解决方案

如何在 Python 中禁用断言?

有多种方法会影响单个进程、环境或一行代码.

我展示了每一个.

对于整个过程

使用 -O 标志(大写 O)禁用进程中的所有断言语句.

例如:

$ python -Oc "assert False"$ python -c断言假"回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中断言错误

请注意,禁用我的意思是它也不会执行其后的表达式:

$ python -Oc "assert 1/0"$ python -c "断言 1/0"回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中ZeroDivisionError:整数除法或模数为零

为了环境

您也可以使用环境变量来设置此标志.

这将影响使用或继承环境的每个进程.

例如,在 Windows 中,设置然后清除环境变量:

C:>python -c "assert False"回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中断言错误C:>SET PYTHONOPTIMIZE=TRUEC:>python -c "assert False"C:>SET PYTHONOPTIMIZE=C:>python -c "assert False"回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中断言错误

在 Unix 中相同(使用 set 和 unset 为各自的功能)

代码中的单点

你继续你的问题:

<块引用>

如果断言失败,我不希望它抛出 AssertionError,而是继续.

如果你想要执行失败的代码,你可以catch或者确保控制流没有到达断言,例如:

如果为假:assert False,我们知道这会失败,但我们没有到达这里"

或者你可以捕捉断言错误:

尝试:assert False, "这段代码运行,失败,异常被捕获"除了 AssertionError 为 e:打印(代表(e))

打印:

AssertionError('这段代码运行失败,异常被捕获')

并且您将从您处理 AssertionError 的那一点开始.

参考文献

来自assert 文档:

像这样的断言语句:

<块引用>

断言表达式#, optional_message

相当于

<块引用>

如果 __debug__:如果不是表达式:引发 AssertionError #(optional_message)

还有,

<块引用>

内置变量__debug__正常情况下为True,请求优化时为False(命令行选项-O).

进一步

<块引用>

__debug__ 的赋值是非法的.内置变量的值在解释器启动时确定.

来自使用文档:

<块引用>

-O

开启基本优化.这会将已编译(字节码)文件的文件扩展名从 .pyc 更改为 .pyo.另请参见 PYTHONOPTIMIZE.

<块引用>

PYTHONOPTIMIZE

如果这被设置为一个非空字符串它是等价的指定 -O 选项.如果设置为整数,则相当于多次指定 -O.

How do I disable assertions in Python?

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

How do I do that?

解决方案

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 either ensure control flow does not reach the assertion, for example:

if False:
    assert False, "we know this fails, but we don't get here"

or you can catch the assertion error:

try:
    assert False, "this code runs, fails, and the exception is caught"
except AssertionError as e:
    print(repr(e))

which prints:

AssertionError('this code runs, fails, and the exception is caught')

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).

and further

Assignments to __debug__ are illegal. The value for the built-in variable is determined when the interpreter starts.

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.

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

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