Python支持短路吗?

问题描述 投票:282回答:3

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

python boolean-logic short-circuiting
3个回答
275
投票

是的,andor运营商都发生短路 - 请参阅the docs


174
投票

Short-circuiting behavior in operator and, or:

让我们首先定义一个有用的函数来确定是否执行某些操作。一个接受参数的简单函数,打印一条消息并返回输入,不变。

>>> def fun(i):
...     print "executed"
...     return i
... 

在下面的例子中,可以观察到Python's short-circuiting behaviorand算子的or

>>> fun(1)
executed
1
>>> 1 or fun(1)    # due to short-circuiting  "executed" not printed
1
>>> 1 and fun(1)   # fun(1) called and "executed" printed 
executed
1
>>> 0 and fun(1)   # due to short-circuiting  "executed" not printed 
0

注意:解释器认为以下值表示错误:

        False    None    0    ""    ()    []     {}

Short-circuiting behavior in function: any(), all():

Python的any()all()函数也支持短路。如文档中所示;他们按顺序评估序列的每个元素,直到找到允许在评估中提前退出的结果。请考虑以下示例来理解两者。

函数any()检查是否有任何元素为True。一旦遇到True,它就会停止执行并返回True。

>>> any(fun(i) for i in [1, 2, 3, 4])   # bool(1) = True
executed
True
>>> any(fun(i) for i in [0, 2, 3, 4])   
executed                               # bool(0) = False
executed                               # bool(2) = True
True
>>> any(fun(i) for i in [0, 0, 3, 4])
executed
executed
executed
True

函数all()检查所有元素是否为True并在遇到False时立即停止执行:

>>> all(fun(i) for i in [0, 0, 3, 4])
executed
False
>>> all(fun(i) for i in [1, 0, 3, 4])
executed
executed
False

Short-circuiting behavior in Chained Comparison:

另外,在Python中

Comparisons can be chained arbitrarily;例如,x < y <= z相当于x < y and y <= z,除了y仅被评估一次(但在两种情况下,当发现z为假时,x < y根本没有被评估)。

>>> 5 > 6 > fun(3)    # same as:  5 > 6 and 6 > fun(3)
False                 # 5 > 6 is False so fun() not called and "executed" NOT printed
>>> 5 < 6 > fun(3)    # 5 < 6 is True 
executed              # fun(3) called and "executed" printed
True
>>> 4 <= 6 > fun(7)   # 4 <= 6 is True  
executed              # fun(3) called and "executed" printed
False
>>> 5 < fun(6) < 3    # only prints "executed" once
executed
False
>>> 5 < fun(6) and fun(6) < 3 # prints "executed" twice, because the second part executes it again
executed
executed
False

编辑: 还有一点需要注意: - Python中的逻辑and, or运算符返回操作数的值而不是布尔值(TrueFalse)。例如:

操作x and y给出结果if x is false, then x, else y

与其他语言不同, &&,C中的||运算符返回0或1。

例子:

>>> 3 and 5    # Second operand evaluated and returned 
5                   
>>> 3  and ()
()
>>> () and 5   # Second operand NOT evaluated as first operand () is  false
()             # so first operand returned 

类似地,or运算符返回最左边的值bool(value) == True其他最正确的值(根据短路行为),示例:

>>> 2 or 5    # left most operand bool(2) == True
2    
>>> 0 or 5    # bool(0) == False and bool(5) == True
5
>>> 0 or ()
()

那么,这有用吗? Practical Python提供的一个示例用法由Magnus Lie Hetland提供: 假设用户应该输入他或她的名字,但可以选择不输入任何内容,在这种情况下你想使用默认值'<unknown>'。您可以使用if语句,但您也可以非常简洁地声明:

In [171]: name = raw_input('Enter Name: ') or '<Unkown>'
Enter Name: 

In [172]: name
Out[172]: '<Unkown>'

换句话说,如果raw_input的返回值为true(不是空字符串),则将其分配给name(没有任何更改);否则,默认的'<unknown>'被分配给name


46
投票

是。在python解释器中尝试以下操作:

>>>False and 3/0
False
>>>True and 3/0
ZeroDivisionError: integer division or modulo by zero

要么

>>>True or 3/0
True
>>>False or 3/0
ZeroDivisionError: integer division or modulo by zero
© www.soinside.com 2019 - 2024. All rights reserved.