python中assert

Python断言关键字 (Python assert keyword)

assert is a keyword (case-sensitive) in python, it is used to debug the code. Generally, it can be used to test a condition – if a condition returns False – it returns an Assertion Error (AssertionError).

assert是python中的一个关键字(区分大小写),用于调试代码。 通常,它可以用于测试条件-如果条件返回False-则返回断言错误(AssertionError)

Syntax of assert keyword

assert关键字的语法

    assert statement

Example:

例:

    Input:
num = 10
# assert statement
assert num==20
Output:
assert num==20
AssertionError

断言关键字的Python示例 (Python examples of assert keyword)

Example 1: Test a False result

示例1:测试错误的结果

# python code to demonstrate example of
# assert statement
num = 10    # number
# assert statements
# nothing will happed as the condition is true
assert num==10
#AssertionError will generate as the condition is false
assert num==20

Output

输出量

Traceback (most recent call last):
File "/home/main.py", line 12, in <module>
assert num==20
AssertionError

Example 2: Test a False result and return a customize message

示例2:测试错误结果并返回自定义消息

# python code to demonstrate example of
# assert statement
num = 10    # number
# assert statements
# nothing will happed as the condition is true
assert num==10
#AssertionError will generate as the condition is false
assert num==20, "There is an error with num's value"

Output

输出量

Traceback (most recent call last):
File "/home/main.py", line 12, in <module>
assert num==20, "There is an error with num's value"
AssertionError: There is an error with num's value

翻译自: https://www.includehelp.com/python/assert-keyword-with-example.aspx

python中assert

python中assert_在Python中带有示例的assert关键字相关推荐

  1. python wait方法_Python条件类| 带有示例的wait()方法

    python wait方法 Python Condition.wait()方法 (Python Condition.wait() Method) wait() is an inbuilt method ...

  2. python线程任务run_Python线程类| 带有示例的run()方法

    python线程任务run Python Thread.run()方法 (Python Thread.run() Method) Thread.run() method is an inbuilt m ...

  3. python 示例_在Python中带有示例的while关键字

    python 示例 关键字的Python (Python for keyword) while is a keyword (case-sensitive) in python, it is used ...

  4. python 示例_Python中带有示例的class关键字

    python 示例 Python类关键字 (Python class keyword) class is a keyword (case-sensitive) in python, it is use ...

  5. python调用带参函数_Python | 带有示例的函数调用类型

    python调用带参函数 There are following types of function calls in python: python中有以下类型的函数调用: Call by value ...

  6. 关键字驱动测试示例_带有示例的False关键字

    关键字驱动测试示例 Python False关键字 (Python False keyword) False is a keyword (case-sensitive) in python, it i ...

  7. python绘制三维曲面图-Python中使用Matplotlib绘制3D图形示例

    原标题:Python中使用Matplotlib绘制3D图形示例 3D图形能给我们对数据带来更加深入地理解.python的matplotlib库就包含了丰富的3D绘图工具.3D图形在数据分析.数据建模. ...

  8. python中延时函数_python中实现延时回调普通函数示例代码

    python中实现延时回调普通函数示例代码 这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...

  9. python解析xml文件elementtree_Python中使用ElementTree解析XML示例

    [XML基本概念介绍] XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. 概念一: 复制代码 代码如下: # foo元素的起始标签 ...

最新文章

  1. gacutil.exe Path
  2. 4.1.7 文件共享
  3. 【PM模块】维护业务处理流程—内部维护(维护工单)
  4. 生物信息学搞计算机,生物信息学前景展望,谈谈感想(已经停止)
  5. (1)编译安装lamp三部曲之apache-技术流ken
  6. 多图证明,Java到底是值传递还是引用传递?
  7. 利用sender的Parent获取GridView中的当前行
  8. 除了允许变量被const函数修改之外,'mutable'关键字是否有任何其他用途?
  9. hbase 使用lzo_【hbase】Hbase最佳实践
  10. C语言 数据结构 结构体 LinkList
  11. 便签pc android同步,微软电脑sticky notes便签软件怎么和安卓手机便签同步?
  12. 中国柔性材料行业发展建议及竞争态势研究报告2021年版
  13. 【301】怪诞行为学-可预测的非理性
  14. 为什么CTO、技术总监、架构师都不写代码,还这么厉害
  15. 【Unity】用Lerp()实现类杀戮尖塔手牌变化
  16. SpringBoot整合SpringDataElasticSearch 完成地理搜索 附近人搜索 距离搜索
  17. ESXI6.7安装补丁
  18. 查看twitter浏览记录_您可以看到谁查看了您的Twitter个人资料吗?
  19. java程序连接redis服务器
  20. 风雨砥砺,岁月如歌——Ts之箭头函数

热门文章

  1. linux中如何复制文件并重命名_linux 下怎么复制一个文件到另外一个目录并且重命名...
  2. date转timestamp格式_技术分享 | MySQL:timestamp 时区转换导致 CPU %sy 高的问题
  3. android sdk是灰的,Android Studio 2.3 sdk管理器标签灰显
  4. 洛谷P1725琪露诺【单调队列+dp】
  5. 存储芯片在智能化产业链中扮演的角色将更加重要
  6. MSSQL-最佳实践-如何监控备份还原进度
  7. Thrift源码学习二——Server层
  8. centos配置ipv6地址
  9. 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现
  10. 看美文,记单词(6)