Python 中 AndAlso (&&) 和 OrElse (||) 逻辑运算符的等价物是什么?
What's the equivalent of AndAlso (&&) and OrElse (||) logical operators in Python?
在.NET VB和C#中,我们可以使用AndAlso (&&), OrElse (||)逻辑运算
Python中的等价逻辑运算符是什么?它们是否仅限于“and”和“or”?
已更新:下面是And/Or和AndAlso/OrElse
的区别
引自
Or/And will always evaluate both1 the expressions and then return a
result. They are not short-circuiting evaulation.
OrElse/AndAlso are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation
of the left expression alone. (That means: OrElse will only evaluate
the right expression if the left expression is false, and AndAlso will
only evaluate the right expression if the left expression is true.)
在python中,可以使用逻辑门。
您可以使用 and
或 &
。
https://docs.python.org/2/library/stdtypes.html#bitwise-operations-on-integer-types
Python 已经这样做了:
def foo():
print ("foo")
return True
def bar():
print ("bar")
return False
print (foo() or bar())
print ("")
print (bar() or foo())
Returns:
foo
True
bar
foo
True
Python 中不需要 AndAlso 或 OrElse,它会延迟计算布尔条件 (docs)。
在.NET VB和C#中,我们可以使用AndAlso (&&), OrElse (||)逻辑运算
Python中的等价逻辑运算符是什么?它们是否仅限于“and”和“or”?
已更新:下面是And/Or和AndAlso/OrElse
的区别
引自
Or/And will always evaluate both1 the expressions and then return a result. They are not short-circuiting evaulation.
OrElse/AndAlso are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation of the left expression alone. (That means: OrElse will only evaluate the right expression if the left expression is false, and AndAlso will only evaluate the right expression if the left expression is true.)
在python中,可以使用逻辑门。
您可以使用 and
或 &
。
https://docs.python.org/2/library/stdtypes.html#bitwise-operations-on-integer-types
Python 已经这样做了:
def foo():
print ("foo")
return True
def bar():
print ("bar")
return False
print (foo() or bar())
print ("")
print (bar() or foo())
Returns:
foo
True
bar
foo
True
Python 中不需要 AndAlso 或 OrElse,它会延迟计算布尔条件 (docs)。