Python的短路值是多少?

What's the value of short-circuit of Python?

我正在学习 Python 中名为 Data Structures & Algorithms 的书。

在介绍逻辑运算符的第 12 页上写道:

The and and or operators short-circuit, (I thinks it should add is called), in that they do not evaluate the second operand if the result can be determined based on the value of the first operand.

This feature is useful when constructing Boolean expressions in which we first test that a certain condition holds (such as a reference not being None), and then test a condition that could have otherwise generated an error condition had the prior test not succeeded.

我对这段话有一些疑问:

  • 我看不懂第二段的确切意思。在C语言中,我们可以使用&&(逻辑and)作为如下表达式: (i != 0) && (j / i > 0) 防止被零除的错误。那么,我可以将 Python 中的表达式 (i != 0) and ( j / i > 0) 用作 C 来获得相同的效果吗?我对这段话的理解对吗?

  • or作为short-circuit有什么用来构造第二段中所说的布尔表达式?

  • 最后一题是第二段had prior test not succeed的语法问题。我这应该是“一个错误条件,可以使先前的测试不成功”,对吗?

I can't understand exact meaning of the second paragraph. In C, we can use the &&(logical and) as the following expression: (i != 0) && (j / i > 0) to prevent the error of a division by zero. So then, could I use the expression (i != 0) and ( j / i > 0) in Python as C to get the same effect? Is my understanding to the passage right?

What's the usage of or as a short-circuit to constructing Boolean expressions as said in the second paragraph ?

举个例子:

if (y is None) or (x not in y):

其中 y 是事物列表或 None ,在这种情况下我们希望将其视为一个空列表,但 x not in None 将是一个错误。

或者:

(i  == 0) or (j / i > 0)

The final question is about the grammar of had the prior test not succeeded in the second paragraph. I this it should be "an error condition that can had the prior test not succeeded", am I right?

不,您的措辞语法不正确。

如果你有X and/or YX是第一个或'prior'测试,Y是第二个,有可能X是假的并尝试评估 Y 将导致错误。

>>> if 1 or 0:
...     print "I am learning"
... 
I am learning
>>> if 1 and 0:
...     print "I am learning"
... 
>>> if 0:
...     print "I am learning"
... 
>>> if 1:
...     print "I am learning"
... 
I am learning
>>> if None and 1:
...     print "be crazy"
... 
>>> if None or 1:
...     print "be crazy"
be crazy

很像 C 实现。

最后一行是正确的,如果第一个条件返回错误不成功,第二个条件检查错误

我记得,短路指的是编译器优化。如您所知,在 AND 条件中,如果第一个条件为假,则不会计算第二个表达式。毕竟,有什么意义呢?整个表达式不能为真,因为第一个条件已经为假。类似地,对于 OR 逻辑,只要一个条件为真,整个 OR 表达式就为真。这归结为节省运行时间。表达式可以不求值,因此不会燃烧任何 CPU 周期。

顺便说一下,我在 bash 编程中一直使用 OR 短路。例如,如果前面的条件为假,则以下表达式对 运行 函数很有用:

[ $# -ge 2 ] || errexit "You have not supplied enough parameters"

在上面的例子中,errexit 只有在命令行没有 2 个或更多参数时才会被调用。当然,在我的示例中,我不关心性能。相反,我正在使用 ||短路逻辑作为语法糖。

归根结底就是:在性能很重要的紧密循环中,您可以确定不会对表达式进行不必要的求值。但是在您为 && 描述的避免被零除的示例中,您可以使用嵌套的 IF 语句轻松编写它。同样,这是一种风格选择,而不是性能考虑。

综上所述,让我一次回答一个问题:

I can't understand exact meaning of the second paragraph. In C, we can use the &&(logical and) as the following expression: (i != 0) && (j / i > 0) to prevent the error of a division by zero. So then, could I use the expression (i != 0) and ( j / i > 0) in Python as C to get the same effect? Is my understanding to the passage right?

你是对的。

What's the usage of or as a short-circuit to constructing Boolean expressions as said in the second paragraph ?

正如我在上面详细解释的那样:性能和语法糖(即更少的输入和更短的表达式;习语)。

The final question is about the grammar of had the prior test not succeeded in the second paragraph. I this it should be "an error condition that can had the prior test not succeeded", am I right?

我同意你的看法,声明的措辞可以更好。但是当你试图表达它时,你会发现这是一件很难做的事情(事实上,你的建议是无效的)。基本上,您避免被零除错误的示例是作者试图表达的完美示例。这是我试图解释的:短路逻辑可用于检查表达式的前提条件,如果不满足这些条件可能会产生错误。

使用短路还有另一个原因——语法正确性。从列表中获取项目进行比较而不在空列表条件下抛出错误是使用短路的重要原因,如下所示。还有其他处理空列表的方法,但这样的代码会更复杂(代码行更多)。

在此示例中,应用程序要求在 'bob' 是最后一个列表项时严格执行不同的操作。 Bob 不能排在我们名单的最后! (因为鲍勃是最好的,仅此而已。)

下面显示的三行代码中包含带 "or" 的条件表达式不会引发错误。

>>> a = []
>>> a[-1] == 'bob'  # is the last one 'bob'?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> a[-1:][0] == 'bob'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> len(a)
0
>>> len(a)==0 or a[-1] != "bob"
True
>>> a.append('bob')
>>> len(a)==0 or a[-1] != "bob"  # the last one is bob. Act different.
False
>>> a.append('news')
>>> len(a)==0 or a[-1] != "bob"
True