个人博客,支持一下呗!https://raycoder.me
本文首发于Ray's Blog
什么是assert
?它的中文名叫做断言
。我们先来看一个简单的例子:
age = int(input())
if age>=18:
print('You can watch it!')
else:
print('You are too young!')
这个例子进行了一下18G操作,没有达到18岁的人会被拒之门外友善的提示。
不过,我们可以通过assert
关键字来实现同等的操作。
>>> age = int(input())
17
>>> assert age >= 18
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
assert age >= 18
AssertionError
age = int(input())
try:
assert age >= 18
print('You can watch it!')
except AssertionError:
print('You are too young!')
这只是一个简单的例子,assert
还可以进行更复杂的操作。
引用一段菜鸟教程。
assert
的语法格式如下:
assert expression
等价于:
if not expression:
raise AssertionError
assert
后面也可以紧跟参数:
assert expression [, arguments]
等价于:
if not expression:
raise AssertionError(arguments)
如:
>>> 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
>>>
以下实例判断当前系统是否为 Linux,如果不满足条件则直接触发异常,不必执行接下来的代码:
import sys
assert ('linux' in sys.platform), "该代码只能在 Linux 下执行"
# 接下来要执行的代码
可以大大优化我们的代码,也可以减少if
、elif
、else
的使用。
这个关键字也可以作校验用,比如我们从网页上下载了一个代码,可以用assert
来断言本地代码与网页代码相同,否则报错。