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 divide
assert 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:
![python assert](https://cdn.journaldev.com/wp-content/uploads/2017/09/assert1.png)
输出将是:
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模块测试代码的信息 。
python断言