Python支持短路吗?

Python支持短路吗?

Python支持布尔表达式中的短路吗?



LEATH
浏览 892回答 3
3回答

鸿蒙传说

算子的短路行为and, or:让我们首先定义一个有用的函数,以确定是否执行了某项操作。一个简单的函数,它接受一个参数,打印一条消息并返回输入,没有变化。>>> def fun(i):...&nbsp; &nbsp; &nbsp;print "executed"...&nbsp; &nbsp; &nbsp;return i...&nbsp;我们可以观察到Python的短路行为的and, or以下示例中的运算符:>>> fun(1)executed1>>> 1 or fun(1)&nbsp; &nbsp; # due to short-circuiting&nbsp; "executed" not printed1>>> 1 and fun(1)&nbsp; &nbsp;# fun(1) called and "executed" printed&nbsp;executed1>>> 0 and fun(1)&nbsp; &nbsp;# due to short-circuiting&nbsp; "executed" not printed&nbsp;0注:解释器认为以下值表示false:&nbsp; &nbsp; &nbsp; &nbsp; False&nbsp; &nbsp; None&nbsp; &nbsp; 0&nbsp; &nbsp; ""&nbsp; &nbsp; ()&nbsp; &nbsp; []&nbsp; &nbsp; &nbsp;{}功能短路行为:any(), all():Python的any()和all()功能还支持短路.如docs所示,它们按顺序计算序列的每个元素,直到找到允许在计算中早期退出的结果。考虑下面的例子来理解这两种情况。功能any()检查是否有任何元素为True。一旦遇到True,它就停止执行,并返回True。>>> any(fun(i) for i in [1, 2, 3, 4])&nbsp; &nbsp;# bool(1) = TrueexecutedTrue>>> any(fun(i) for i in [0, 2, 3, 4])&nbsp; &nbsp;executed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# bool(0) = Falseexecuted&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# bool(2) = TrueTrue>>> any(fun(i) for i in [0, 0, 3, 4])executedexecutedexecutedTrue功能all()检查所有元素是否为true,并在遇到false时立即停止执行:>>> all(fun(i) for i in [0, 0, 3, 4])executedFalse>>> all(fun(i) for i in [1, 0, 3, 4])executedexecutedFalse连锁比较中的短路行为:此外,在Python中比较可以被任意链接。;例如,x < y <= z等于x < y and y <= z,除了y只计算一次(但在这两种情况下)z时,则根本不进行评估。x < y被发现是假的)。>>> 5 > 6 > fun(3)&nbsp; &nbsp; # same as:&nbsp; 5 > 6 and 6 > fun(3)False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 5 > 6 is False so fun() not called and "executed" NOT printed>>> 5 < 6 > fun(3)&nbsp; &nbsp; # 5 < 6 is True&nbsp;executed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # fun(3) called and "executed" printedTrue>>> 4 <= 6 > fun(7)&nbsp; &nbsp;# 4 <= 6 is True&nbsp;&nbsp;executed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # fun(3) called and "executed" printedFalse>>> 5 < fun(6) < 3&nbsp; &nbsp; # only prints "executed" onceexecutedFalse>>> 5 < fun(6) and fun(6) < 3 # prints "executed" twice, because the second part executes it againexecutedexecutedFalse编辑:还有一点值得注意:-逻辑and, orPython中的运算符返回操作数的价值而不是布尔(True或False)。例如:操作x and y给出结果if x is false, then x, else y与其他语言不同的是。&&, ||C中返回0或1的运算符。例子:>>> 3 and 5&nbsp; &nbsp; # Second operand evaluated and returned&nbsp;5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;>>> 3&nbsp; and ()()>>> () and 5&nbsp; &nbsp;# Second operand NOT evaluated as first operand () is&nbsp; false()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# so first operand returned&nbsp;类似or运算符返回最左边的值,其中bool(value) == True其他最正确的错误值(根据短路行为),示例:>>> 2 or 5&nbsp; &nbsp; # left most operand bool(2) == True2&nbsp; &nbsp;&nbsp;>>> 0 or 5&nbsp; &nbsp; # bool(0) == False and bool(5) == True5>>> 0 or ()()那么,这有什么用呢?中给出的一个示例使用实用Python马格努斯·利·赫特兰:假设用户应该输入他或她的名字,但是可以选择不输入任何内容,在这种情况下,您需要使用默认值'<unknown>'..您可以使用if语句,但也可以非常简洁地说明:In [171]: name = raw_input('Enter Name: ') or '<Unkown>'Enter Name:&nbsp;In [172]: nameOut[172]: '<Unkown>'换句话说,如果RAW_INPUT的返回值为true(而不是空字符串),则将其赋值为name(不变);否则,默认'<unknown>'分配给name.

呼啦一阵风

是。在python解释器中尝试以下操作:和>>>False&nbsp;and&nbsp;3/0False>>>True&nbsp;and&nbsp;3/0ZeroDivisionError:&nbsp;integer&nbsp;division&nbsp;or&nbsp;modulo&nbsp;by&nbsp;zero或>>>True&nbsp;or&nbsp;3/0True>>>False&nbsp;or&nbsp;3/0ZeroDivisionError:&nbsp;integer&nbsp;division&nbsp;or&nbsp;modulo&nbsp;by&nbsp;zero
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python