在尝试理解 Python 中的 assert
时,特别是反转它,我想到了这个......
>>> assert != ( 5 > 2 )
>>> assert != ( 2 > 5 )
现在第一行失败,第二行通过。断言某事为假的惯用方式是什么?
最佳答案
你会使用 boolean not
operator ,而不是 !=
不等式比较运算符:
>>> assert not (5 > 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert not (2 > 5)
如果测试在 bool 意义上为真,则 assert
通过,因此您需要使用 bool not
运算符来反转测试。
关于python - 你如何在 Python 中断言某事不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439370/