python - Python 中 bool 值 'and' 和 'or' 的运算符方法是什么?

标签 python parsing operator-keyword

<分区>

例如,这些在 operator module 中定义并且可以这样使用:

import operator
print operator.__add__   # alias add -> +
print operator.__sub__   # alias sub -> -
print operator.__and__   # alias and_ -> &
print operator.__or__    # alias or_ -> |

那么andor的等价物是什么?

print operator."and ?????"  # should be boolean-and
print operator."or ????"    # should be boolean-or

最佳答案

andor 运算符在运算符模块中没有等效项,因为它们不能作为函数实现。这是因为它们短路了:它们可能不会根据第一个操作数的结果来计算第二个操作数。

关于python - Python 中 bool 值 'and' 和 'or' 的运算符方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5084615/

相关文章:

python - Pandas groupby 带有索引的分位数

java - 如何找出java类的继承树的深度?

parsing - Scala 解析 SML 的相互递归函数

C++ boost date_input_facet 似乎使用传递给 facet 构造函数的不正确格式意外地解析日期

c++ - 赋值运算符重载问题

python - 如何在 Python 中找到具有两个元素的数组的均值?

python - 使用 matplotlib 线图的颜色图

floating-point - fmod 是否等同于 HLSL 中的 % 运算符

python - 如何以表格形式显示值,因为现在只显示表格标题

c++ - 如何在 C++ 中同时重载 = 和 [] 运算符