python中assert用法实例分析

WBOY
Release: 2016-06-06 11:15:30
Original
1482 people have browsed it

本文实例讲述了python中assert用法。分享给大家供大家参考。具体分析如下:

1、assert语句用来声明某个条件是真的。

2、如果你非常确信某个你使用的列表中至少有一个元素,而你想要检验这一点,并且在它非真的时候引发一个错误,那么assert语句是应用在这种情形下的理想语句。

3、当assert语句失败的时候,会引发一AssertionError。

测试程序:

>>> mylist = ['item']
>>> assert len(mylist) >= 1
>>> mylist.pop()
'item'
>>> assert len(mylist) >= 1
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError
>>>
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template