python中的断言
Hello dear readers! welcome back to another section of my tutorial on Python. In this tutorial post, we will be studying about Assertions in Python.
亲爱的读者您好! 欢迎回到我的Python教程的另一部分。 在本教程中,我们将研究Python中的断言。
什么是断言? (What is an Assertion?)
An assertion is a sanity-check that you can turn on or turn off when you are done with your program testing.
断言是一种健全性检查,可以在完成程序测试后打开或关闭。
The easiest way to think of an assertion is to link it to a raise-if statement (or to be more accurate, a raise-if-not statement). An expression is tested and if the result comes up false, then an exception is raised.
想到一个断言的最简单方法是将其链接到一个if-if语句(或更准确地说,是一个if-not-not语句)。 测试表达式,如果结果为假,则引发异常。
Assertions in Python are carried out by the assert statement, the newest keyword to Python, it was introduced in version 1.5
Python中的断言由assert语句执行,assert语句是Python的最新关键字,它是在1.5版中引入的
Python programmers often place assertions at the start of a function to check for valid input, and after a function call to check for valid output.
Python程序员经常将断言放在函数的开头以检查有效的输入,并在函数调用之后进行检查以检查有效的输出。
When an assert statement is being encountered, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, then an AssertionError exception is raised by Python.
当遇到断言语句时,Python会评估附带的表达式,希望它是正确的。 如果表达式为假,则Python会引发AssertionError异常。
句法 (Syntax)
The syntax for assert statement is -
assert语句的语法是-
assert Expression[, Arguments]
If the assertion fails, Python uses ArgumentExpression as the code argument for the AssertionError. The AssertionError exceptions can be of course caught and handled like any other exception using the try-except statement, but if it is not handled, they will then terminate the program and then produce a traceback.
如果断言失败,Python将ArgumentExpression用作AssertionError的代码参数。 当然,可以使用try-except语句捕获和处理AssertionError异常,就像处理其他任何异常一样,但是如果不处理,则它们将终止程序并产生回溯。
例 (Example)
Below is a function which converts a temperature from degrees Kelvin to degrees Fahrenheit. Since zero degrees Kelvin is as cold as it gets, then the function bails out if it sees negative temperature -
下面是将温度从开氏度转换为华氏度的函数。 由于开尔文温度为零,因此温度会尽可能低,因此如果看到负温度,该函数就会失效-
#!/usr/bin/python
def KelvinToFahrenheit(Temperature):
assert (Temperature >= 0),"Colder than absolute zero!"
return ((Temperature-273)*1.8)+32
print KelvinToFahrenheit(273)
print int(KelvinToFahrenheit(505.78))
print KelvinToFahrenheit(-5)
输出量 (Output)
When the above code is executed, it will produce the following result -
执行以上代码后,将产生以下结果-
32.0 451 Traceback (most recent call last): File "test.py", line 9, in <module> print KelvinToFahrenheit(-5) File "test.py", line 4, in KelvinToFahrenheit assert (Temperature >= 0),"Colder than absolute zero!" AssertionError: Colder than absolute zero!
Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Object Oriented Programming in Python.
好的,伙计们! 这是我们为本教程帖子准备的内容。 在我的下一个教程中,我们将讨论有关Python中的面向对象编程的知识。
Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
如有需要,请随时提出您的问题,我会尽快处理。 如果本教程对您有所帮助,则可以使用“共享”按钮共享本教程。
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
在各种社交媒体平台上关注我们,以获取最新教程的最新信息。 您也可以订阅我们的新闻通讯,以将我们的教程直接发送到您的电子邮件中。
Thanks for reading and bye for now.
感谢您的阅读和再见。
Originally published at https://www.webdesigntutorialz.com.
最初在https://www.webdesigntutorialz.com上发布。
翻译自: https://medium.com/@novatekz33/assertion-in-python-programming-97d78b5f8614
python中的断言