assert 断言 「Python」

  • 学一点语法

  • 断言是一种错误/异常提示,正确使用有助减少代码可能出现的错误

  • 给出示例

assert 1
print("heheda")

assert 0
print("no heheda")
  • 输出
heheda
Traceback (most recent call last):
  File "/Users/断言.py", line 5, in <module>
    assert 0
AssertionError

可见,当assert后面条件为真时,可以执行后面的
当条件为假,则输出错误提示。

参考:
https://www.runoob.com/python3/python3-assert.html

发布了51 篇原创文章 · 获赞 1 · 访问量 3091

猜你喜欢

转载自blog.csdn.net/m0_38139098/article/details/104751230