“断言”带或不带括号的语句

发布于 2024-09-06 21:26:27 字数 499 浏览 19 评论 0原文

以下是断言的四个简单调用:

>>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

羁〃客ぐ 2024-09-13 21:26:27

如果您通过完整的解释器而不是通过 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. Because assert 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 called assert True when you wrote assert(1==2, "hi").

转瞬即逝 2024-09-13 21:26:27

如果您因为想要多行断言而将括号放在那里,则另一种方法是在行末尾放置反斜杠,如下所示:

foo = 7
assert foo == 8, \
    "derp should be 8, it is " + str(foo)

打印:

AssertionError: "derp should be 8, it is 7

为什么此 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:

foo = 7
assert foo == 8, \
    "derp should be 8, it is " + str(foo)

Prints:

AssertionError: "derp should be 8, it is 7

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.

最近可好 2024-09-13 21:26:27

您可以在没有 \ 的情况下中断断言语句,如下所示:

foo = 7
assert foo == 8, (
    'derp should be 8, it is ' + str(foo))

或者如果您有更长的消息:

foo = 7
assert foo == 8, (
    'Lorem Ipsum is simply dummy text of the printing and typesetting '
    'industry. Lorem Ipsum has been the industry\'s standard dummy text '
    'ever since the 1500s'
)

You can break assert statement without \ like this:

foo = 7
assert foo == 8, (
    'derp should be 8, it is ' + str(foo))

Or if you have even longer message:

foo = 7
assert foo == 8, (
    'Lorem Ipsum is simply dummy text of the printing and typesetting '
    'industry. Lorem Ipsum has been the industry\'s standard dummy text '
    'ever since the 1500s'
)
睡美人的小仙女 2024-09-13 21:26:27

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 as assert 1==2, "hi" with "hi" as the second parameter for the keyword. Hence why it properly gives an error.

assert(1==2) is parsed as assert (1==2) which is identical to assert 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 as assert (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.

别在捏我脸啦 2024-09-13 21:26:27

以下引用自 python 文档

断言语句是将调试断言插入程序的便捷方法:

assert_stmt ::= "断言" 表达式 ["," 表达式]

简单的形式,断言表达式,相当于
<代码>
如果__调试__:
如果不是表达式:引发断言错误

扩展形式,断言表达式1,表达式2,相当于
<代码>
如果__调试__:
如果不是表达式1:引发断言错误(表达式2)

因此,当您在这里使用括号时,您使用的是简单形式,并且表达式被计算为一个元组,在转换为 bool 时始终为 True

Following is cited from the python doc

Assert statements are a convenient way to insert debugging assertions into a program:

assert_stmt ::= "assert" expression ["," expression]

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

怪我入戏太深 2024-09-13 21:26:27

断言语句或不带括号,如下所示:

assert (x == 3)
assert x == 3

并且,其他语句,例如ifwhile, fordel 带或不带括号如下图也一样:


if (x == "Hello"):
if x == "Hello":

while (x == 3):
while x == 3:

for (x) in (fruits):
for x in fruits:


del (x)
del x

另外,基本上,大多数到目前为止我所看到的示例Python代码没有使用括号来表示assertifwhilefordel 语句,所以我不喜欢对它们使用括号

assert statement with or without parentheses as shown below are the same:

assert (x == 3)
assert x == 3

And, other statements such as if, while, for and del with or without parentheses as shown below are also the same:


if (x == "Hello"):
if x == "Hello":

while (x == 3):
while x == 3:

for (x) in (fruits):
for x in fruits:


del (x)
del x

In addition, basically, most example python code which I've seen so far doesn't use parentheses for assert, if, while, for and del statements so I prefer not using parentheses for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文