调用assert
语句后如何运行多个命令?例如,这就是我想要做的事情(不使用assert):
x = False
if x != True:
my_func()
raise ValueError("My statement")
这正是我想要的,但在这种情况下使用assert
似乎更Pythonic。在调用assert
之后,我无法弄清楚如何做多件事。这是我正在尝试做的事情(但语法不正确):
x = False
assert x == True, my_func() "My statement"
你可以做到
assert x == True, [my_func(), "My statement"][1]
DEMO
def my_func():
print("my function")
x = False
assert x == True, [my_func(), "My statement"][1]
OUTPUT
my function
Traceback (most recent call last):
File "C:/Users/abdul.niyas/AppData/Local/Programs/Python/Python36-32/a.py", line 5, in <module>
assert x == True, [my_func(), "My statement"][1]
AssertionError: My statement