# Python 学习笔记(四) ## [Python]断言assert的用法 assert的语法格式:assert expression,它的等价语句为: ```python if not expression: raise AssertionError ``` 这段代码用来检测数据类型的断言,因为 `a_str` 是 `str` 类型,所以认为它是 `int` 类型肯定会引发错误。 ```python >>> a_str = 'this is a string' >>> type(a_str) >>> assert type(a_str)== str >>> assert type(a_str)== int Traceback (most recent call last): File "", line 1, in assert type(a_str)== int AssertionError ```