功能

Python 官方文档解释(https://docs.python.org/3/reference/simple_stmts.html#assert),:
“Assert statements are a convenient way to insert debugging assertions into a program”.
翻译一下就是断言语句是将调试断言插入程序的便捷方式。

语法

assert expression [, arguments]

用法

一般的用法是:

assert condition

用来让程序测试这个condition,如果conditionfalse,那么raise一个AssertionError出来。逻辑上等同于:

if not condition:raise AssertionError()

比如下面的例子

>>> assert 1==1
>>> assert 1==0
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>assert 1==0
AssertionError>>> assert True
>>> assert False
Traceback (most recent call last):File "<pyshell#3>", line 1, in <module>assert False
AssertionError>>> assert 3<2
Traceback (most recent call last):File "<pyshell#4>", line 1, in <module>assert 3<2
AssertionError

也可以增加描述信息:

>>> assert len(lists) >=5,'列表元素个数小于5'
Traceback (most recent call last):File "D:/Data/Python/helloworld/helloworld.py", line 1, in <module>assert 2>=5,'列表元素个数小于5'AssertionError: 列表元素个数小于5
>>> assert 2==1,'2不等于1'
Traceback (most recent call last):File "D:/Data/Python/helloworld/helloworld.py", line 1, in <module>assert 2==1,'2不等于1'AssertionError: 2不等于1

这样在运行结果中就能打印提示信息。

[Python] 关键字 assert相关推荐

  1. python 关键字大全_一日一技:用实例列举python中所有的关键字(01)

    Python关键字列表 本教程提供有关Python中使用的所有关键字的简要信息. 关键字是Python中的保留字.我们不能将关键字用作变量名,函数名或任何其他标识符. 以下是Python中所有关键字的 ...

  2. python关键字的意思_python 关键字(Keywords)

    2018-04-30 python 关键字(Keywords) 声明:本文章大部分来自下面博客,对其作者表示感谢.以后会有详细的关键字说明. 1.and: 表示逻辑'与' 2.del: 用于list列 ...

  3. [转载] python关键字和保留字_Python关键字

    参考链接: Python关键字和标识符 python关键字和保留字 关键词 (Keywords) Keywords are the reserved words in Python programmi ...

  4. Python关键字和标识符

    Today we will learn about Python keywords and identifiers. Earlier we learned how to install python ...

  5. Python之assert断言语句

    关键字assert构成断言语句,主要是可以在我们书写一个新的程序时,可以使用它帮我们锁定bug范围. 表达式: assert 表达式 '窗口提示的信息' 括号中的项目为选填项目,选填项目将会在表达式的 ...

  6. 查看python关键字的方法,查看python关键字的方法

    查看python关键字的方法 作者:ikeycn 和 python 相关 简介:这是查看python关键字的方法的详细页面,介绍了和python,python 查看python关键字的方法有关的知识. ...

  7. Python关键字(保留字)

    3.2 Python关键字(保留字) 保留字又称关键字,不可用于普通标识符.关键字的拼写必须与这里列出的完全一致.Python 的标准库提供了一个 keyword 模块,可以输出当前版本的所有关键字. ...

  8. python 关键字 keywords

    python关键字一览 Python Keywords False None① True and as② assert③ break class continue def del elif else ...

  9. python的assert作用

    python的assert作用 用处: 检查条件,不符合就终止程序,并且提示错误原因 代码: a=-1 #报错 assert (a>0),"a超出范围" 效果: Traceb ...

最新文章

  1. 排序 时间倒序_经典排序算法之冒泡排序(Bubble Sort)
  2. 算法--- 二叉树的右视图
  3. AIX的异步IO设置
  4. Lesson 6. Scikit-Learn使用与进阶
  5. mysql odbc安装提示找不到ian_如何解决mysql odbc安装丢失的问题
  6. 升级浏览器_星愿浏览器升级至6.3.2000.2001
  7. 用计算机计算电力系统故障,用计算机计算电力系统故障的方法.ppt
  8. 物联网开发- Arduino 101/Genuino 101
  9. C++入门教程(一)
  10. chm转换成txt的url顺序问题
  11. Retrofit2 详解和使用(一)
  12. 认识电信产品生命周期管理PLM及其PLM服务
  13. 浏览器如何工作:在现代web浏览器场景的之下
  14. 14Java第十四次课 五子棋的实现1
  15. Modem2G/3G/4G/5G:完整收录,2020最新MCC、MNC、运营商对照表,全球运营商MCCMNC查询列表
  16. java二进制对象_java 二进制搜索
  17. r7 4800h安装linux,华硕天选(R7-4800H) u盘pe如何重装win7系统
  18. 你的福利是咋被百度云盘给和谐的
  19. python开发游戏的优势和劣势-Python的设计理念和优缺点
  20. 电子电路设计基本概念100问(五)【学习目标:原理图、PCB、阻抗设计、电子设计基本原则、基本原器件等】

热门文章

  1. python项目开发实例-有趣的十个Python实战项目,让你瞬间爱上Python!
  2. 自学python买什么书比较好-python入门学习哪个书比较好(python视频教程知乎)
  3. python有哪些用途-Python是什么 Python的用处
  4. 语音识别模块 LD3320
  5. 算法-罗马数字转成整数(leetcode13)
  6. 【Java笔记】四种权限修饰符总结
  7. 【动态规划笔记】完全背包问题及优化
  8. java水晶报表pull模式_水晶报表Pull模式 每次调用水晶报表的时候都要输入数据库密码的解决方案...
  9. paypal如何支付欧元_国际在线支付巨头——PayPal
  10. Kubernetes 搭建 Kafka 和 Zookeeper 集群(完整教程)