【发布时间】:2020-06-27 09:27:30
【问题描述】:
Python 是否支持布尔表达式中的短路?
【问题讨论】:
标签: python boolean-logic short-circuiting
Python 是否支持布尔表达式中的短路?
【问题讨论】:
标签: python boolean-logic short-circuiting
and、or中的短路行为:让我们首先定义一个有用的函数来确定是否执行了某些操作。一个简单的函数,它接受一个参数、打印一条消息并返回未更改的输入。
>>> def fun(i):
... print "executed"
... return i
...
可以观察以下示例中的and、or 运算符中的Python's short-circuiting behavior:
>>> 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 "" () [] {}
any(),all():Python 的 any() 和 all() 函数也支持短路。如文档所示;他们按顺序评估序列的每个元素,直到找到允许在评估中提前退出的结果。请考虑以下示例以了解两者。
函数any() 检查是否有任何元素为真。一旦遇到 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()检查所有元素是否为真,一旦遇到假就停止执行:
>>> 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
另外,在 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 运算符返回操作数的值,而不是布尔值(True 或False)。例如:
操作
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 '<Unknown>'
Enter Name:
In [172]: name
Out[172]: '<Unknown>'
换句话说,如果raw_input 的返回值为真(不是空字符串),则将其赋值给name(没有任何变化);否则,默认的'<Unknown>' 分配给name。
【讨论】:
0 的数值类型都是虚假的(所以不仅仅是0,它是0.0、0j、decimal.Decimal(0)、fractions.Fraction(0) 等),所有长度为@ 的集合也是如此987654375@ (所以除了你列出的,b'' [Py3]、u'' [Py2] 和 set()/frozenset() 都是评估为假的内置插件),但用户定义/第三 -派对类型可以定义自己的(直接使用__bool__ [Py3]/__nonzero__ [Py2],或通过定义__len__间接使用)。
是的,Python 确实支持布尔运算符的短路评估、最小评估或麦卡锡评估。它用于减少计算布尔表达式输出的评估次数。示例 -
基本函数
def a(x):
print('a')
return x
def b(x):
print('b')
return x
与
if(a(True) and b(True)):
print(1,end='\n\n')
if(a(False) and b(True)):
print(2,end='\n\n')
与输出
a
b
1
a
或
if(a(True) or b(False)):
print(3,end='\n\n')
if(a(False) or b(True)):
print(4,end='\n\n')
OR-输出
a
3
a
b
4
【讨论】:
是的。在您的 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
【讨论】:
是的,and 和 or 运算符都短路了 - 请参阅 the docs。
【讨论】: