python断言

Hello everyone!! In this tutorial we will learn about python assert keyword. Python assert helps us to debug code. If you want to simulate your code like what is happening in which stage, then you can use python assert statements in your code. What is the expectation for the assignment of any variable can be detected using assert keyword in Python?

大家好!! 在本教程中,我们将学习python assert 关键字 。 Python assert可以帮助我们调试代码。 如果要模拟代码在哪个阶段发生的情况,则可以在代码中使用python assert语句。 使用Python中的assert关键字可以检测到对任何变量赋值的期望是什么?

Python断言 (Python assert)

The following is the basic structure of python assert statements:

以下是python assert语句的基本结构:

assert condition

You can also send information with the assert statement for the better understanding of the fault of the code.

您也可以使用assert语句发送信息,以更好地了解代码错误。

Following is the way of giving a message with assert statement:

以下是使用assert语句给出消息的方式:

assert condition, your message

Python断言语句 (Python assert statement)

Python assert statement takes a condition, the condition needs to be true. If the condition is true, that means the assertion of the value of the variable is ok, then the program will run smoothly and the next statements will be executed. But, if the condition is false ( that means there is some bug in our code) then it raises an exception.

Python断言语句带有一个条件,该条件必须为true。 如果条件为真,则意味着变量值的确定是正确的,然后程序将平稳运行并执行下一条语句。 但是,如果条件为假(这意味着我们的代码中存在一些错误),则会引发异常。

Python断言示例 (Python assert example)

We want to write a function that will return the quotient of two number. The following is the code:

我们要编写一个函数,该函数将返回两个数字的商。 以下是代码:

# defining the function definition
def divide(num1, num2):assert num2 > 0 , "Divisor cannot be zero"return num1/num2
# calling the divide function
a1 = divide(12,3)
# print the quotient
print(a1)
# this will give the assertion error
a2 = divide(12,0)
print(a2)

If we run the above code then the output will be:

如果我们运行上面的代码,那么输出将是:

4.0
Traceback (most recent call last):File "D:/T_Code/PythonPackage3/Assert.py", line 10, in a2 = divide(12,0)File "D:/T_Code/PythonPackage3/Assert.py", line 3, in divideassert num2>0 , "Divisor cannot be zero"
AssertionError: Divisor cannot be zero

In the third line of the above code, you can see the assert statement. In this line, it is checked that whether the variable num2 value is greater than 0 or not. If greater than zero i.e. condition is true, then no problem occurs and we get the output accordingly.

在以上代码的第三行中,您可以看到assert语句。 在这一行中,检查变量num2的值是否大于0。 如果大于零(即条件为真),则不会发生任何问题,我们将得到相应的输出。

But when we called the function division() with the 2nd argument 0, then the assert condition is false. That is why a AssertionError occurs and it gives the message “Divisor cannot be zero”, that we wrote in the message part of the python assert statement. Read more about python exception handling.

但是,当我们使用第二个参数0调用函数divide()时,则断言条件为false。 这就是为什么出现AssertionError并给出消息“除数不能为零”的原因,我们在python assert语句的消息部分中编写了该消息。 阅读有关python异常处理的更多信息。

Python断言变量替换示例 (Python assert example with variable replacement)

Consider the following code, we are trying to find square root of the equation say (b2 - 4ac).

考虑下面的代码,我们试图找到方程式(b2 - 4ac)平方根。

import math
def sqrt(a,b,c):assert b*b >= 4*a*c, "Cannot find square root of negative number, found %s < %s" % (b*b, 4*a*c)return math.sqrt(b*b - 4*a*c)print(sqrt(10, 12, 3))
# this will cause assertion error
print(sqrt(-4, 5, -3))

Output will be:

输出将是:

This is how we can use python assert statements to debug and find the bugs in our code in the testing phase. You can learn more about testing code using unitest module.

这就是我们在测试阶段可以使用python assert语句调试和查找代码中的错误的方式。 您可以了解更多有关使用unitest模块测试代码的信息 。

翻译自: https://www.journaldev.com/15791/python-assert

python断言

