python中assert断言的用法

本文转载自  python中assert断言的用法

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

assert断言是一句必须等价于布尔真的判定!

1 不等于 0 就会有AssertionError异常

1 等于 0 就没有异常

如果断言成功(如果为真)那么不执行任何操作!

如果断言不成功,那么会触发AssertionError

我们还可使用异常参数:

扫描二维码关注公众号,回复: 4256669 查看本文章

用法:assert expression 【,argument】(异常参数可有可无)

>>> assert 1 == 0,'one does not equal zero'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError: one does not equal zero

-------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/zhuoyuezai/article/details/83823263