程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python practical skills - make good use of assert

編輯:Python

List of articles

  • 1 Python The use of interruptions
  • 2 for instance
  • 3 assert Summary of usage

1 Python The use of interruptions

assert yes Python3 One of the keywords in , Used to... An expression “ Assertion ”, When the result of the expression is False when , Throw out AeertError abnormal , When the expression results in True when , Continue with the rest of the code .

assert Used to determine whether the program should continue , If there are some environmental problems or other problems , You can end the program directly , There is no need to report an error after the program is executed , It's good for efficiency , Early exposure of problems . For example, a program can only be used in linux Running under the system , Then you can add an assertion at the beginning of the program , If not, report the error directly .

2 for instance

  • use if Judge
import sys
if sys.platform != 'linux':
raise AssertionError(" This procedure must be in Linux Run under ")
""" Traceback (most recent call last): File "D:/coder/wechatMiniBackEnd/test/test.py", line 13, in <module> raise AssertionError(" This procedure must be in Linux Run under ") AssertionError: This procedure must be in Linux Run under """
  • Using assertions
import sys
assert (sys.platform == 'linux'), " This procedure must be in Linux Run under "

The output mode of the two modes is the same

3 assert Summary of usage

Assert the result of an expression , If it is True, Then the code continues to run , If it is False, Report errors AssertionError, The optional error prompt statement

assert expression [, argument]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved