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 .
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 """
import sys
assert (sys.platform == 'linux'), " This procedure must be in Linux Run under "
The output mode of the two modes is the same
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]