python assert 语句,又称断言语句,可以看做是功能缩小版的 if 语句,它用于判断某个表达式的值,如果值为真,则程序可以继续往下执行;反之,python 解释器会报 assertionerror 错误。

assert 语句的语法结构为:

assert 表达式

assert 语句的执行流程可以用 if 判断语句表示(等效),如下所示:

if 表达式==true:
程序继续执行
else:
程序报 assertionerror 错误

有读者可能会问,明明 assert 会令程序崩溃,为什么还要使用它呢?这是因为,与其让程序在晚些时候崩溃,不如在错误条件出现时,就直接让程序崩溃,这有利于我们对程序排错,提高程序的健壮性。
因此,assert 语句通常用于检查用户的输入是否符合规定,还经常用作程序初期测试和调试过程中的辅助工具。
下面的程序演示了 assert 语句的用法:

mathmark = int(input())
#断言数学考试分数是否位于正常范围内
assert 0 <= mathmark <= 100
#只有当 mathmark 位于 [0,100]范围内,程序才会继续执行
print(“数学考试分数为:”,mathmark)

运行该程序,测试数据如下:

90

数学考试分数为: 90

再次执行该程序,测试数据为:

159

traceback (most recent call last):
file “c:\users\mengma\desktop\file.py”, line 3, in <module>
assert 0 <= mathmark <= 100
assertionerror

可以看到,当 assert 语句后的表达式值为真时,程序继续执行;反之,程序停止执行,并报 assertionerror 错误。

常用断言函数
常用

这里介绍几个常用断言的使用方法,可以一定程度上帮助大家对预期结果进行判断。-

  • assertequal
  • assertnotequal
  • asserttrue
  • assertfalse
  • assertisnone
  • assertisnotnone
  • assertequal 和 assertnotequal
  • assertequal:如两个值相等,则pass
  • assertnotequal:如两个值不相等,则pass

使用方法:

assertequal(first,second,msg)其中first与second进行比较,如果相等则通过;msg为失败时打印的信息,选填;断言assertnotequal反着用就可以了。

  • asserttrue和assertfalse
  • asserttrue:判断bool值为true,则pass
  • assertfalse:判断bool值为false,则pass

使用方法:

  • asserttrue(expr,msg)其中express输入相应表达式,如果表达式为真,则pass;msg选填;断言assertfalse如果表达式为假,则pass
  • assertisnone和assertisnotnone
  • assertisnone:不存在,则pass
  • assertisnotnone:存在,则pass

使用方法:

assertisnone(obj,msg)检查某个元素是否存在

总结:

常用

assertequal(a, b) a == b
assertnotequal(a, b) a != b
asserttrue(x) bool(x) is true
assertfalse(x) bool(x) is false
assertis(a, b) a is b 2.7
assertisnot(a, b) a is not b 2.7
assertisnone(x) x is none 2.7
assertisnotnone(x) x is not none 2.7
assertin(a, b) a in b 2.7
assertnotin(a, b) a not in b 2.7
assertisinstance(a, b) isinstance(a, b) 2.7
assertnotisinstance(a, b) not isinstance(a, b) 2.7

其它

assertalmostequal(a, b) round(a-b, 7) == 0
assertnotalmostequal(a, b) round(a-b, 7) != 0
assertgreater(a, b) a > b 2.7
assertgreaterequal(a, b) a >= b 2.7
assertless(a, b) a < b 2.7
assertlessequal(a, b) a <= b 2.7
assertregexpmatches(s, re) regex.search(s) 2.7
assertnotregexpmatches(s, re) not regex.search(s) 2.7
assertitemsequal(a, b) sorted(a) == sorted(b) and works with unhashable objs 2.7
assertdictcontainssubset(a, b) all the key/value pairs in a exist in b 2.7
assertmultilineequal(a, b) strings 2.7
assertsequenceequal(a, b) sequences 2.7
assertlistequal(a, b) lists 2.7
asserttupleequal(a, b) tuples 2.7
assertsetequal(a, b) sets or frozensets 2.7
assertdictequal(a, b) dicts 2.7
assertmultilineequal(a, b) strings 2.7
assertsequenceequal(a, b) sequences 2.7
assertlistequal(a, b) lists 2.7
asserttupleequal(a, b) tuples 2.7
assertsetequal(a, b) sets or frozensets 2.7assertdictequal(a, b) dicts 2.7

使用时机:

那么我们什么时候应该使用断言呢?如果没有特别的目的,断言应该用于如下情况:

  • 防御性的编程
  • 运行时对程序逻辑的检测
  • 合约性检查(比如前置条件,后置条件)
  • 程序中的常量
  • 检查文档
  • 例:防御性编程中添加断言

在代码开始添加注释是个好的开端,但是人们都不太喜欢读和更新这些注释,这些注释会很快变得过时。但对于断言,我们可以同时对这块代码编写文档,如果这些断言被违反了,会直接引起一个简单而又直接的失败。

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