相关疑难解决方法(0)

python断言有和没有括号

这里有四个简单的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")
Run Code Online (Sandbox Code Playgroud)

请注意,最后一个不会引发错误.调用带或不带括号的断言会导致这种行为有什么区别?我的做法是使用括号,但上面的建议我不应该这样做.

python assert parentheses

91
推荐指数
4
解决办法
3万
查看次数

标签 统计

assert ×1

parentheses ×1

python ×1