python断言_Python断言相关推荐

  1. python异常处理_Python入门 断言与异常处理

    一.断言 断言,可以理解为判断是否断开的预言. assert 表达式 , 描述 表达式为我们的预期结果,当表达式的结果为False时,抛出 AssertionError 异常,如无异常捕获程序遇到异常 ...

  2. python unittest断言_python unittest之断言及示例

    assert.png 前言 python unintest单元测试框架提供了一整套内置的断言方法. 如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态 如果异常,则当做错误来 ...

  3. python笔记:断言assert

    Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常 断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况

  4. 【学习笔记】31、Python中的断言

    Python中的断言 一 .初识断言 功能:Python内置的assert语句(断言)可以用来自动检测Python程序中的错误,让程序更可靠更易于调试. 含义:断言语句是一种调试工具,用来测试某个断言 ...

  5. python中的断言

    # python中的断言方法举例a = 2 assert(a > 1) print("断言成功,程序继续向下执行") b = 2 assert(b >= 3), &qu ...

  6. Python——assert(断言)主进程级的终止判断语句

    1 致谢 感谢菜鸟教程的帮助! 原文链接如下: https://www.runoob.com/python3/python3-assert.html 1 前言 今天在看代码的时候,又看到了assert ...

  7. python中的断言是什么意思_一日一技:python中的断言

    一.使用python中的断言来自动检测python程序中的错误,让程序更加可靠且更易于调试 从根本上来说,python中的断言语句是一种调试工具,用来测试某个断言条件,如果断言条件为True,则程序将 ...

  8. Python 正则 —— 正则表达式断言

    有关正则表达式的前要知识 正则表达式断言解释 正则表达式断言有以下类型: 零宽正向先行断言: 零宽负向先行断言: 零宽正向后行断言: 零宽负向后行断言. 所谓的零宽断言,也就是与想要匹配字段的中间不能 ...

  9. 一日一技:python中的断言

    一.使用python中的断言来自动检测python程序中的错误,让程序更加可靠且更易于调试: 从根本上来说,python中的断言语句是一种调试工具,用来测试某个断言条件,如果断言条件为True,则程序 ...

  10. python画代码-Python教程_Python画Mandelbrot集 代码

    Python教程_Python画Mandelbrot集 代码 作者:Comet 来源: 课课家 www.kokojia.com点击数:278发布时间:2015-06-19 11:17:19 曼德勃罗集 ...

最新文章

  1. Wireshark数据抓包分析(网络协议篇)第1章网络协议抓包概述
  2. VB.NET中的日期时间转换
  3. 【电商福利】双十二优惠福利专场
  4. oracle 取当天日期减一天 应该如何写
  5. TaskTracker获取并执行map或reduce任务的过程(一)
  6. 【MySQL性能优化】概述与优化方面(一)
  7. 重学java基础第七课:什么是计算机
  8. 曾经辉煌无限,如今员工持续大量流失,集团目前仅剩10余人
  9. Android常用开发网址(持续更新)
  10. Linux系统基本操作(二)—设置本地光盘为yum源
  11. python图形库使用教程_PySide——Python图形化界面入门教程(四)
  12. smart210 资源
  13. svn合并分支到主干,工具操作
  14. 计算机科学家壁纸,电脑励志好看的文字壁纸推荐
  15. 杭电oj刷题第一阶段答案
  16. CentOS8.3安装时安装源设置基础软件仓库时出错
  17. 这是我见过最好的唐诗,而且通俗易懂2
  18. Linux基础命令实例
  19. 美国人口统计数据MATLAB,根据表的数据,完成下列数据拟合问题:美国人口统计数据 年份 1790 1800 1810 1820 1830 1840...
  20. 工业控制计算机固态硬盘,工业级SSD接口全解析,懂了你才会选对工业级SSD

热门文章

  1. java获取大写字母_获取中文大写首字母java实现
  2. 在Excel表中进行度分秒单位转换
  3. qpython3编辑器手机版下载_QPython3
  4. 计算机相关美文摘抄,唯美的散文段落摘抄
  5. python写法教程_Python的表达式写法
  6. cannot be cast to com.activiti.common.config.ICustomProcessDiagramGenerator
  7. java微信公众号图文消息编辑器,如何使用微信公众号自带的编辑器做出简洁舒适的图文排版...
  8. 在大学里我们应该学习什么
  9. 计算机桌面移至其它盘,win7系统电脑桌面文件转移到其他盘的操作方法
  10. 厉害了,10行代码实现抽奖助手自动参与抽奖