如何在python中正确使用断言?
How to properly use assertion in python?
我有一个 python 程序需要在执行前断言很多嵌套条件。
Python 有办法用 assert
语句断言,
语法:assert condition, error_message(optional)
但是,我需要断言多个嵌套条件,并且可能在断言中执行多个语句。
在 python 中使用断言的正确方法是什么?
我的想法是:只有当b > a
为true
时才检查c
,只有当a > c
都为真时才检查a > c
。在断言后执行多个语句,如:- 记录信息并打印信息等。
伪代码:
if c != null then
if b > a then
if a > c then
print 'yippee!'
else
throw exception('a > c error')
else
throw exception('b > a error')
else
throw exception('c is null')
你的意思是这样的吗?
a = 1
b = 2
c = 3
assert b > a and c > b and a > c, 'Error: a is not greater than c'
输出:
Traceback (most recent call last):
File "main.py", line 6, in <module>
assert b > a and c > b and a > c, 'Error: a is not greater than c'
AssertionError: Error: a is not greater than c
something like this:- check c only when b > a is true and check a > c only both are true. And after assertion executes multiple statements like:- log the info and print the info etc..
您可以一个接一个地使用多个 assert
语句,就像您将在彼此下编写多个 if 语句一样,除了您必须考虑到您的 assert
可以引发一个异常,您需要处理。这样你就可以简单地控制执行流程,并且print/log任何你需要的......例如像这样的东西:
def make_comperations(a, b, c = None): # note c is optional
assert c is not None, 'Error: c is None'
assert b > a, 'Error: a is not greater than b'
assert c > a, 'Error: c is not greater than a'
try:
make_comperations(1, 2, 3) # ok
make_comperations(1, 2) # error
make_comperations(1, 2, 0) # error, but won't be executed since the line above will throw an exception
print("All good!")
except AssertionError as err:
if str(err) == 'Error: c is None':
print("Log: c is not given!")
if str(err) == 'Error: a is not greater than b':
print("Log: b > a error!")
elif str(err) == 'Error: c is not greater than a':
print("Log: c > a error!")
输出:
Log: c is not given!
我有一个 python 程序需要在执行前断言很多嵌套条件。
Python 有办法用 assert
语句断言,
语法:assert condition, error_message(optional)
但是,我需要断言多个嵌套条件,并且可能在断言中执行多个语句。
在 python 中使用断言的正确方法是什么?
我的想法是:只有当b > a
为true
时才检查c
,只有当a > c
都为真时才检查a > c
。在断言后执行多个语句,如:- 记录信息并打印信息等。
伪代码:
if c != null then
if b > a then
if a > c then
print 'yippee!'
else
throw exception('a > c error')
else
throw exception('b > a error')
else
throw exception('c is null')
你的意思是这样的吗?
a = 1
b = 2
c = 3
assert b > a and c > b and a > c, 'Error: a is not greater than c'
输出:
Traceback (most recent call last):
File "main.py", line 6, in <module>
assert b > a and c > b and a > c, 'Error: a is not greater than c'
AssertionError: Error: a is not greater than c
something like this:- check c only when b > a is true and check a > c only both are true. And after assertion executes multiple statements like:- log the info and print the info etc..
您可以一个接一个地使用多个 assert
语句,就像您将在彼此下编写多个 if 语句一样,除了您必须考虑到您的 assert
可以引发一个异常,您需要处理。这样你就可以简单地控制执行流程,并且print/log任何你需要的......例如像这样的东西:
def make_comperations(a, b, c = None): # note c is optional
assert c is not None, 'Error: c is None'
assert b > a, 'Error: a is not greater than b'
assert c > a, 'Error: c is not greater than a'
try:
make_comperations(1, 2, 3) # ok
make_comperations(1, 2) # error
make_comperations(1, 2, 0) # error, but won't be executed since the line above will throw an exception
print("All good!")
except AssertionError as err:
if str(err) == 'Error: c is None':
print("Log: c is not given!")
if str(err) == 'Error: a is not greater than b':
print("Log: b > a error!")
elif str(err) == 'Error: c is not greater than a':
print("Log: c > a error!")
输出:
Log: c is not given!