我喜欢在断言失败时看到一些有意义的描述。你知道吗
以下是我的代码及其执行:
>cat /tmp/1.py
a="aaa" + "bbb"
print(a)
assert ("hello" + a) and 0
>python /tmp/1.py
aaabbb
Traceback (most recent call last):
File "/tmp/1.py", line 3, in <module>
assert ("hello" + a) and 0
AssertionError
我使用的是python3.7。你知道吗
你知道为什么"hello" + a
不首先作为字符串连接进行计算吗?我怎样才能做到?你知道吗
[更新]感谢您的回复,以下是我想要的:
>cat /tmp/1.py
a="aaa" + "bbb"
print(a)
assert 0, "hello" + a
您可以在
assert
语句后面加上逗号。你知道吗例如:
结果将是:
According to the docs,失败消息后跟逗号:
这相当于:
正如注释中所指出的,
assert
不是函数调用。不要加括号,否则可能会有奇怪的结果。assert(condition, message)
将被解释为一个元组,用作没有消息的条件,并且永远不会失败。你知道吗相关问题 更多 >
编程相关推荐