当前位置:首页 » python教程 » 正文

Python3 assert断言实现原理解析

看: 1029次  时间:2020-08-09  分类 : python教程

语法格式如下:

assert expression

等价于:

if not expression:
 raise AssertionError

assert 后面也可以紧跟参数:

assert expression [, arguments]

等价于:

if not expression:
 raise AssertionError(arguments)

以下为 assert 使用实例:

>>> assert True   # 条件为 true 正常执行
>>> assert False  # 条件为 false 触发异常
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError
>>> assert 1==1  # 条件为 true 正常执行
>>> assert 1==2  # 条件为 false 触发异常
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError 
>>> assert 1==2, '1 不等于 2'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError: 1 不等于 2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!