这里有四个简单的 assert 调用:
>>> assert 1==2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError
>>> assert 1==2, "hi"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: hi
>>> assert(1==2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError
>>> assert(1==2, "hi")
请注意,最后一个不会引发错误。带或不带括号调用断言导致此行为有什么区别?我的做法是使用括号,但以上建议我不应该。