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中的运算符返回操作数的值而不是布尔值(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
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.
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)
| 归档时间: |
|
| 查看次数: |
82944 次 |
| 最近记录: |