Python assert usage code parsing, pythonassert

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.