Python支持短路吗?

Din*_*nah 301 python boolean-logic short-circuiting

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

Ale*_*lli 283

是的,两者都andor运营商短路 - 请参阅文档.


Gri*_*han 180

操作员的短路行为and,or:

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

>>> def fun(i):
...     print "executed"
...     return i
... 
Run Code Online (Sandbox Code Playgroud)

在下面的例子中,可以观察Python的运算符的短路行为:andor

>>> 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
Run Code Online (Sandbox Code Playgroud)

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

        False    None    0    ""    ()    []     {}
Run Code Online (Sandbox Code Playgroud)

功能中的短路行为: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
Run Code Online (Sandbox Code Playgroud)

该函数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
Run Code Online (Sandbox Code Playgroud)

链式比较中的短路行为:

另外,在Python中

比较可以任意链接 ; 例如,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
Run Code Online (Sandbox Code Playgroud)

编辑:
还有一点需要注意: -逻辑and,or Python中的运算符返回操作数的而不是布尔值(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 
Run Code Online (Sandbox Code Playgroud)

类似地,or运算符返回最左边的值bool(value)== Trueelse else right false value(根据短路行为),示例:

>>> 2 or 5    # left most operand bool(2) == True
2    
>>> 0 or 5    # bool(0) == False and bool(5) == True
5
>>> 0 or ()
()
Run Code Online (Sandbox Code Playgroud)

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

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

In [172]: name
Out[172]: '<Unkown>'
Run Code Online (Sandbox Code Playgroud)

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

  • 小问题:虚假值的明确列表有点误导。*任何*类型都可以有一个或多个假值。按照惯例,所有值为“0”的数字类型都是假的(所以它不仅仅是“0”,它是“0.0”、“0j”、“decimal.Decimal(0)”、“fractions.Fraction(0)”等.),所有长度为“0”的集合也是如此(因此在您列出的内容之上,还有“b”[Py3]、“u”[Py2] 和“set()”/“frozenset()”都是评估为假的内置函数),但用户定义/第三方类型可以定义自己的类型(直接使用 `__bool__` [Py3]/`__nonzero__` [Py2],或通过定义 `__len__` 间接)。 (2认同)
  • @ShadowRanger 这里你的评论将完成我的答案。感谢您添加此注释。 (2认同)

Cap*_*oja 47

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

>>>False and 3/0
False
>>>True and 3/0
ZeroDivisionError: integer division or modulo by zero
Run Code Online (Sandbox Code Playgroud)

要么

>>>True or 3/0
True
>>>False or 3/0
ZeroDivisionError: integer division or modulo by zero
Run Code Online (Sandbox Code Playgroud)