繁体   English   中英

你如何断言某些东西在Python中是不正确的?

[英]How do you assert something is not true in Python?

在试图理解Python中的assert ,特别是反转它时,我想出了这个......

>>> assert != ( 5 > 2 )
>>> assert != ( 2 > 5 )

现在第一行失败,第二行失败。 什么是断言某事的惯用方法是错误的?

你使用boolean not运算符 ,而不是!=不等式比较运算符:

>>> assert not (5 > 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert not (2 > 5)

一个assert通过测试是否在布尔意义上真正的,所以你需要使用布尔not操作反转测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM