“断言”带或不带括号的语句
以下是断言的四个简单调用:
>>> 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")
请注意,最后一个不会引发错误。调用带括号或不带括号的断言导致此行为有什么区别?我的做法是使用括号,但上面的内容表明我不应该。
Here are four simple invocations of 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")
Note that the last one does not raise an error. What is the difference between calling assert with or without parenthesis that causes this behavior? My practice is to use parenthesis, but the above suggests that I should not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您通过完整的解释器而不是通过 IDLE 运行最后一个
assert
,则会向您发出警告(SyntaxWarning:断言始终为真,也许删除括号?
)。因为assert
是关键字而不是函数,所以您实际上传入一个元组作为第一个参数,并省略第二个参数。回想一下,非空元组的计算结果为 True,并且由于断言消息是可选的,因此当您编写
assert(1==) 时,您实际上已经调用了
assert True
2、“嗨”)。The last
assert
would have given you a warning (SyntaxWarning: assertion is always true, perhaps remove parentheses?
) if you ran it through a full interpreter, not through IDLE. Becauseassert
is a keyword and not a function, you are actually passing in a tuple as the first argument and leaving off the second argument.Recall that non-empty tuples evaluate to
True
, and since the assertion message is optional, you've essentially calledassert True
when you wroteassert(1==2, "hi")
.如果您因为想要多行断言而将括号放在那里,则另一种方法是在行末尾放置反斜杠,如下所示:
打印:
为什么此 python
assert
必须是与其他一切不同:我认为 pythonic 意识形态是程序应该自我纠正,而不必担心打开断言的特殊标志。关闭断言的诱惑太大,因此它已被弃用。
我和你一样烦恼,python
assert
相对于所有其他 python 编程结构具有独特的语法,并且该语法再次从 python2 更改为 python3,并再次从 python 3.4 更改为 3.6。使断言语句不向后兼容任何版本到任何其他版本。
assert
是三等公民,这对我们来说是一个轻击,它将在 python4 中被完全删除,当然在 Python 8.1 中也会再次被删除。If you put the parenthesis in there because you wanted a multi-line assert, then an alternative is to put a backslash at the end of the line like this:
Prints:
Why does this python
assert
have to be different from everything else:I think the pythonic ideology is that a program should self-correct without having to worry about the special flag to turn on asserts. The temptation to turn off asserts is too great, and thus it's being deprecated.
I share your annoyance that the python
assert
has unique syntax relative to all other python programming constructs, and this syntax has yet again changed from python2 to python3 and again changed from python 3.4 to 3.6.Making assert statements not backward compatible from any version to any other version.
It's a tap on the shoulder that
assert
is a 3rd class citizen, it will be totally removed in python4, and certainly again in Python 8.1.您可以在没有
\
的情况下中断断言语句,如下所示:或者如果您有更长的消息:
You can break assert statement without
\
like this:Or if you have even longer message:
assert 1==2, "hi"
被解析为assert 1==2, "hi"
,其中“hi”作为关键字的第二个参数。这就是为什么它正确地给出了错误。assert(1==2)
被解析为assert (1==2)
,与assert 1==2
相同,因为括号围绕单个项目不要创建元组,除非有尾随逗号,例如(1==2,)
。assert(1==2, "hi")
被解析为assert (1==2, "hi")
,它不会给出错误,因为非-empty tuple(False, "hi")
不是 false 值,并且没有向关键字提供第二个参数。您不应该使用括号,因为
assert
不是 Python 中的函数 - 它是一个关键字。assert 1==2, "hi"
is parsed asassert 1==2, "hi"
with "hi" as the second parameter for the keyword. Hence why it properly gives an error.assert(1==2)
is parsed asassert (1==2)
which is identical toassert 1==2
, because parens around a single item don't create a tuple unless there's a trailing comma e.g.(1==2,)
.assert(1==2, "hi")
is parsed asassert (1==2, "hi")
, which doesn't give an error because a non-empty tuple(False, "hi")
isn't a false value, and there is no second parameter supplied to the keyword.You shouldn't use parentheses because
assert
is not a function in Python - it's a keyword.以下引用自 python 文档
简单的形式,断言表达式,相当于
<代码>
如果__调试__:
如果不是表达式:引发断言错误
扩展形式,断言表达式1,表达式2,相当于
<代码>
如果__调试__:
如果不是表达式1:引发断言错误(表达式2)
因此,当您在这里使用括号时,您使用的是简单形式,并且表达式被计算为一个元组,在转换为 bool 时始终为 True
Following is cited from the python doc
The simple form, assert expression, is equivalent to
if __debug__:
if not expression: raise AssertionError
The extended form, assert expression1, expression2, is equivalent to
if __debug__:
if not expression1: raise AssertionError(expression2)
So when you're using parenthesis here, you're using the simple form, and the expression is evaluated as a tuple, which is always True when being casted to bool
断言语句或不带括号,如下所示:
并且,其他语句,例如if,while, for 和 del 带或不带括号如下图也一样:
另外,基本上,大多数到目前为止我所看到的示例Python代码没有使用括号来表示
assert
、if
、while
、for
和del
语句,所以我不喜欢对它们使用括号。assert statement with or without parentheses as shown below are the same:
And, other statements such as if, while, for and del with or without parentheses as shown below are also the same:
In addition, basically, most example python code which I've seen so far doesn't use parentheses for
assert
,if
,while
,for
anddel
statements so I prefer not using parentheses for them.