【发布时间】:2021-12-25 12:06:30
【问题描述】:
编辑:它的回答,我不明白三元运算符是什么。 对于未来有类似问题的人:https://book.pythontips.com/en/latest/ternary_operators.html
我正在研究python中的'assert'语句,但我不明白下面的句子。
assert .. if ... else ... and ...
所以如果我理解正确,如果你想测试一个“if else”语句,你必须使用上面的方法。您必须在“if”语句之后插入以下内容:assert (P1 if E else P2) and E
例如
assert (y == builtins.max(x, y) if x < y else x == builtins.max(x, y)) and x < y
如果了解assert y == builtins.max(x,y)
它只是检查条件是否为真,当它不为真时,它返回一个断言错误。但是,在以下情况下:
assert (y == builtins.max(x, y) if x < y else x == builtins.max(x, y)) and x < y
我不知道发生了什么。它显然也总是返回 true。但我什至无法猜测到底发生了什么。我查看了 assert 语句的作用,它唯一的作用是:assert <condition>,<error message> 所以检查条件并可能返回错误消息。但是我不明白... if ... else ... and ... 是一个条件。我了解and,但您如何准确地解释这种情况下的if else 部分?
我真的不明白我不明白的东西。这可能非常微不足道。希望有人可以帮助我。抱歉我的拼写错误。
编辑:它的回答,我不明白三元运算符是什么。 对于未来有类似问题的人:https://book.pythontips.com/en/latest/ternary_operators.html
【问题讨论】: