Python短路的价值是什么? [英] What's the value of short-circuit of Python?

查看:62
本文介绍了Python短路的价值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习名为 Data Structures& Python中的算法.

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

On Page 12 that introduce the Logical Operators, it writes:

运算符短路,(我认为应该添加被称为),如果可以根据第一个操作数的值确定结果,则它们不会评估第二个操作数.

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.

此功能在构造布尔表达式时非常有用,在布尔表达式中,我们首先测试某个条件是否成立(例如,引用不为None),然后测试可能会生成错误条件的条件,而测试失败.

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.

我对这段话有一些疑问:

I have some questions about this passage:

  • 我不明白第二段的确切含义.在C语言中,我们可以使用&& (逻辑)作为以下表达式: (i != 0) && (j / i > 0)防止被零除的错误.那么,是否可以在Python中使用表达式(i != 0) and ( j / i > 0)作为C来获得相同的效果?我对这段经文的理解正确吗?

  • 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?

推荐答案

我记得,短路是指编译器优化.如您所知,在AND条件中,如果第一个条件为false,则不对第二个表达式求值.毕竟有什么意义?整体表达式不能为true,因为第一个条件已经为false.同样,使用OR逻辑,只要一个条件为真,整个OR表达式就为真.这归结为节省运行时间.表达式可以不计算,因此不会消耗任何CPU周期.

As I recall, short-circuit is referring to a compiler optimization. As you are already aware, in an AND conditional, the second expression is not evaluated if the first condition is false. After all, what is the point? The overall expression CANNOT be true since the first condition is already false. Similarly, with the OR logic, as soon as a single condition is true, the entire OR expression is true. This boils down to runtime savings. Expressions can be left unevaluated and thus not burn any CPU cycles.

偶然地,我在bash编程中一直使用OR短路.例如,如果前面的条件为false,则以下表达式对于运行函数很有用:

Incidentally, I use OR short-circuits all the time in bash programming. For example, the following expression is useful for running a function if the preceeding condition is false:

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

在上面的示例中,仅当命令行中没有2个或更多参数时,才会调用errexit.当然,在我的示例中,我并不关心性能.而是,我在使用||.短路逻辑作为语法糖.

In the above example, errexit will be called only if the command line did not have 2 arguments or more. Of course, in my example, I don't care about performance. Rather, I'm using the || short circuit logic as syntactic sugar.

这就是问题所在:在一个紧密循环中,在性能很重要的地方,您可以确定不会不必要地对表达式求值.但是在您为&&描述的示例中为避免被零除,您可以使用嵌套的IF语句轻松地编写该代码.再次,它是一种样式选择,而不是性能考虑.

And that's what it boils down to: In a tight loop, where performance matters, you can be somewhat certain that expressions will not be evaluated unnecessarily. But in the example that you described for && to avoid divide by zero, you could have just as easily written that with a nested IF statement. Again, it's a style choice more often than a performance consideration.

所有这些,让我一次回答您的问题:

All that said, let me answer your questions one at a time:

我不明白第二段的确切含义.在C语言中,我们可以将&&(逻辑和)用作以下表达式:(i!= 0)&& (j/i> 0)以防止被零除的错误.所以那么 我在Python中使用表达式(i!= 0)和(j/i> 0)作为C获取 效果一样吗?我对段落的理解正确吗?

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 ?

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

As I explained in detail above: performance and syntax sugar ( that is, less typing and shorter expressions; idioms ).

最后一个问题是关于没有先验考试的语法 在第二段中成功.我这应该是一个错误 可以进行先前测试的条件不成功",对吗?

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?

我同意您的说法,措词可能更好.但是当您尝试表达它时,您会发现这很难做(实际上,您的建议无效).基本上,您的避免零错误除法的示例是作者尝试说明的完美示例.这是我的尝试:短路逻辑对于检查表达式的前置条件很有用,如果不满足这些条件,这些前置条件可能会产生错误.

I agree with you that the statement could be worded better. But when you try to express it, you'll see that it is a difficult thing to do ( in fact, your suggestion is not valid ). Basically, your example of avoiding divide by zero error is a perfect example of what the author is trying to say. Here's my attempt to paraphrase: Short-circuit logic is useful to check pre-conditions of expressions that may generate errors if those conditions are not met.

这篇关于Python短路的价值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