Python是否会懒惰地评估条件? [英] Does Python evaluate if's conditions lazily?
本文介绍了Python是否会懒惰地评估条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如果我有以下声明:
For example, if I have the following statement:
if( foo1 or foo2)
...
...
如果foo1为true,Python会检查foo2的条件吗?
if foo1 is true, will python check the condition of foo2?
推荐答案
是的,Python懒惰地评估布尔条件.
Yes, Python evaluates boolean conditions lazily.
文档说,
表达式x和y首先计算x;如果x为假,则其值为 回来;否则,将评估y并得出结果值为 返回.
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
表达式x或y首先计算x;如果x为true,则其值为 回来;否则,将评估y并得出结果值为 返回.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
这篇关于Python是否会懒惰地评估条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文