Python assert usage code parsing, pythonassert
When developing a program, instead of making it crash during running, it is better to crash when it has an error condition (an error is returned ). Assert is very useful at this time.
Python assert asserted that the Boolean value must be true. If an exception occurs, the expression is false.
It can be understood that the assert asserted statement is raise-if-not, which is used to test the expression. if its return value is false, an exception is triggered.
Syntax format of assert:
Assert expression
Its equivalent statement is:
If not expression: raise AssertionError
This code is used to detect data type assertions. Because a_str is of the str type, it is regarded as an int type and will certainly cause an error.
>>> A_str = 'this is a string '>>> type (a_str) <type 'str' >>> assert type (a_str) ==str >>> assert type (a_str) = intTraceback (most recent call last): File "<pyshell #41>", line 1, in <module> assert type (a_str) = intAssertionError
Summary
The above is all about parsing the usage code of the Python assert, and I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!