(没有)快速布尔评估? [英] (no) fast boolean evaluation ?

查看:43
本文介绍了(没有)快速布尔评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我发现Python中的布尔评估是快速

(一旦条件合适,剩下的表达式被忽略了。


是这个标准行为还是有编译器开关打开/关闭它?


谢谢,

Stef Mientki

hello,

I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

Is this standard behavior or is there a compiler switch to turn it on/off ?

thanks,
Stef Mientki

推荐答案

En Thu,2007年8月2日18:47:49 -0300,Stef Mientki

< S. ************** @mailbox.kun.nlescribió:
En Thu, 02 Aug 2007 18:47:49 -0300, Stef Mientki
<S.**************@mailbox.kun.nlescribió:

我发现布尔评估在Python中完成快速

(一旦条件合适,表达式的其余部分将被忽略)。


这是标准行为或者是否有一个编译器开关来打开/关闭

I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

Is this standard behavior or is there a compiler switch to turn it
on/off ?



确切的行为在语言参考中定义

< http://docs.python.org/ref/Booleans.html> ;


"表达式x和y首先评估x;如果x为假,则其值为

返回;否则,计算y并返回结果值。

表达式x或y首先计算x;如果x为真,则其值为

返回;否则,y被评估并返回结果值。

注意,既不会也不会限制它们返回的值和类型

False和True,而是返回最后评估的论点。这是

有时是有用的,例如,如果s是一个字符串,如果它是空的,应该用
默认值替换,表达式为s或''foo''得到所需的

值。


教程第5.7节用口语说出同样的事情。


-

Gabriel Genellina

The exact behavior is defined in the Language Reference
<http://docs.python.org/ref/Booleans.html>

"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.
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.
Note that neither and nor or restrict the value and type they return to
False and True, but rather return the last evaluated argument. This is
sometimes useful, e.g., if s is a string that should be replaced by a
default value if it is empty, the expression s or ''foo'' yields the desired
value."

Tutorial section 5.7 say the same thing in a colloquial way.

--
Gabriel Genellina


Stef Mientki写道:
Stef Mientki wrote:

你好,


我发现Python中的布尔评估是快速

(一旦条件合适,其余的表达式被忽略了。


是这个标准行为还是有编译器开关打开/关闭它?


谢谢,

Stef Mientki
hello,

I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

Is this standard behavior or is there a compiler switch to turn it on/off ?

thanks,
Stef Mientki



它被称为短路评估,据我所知它是标准的

在大多数语言中。只有当条件求值为

为真时才会出现这种情况,而且仍需要评估的其他运算符只有

''或'或者条件求值为False且所有其他运营商

是''和''。原因是那些其他运营商永远不会改变

结果:对于任何数量的False',真或者'仍然是真的和

假和''任何数量的特鲁斯仍然是假的。


我的问题是你为什么不*想要这个?


伊恩

It''s called short circuit evaluation and as far as I know it''s standard
in most all languages. This only occurs if a conditional evaluates to
True and the only other operators that still need to be evaluated are
''or''s or the condition evaluates to False and all the other operators
are ''and''s. The reason is those other operators will never change the
outcome: True or''d with any number of False''s will still be True and
False and''ed to any number of Trues will still be False.

My question would be why would you *not* want this?

Ian


2007年8月2日,Stef Mientki< S. ************** @ mailbox.kun。 nlwrote:
On 8/2/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:

hello,


我发现Python中的布尔评估是快速完成的

(一旦条件合适,表达式的其余部分就会被忽略)。


这是标准行为还是有编译器开关来打开/关闭它?
hello,

I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

Is this standard behavior or is there a compiler switch to turn it on/off ?



这是我遇到的每种语言的标准行为。如果您正在评估和/或副作用,并且您需要双方

效果,您可以轻松编写实现此功能的函数

行为,例如


def a():

打印''foo''

def b():

打印''栏''

def my_and(lh,rh):

返回a和b


然后my_and(a(),b())将同时评估a和b并打印foo

和bar,即使a()为False。


-

Evan Klitzke< ev ** @ yelp.com>

This is standard behavior in every language I''ve ever encountered. If
you are evaluating an and/or with side effects and you need both side
effects to occur, you can trivially write functions implementing this
behavior, e.g.

def a():
print ''foo''
def b():
print ''bar''
def my_and(lh, rh):
return a and b

Then my_and(a(), b()) will evaluate both a and b and print both foo
and bar even though a() is False.

--
Evan Klitzke <ev**@yelp.com>


这篇关于(没有)快速布尔评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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