gpt4 book ai didi

python3 - 使用断言的不好做法?

转载 作者:行者123 更新时间:2023-11-30 22:06:19 26 4
gpt4 key购买 nike

我有一个 python3 脚本,它使用简单的断言语句来比较两个变量的值。

assert response['checksum'] == checksum

名为 bandit 的代码分析工具将其标记为不良做法,并且受到的保护较少。 https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html

使用断言真的是不好的做法并且不太安全吗?

最佳答案

您自己在链接中给出了答案:

It was discovered that some projects used assert to enforce interface constraints. However, assert is removed with compiling to optimised byte code (python -o producing *.pyo files).

因此,如果一个库依赖于 assert 来强制执行某种验证,并且我将其与 py2exe 或一些类似的工具一起使用,那么生成的代码很有可能将丢失断言并且失败或跳过所述验证。这就是为什么您应该只在测试代码等中使用assert

关于python3 - 使用断言的不好做法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52765784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com