断言:assert
2015-07-02 10:17
239 查看
if not condition:
crash program
与其让程序晚点崩溃,不然在错误的条件出现时直接崩溃。哈哈。以为然。
在检查函数参数的属性时,或者作为初期测试和调试过程中的辅助条件。语句中使用关键字:assert
>>> age = 16
>>> assert 0 < age < 120
>>> age = -1
>>> assert 0 < age <120
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
assert 0 < age <120
AssertionError
>>>
如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert语句就有用公里。它可以在在程序中置入检查点。
条件后可以添加字符串,用来解释断言。
>>> age = -1
>>> assert 0 <age < 120 ,'The age must be realistic'
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
assert 0 <age < 120 ,'The age must be realistic'
AssertionError: The age must be realistic
>>>
crash program
与其让程序晚点崩溃,不然在错误的条件出现时直接崩溃。哈哈。以为然。
在检查函数参数的属性时,或者作为初期测试和调试过程中的辅助条件。语句中使用关键字:assert
>>> age = 16
>>> assert 0 < age < 120
>>> age = -1
>>> assert 0 < age <120
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
assert 0 < age <120
AssertionError
>>>
如果需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert语句就有用公里。它可以在在程序中置入检查点。
条件后可以添加字符串,用来解释断言。
>>> age = -1
>>> assert 0 <age < 120 ,'The age must be realistic'
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
assert 0 <age < 120 ,'The age must be realistic'
AssertionError: The age must be realistic
>>>
相关文章推荐
- markdown语法介绍
- Hadoop学习
- 6月份全球域名商(国际域名)解析量排行榜TOP20
- ibeacon笔记
- Python笔记:virtualenv virtualenvwarpper
- Thrift入门
- PDB符号文件与Windows下利用Windbg 分析dump
- H5+PhoneGap开发学习资源
- Why No Guaranteed Delivery
- 实现可以滑动的GrildView,类似美团网首页的GrildView功能菜单
- eclipse编译异常修正:the project cannot be built until its prerequisite...
- 升级xcode6.4之后插件不能使用的问题
- 软件分发管理器Secure Delivery Center基本概念概述(四)
- sql语句中针对日期格式的处理
- UVA 11489 Integer Game(博弈,规律)
- Android studio第一次使用配置(三)gradle项目构建
- MasterCAM二次开发(一)
- C# MongoDB 操作
- PHP上传文件限制的大小
- 转:HashMap深度解析(一)