python assert断言的用法
2017-11-18 18:38
351 查看
使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单。在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件时就崩溃,这时候就需要assert断言的帮助。
一般的用法是:
用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError出来。逻辑上等同于:
比如下面的例子:
参考:
https://www.cnblogs.com/cedrelaliu/p/5948567.html
https://www.cnblogs.com/liuchunxiao83/p/5298016.html
一般的用法是:
assert condition
用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError出来。逻辑上等同于:
if not condition: raise AssertionError()
比如下面的例子:
>>> assert 1==1 >>> assert 1==0 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> assert 1==0 AssertionError >>> assert True >>> assert False Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> assert False AssertionError >>> assert 3<2 Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> assert 3<2 AssertionError
参考:
https://www.cnblogs.com/cedrelaliu/p/5948567.html
https://www.cnblogs.com/liuchunxiao83/p/5298016.html
相关文章推荐
- Python断言assert的用法代码解析
- python3 assert 断言的用法 (区别于python2)
- Python 断言assert用法
- python中assert断言的用法
- python assert 断言详细用法格式
- [Python]断言assert的用法
- Python assert断言异常语句格式及用法
- python assert 断言基本用法
- python 断言assert用法
- python assert 断言详细用法格式
- 断言(assert)的用法
- 断言(ASSERT)的用法
- java中assert(断言)用法
- assert()断言函数以及NODEBUG用法总结
- python assert断言
- python中assert、isinstance的用法
- python中小用法之assert,*arg,**arg用法全解
- python assert dict 断言@对比@之递归断言
- 断言(assert)的用法
- assert 断言用法总结