语法:
assert 条件,参数
如果条件满足,继续执行;
条件不满足,抛出异常以及参数,此处的参数相当于注释。
def foo(n):
assert n, "n is zero."
print(10/n)
if __name__=="__main__":
foo(0)
#output
/home/yjys/anaconda3/bin/python3.8 /home/yjys/PycharmProjects/TEST/validation/test/assert_test.py
Traceback (most recent call last):
File "/home/yjys/PycharmProjects/TEST/validation/test/assert_test.py", line 6, in <module>
foo(0)
File "/home/yjys/PycharmProjects/TEST/validation/test/assert_test.py", line 2, in foo
assert n, "n is zero."
AssertionError: n is zero.
Process finished with exit code 1