概述
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关键字的全部内容,希望文章能够帮你解决python中assert_在Python中带有示例的assert关键字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复