Python assert语句的简单使用示例
作者:王尼玛 时间:2023-06-12 16:38:58
写在前面
最近每日一更,我这菜鸡都有点儿不好意思了
简单介绍
简单用法是:
assert expression
让我们用程序来测试这个expression,如果expression相当于False,那么raise一个AssertionError出来。
即逻辑上等同于:
if not expression:
raise AssertionError
简单看看这些例子:
>>> assert True
>>> assert False
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
assert False
AssertionError
>>> assert 1==1
>>> assert 1==0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
assert 1==0
AssertionError
>>> assert [1, 2] # 非空列表值得注意一下,虽说也没个啥,哈哈
>>> assert not [1, 2]
Traceback (most recent call last):
File "<ipython-input-48-eae410664122>", line 1, in <module>
assert not [1, 2]
AssertionError
为assert断言语句添加异常参数
assert的异常参数,其实就是在断言表达式后添加字符串信息,一般用来解释断言。格式如下:
assert expression [, arguments]
assert 表达式 [, 参数]
举例请看之后的代码
一些重要的细节
老铁们可以试着运行一下以下代码段:
>>> assert None, 'None若作为布尔表达式,则相当于False'
>>> assert [], '空列表若作为布尔表达式,则相当于False'
>>> assert (), '空元组若作为布尔表达式,则相当于False'
>>> assert {}, '空字典若作为布尔表达式,则相当于False'
>>> assert set(), '空集合若作为布尔表达式,则相当于False'
>>> assert '', '空字符串若作为布尔表达式,则相当于False'
当然还有奇葩的numpy
>>> a = np.array([1, 2])
>>> assert a
Traceback (most recent call last):
File "<ipython-input-45-63e954d94e9b>", line 1, in <module>
assert aa
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
是的,你没看错,哪里有numpy,哪里就有Use a.any() or a.all()......
最后,再试一试这俩吧:
>>> assert np.array([])
>>> assert np.array([[], []])
是的,只要是空的,甭管是几维的,都相当于False
来源:https://segmentfault.com/a/1190000019894418
标签:python,assert,语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP 危险函数全解析
2023-11-20 15:36:12
python xlwt如何设置单元格的自定义背景颜色
2022-07-25 10:41:05
详述numpy中的np.random.random()系列函数用法
2023-08-08 00:25:09
![](https://img.aspxhome.com/file/2023/5/97415_0s.png)
django中的auth模块与admin后台管理方法
2022-11-22 00:01:21
![](https://img.aspxhome.com/file/2023/4/102754_0s.png)
TensorFlow tf.nn.conv2d实现卷积的方式
2021-07-19 08:54:28
MySQL 8.0新功能监控统计限制连接不再担心被垃圾SQL搞爆内存
2024-01-16 12:51:25
获取键盘键的值 集合 方便监控键盘事件
2023-12-11 07:17:02
对django 2.x版本中models.ForeignKey()外键说明介绍
2022-10-07 18:18:24
7个好用的TypeScript新功能
2024-04-23 09:04:00
![](https://img.aspxhome.com/file/2023/2/135942_0s.jpg)
vue 点击展开显示更多(点击收起部分隐藏)
2024-05-29 22:47:56
![](https://img.aspxhome.com/file/2023/9/122999_0s.png)
解决Navicat for Mysql连接报错1251的问题(连接失败)
2024-01-27 04:29:52
![](https://img.aspxhome.com/file/2023/5/109825_0s.png)
MySQL内部临时表的具体使用
2024-01-20 13:09:54
![](https://img.aspxhome.com/file/2023/9/117829_0s.jpg)
asp彩色验证码的制作详解
2007-09-18 13:22:00
Vue实现简单跑马灯特效
2024-04-30 10:31:05
![](https://img.aspxhome.com/file/2023/4/130164_0s.jpg)
ie9后浏览器fckeditor无法上传图片、弹出浮层内容不显示的解决方法
2023-05-08 03:34:30
诊断SQLSERVER问题常用的日志概述及使用
2024-01-14 04:38:16
![](https://img.aspxhome.com/file/2023/9/92789_0s.jpg)
oracle中的ID号实现自增长的方法
2024-01-13 13:27:26
django框架实现一次性上传多个文件功能示例【批量上传】
2022-03-30 09:00:43
![](https://img.aspxhome.com/file/2023/7/66767_0s.png)
如何创建一个对索引服务器进行查询的ASP页面?
2009-11-14 20:54:00
python-httpx的使用及说明
2022-11-22 15:35:18