Python中boolean'和','或'的运算符方法是什么?

时间:2022-10-29 19:42:15

For instance, these are defined in the operator module and can be used as such:

例如,这些在运算符模块中定义,可以这样使用:

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

Then what is the equivalent of and and or?

那么什么是and和or?

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

3 个解决方案

#1


6  

These do not exist. The best you can do is to replace them with a lambda:

这些不存在。你能做的最好的事就是用lambda代替它们:

band = (lambda x,y: x and y)
bor = (lambda x,y: x or y)

The reason is you can not implement the complete behavior of and or or because they can short circuit.

原因是你不能实现和/或因为它们可以短路的完整行为。

E.G:

例如:

if variable or long_fonction_to_execute():
    # do stuff

If variable is True, the long_fonction_to_execute will never be called because Python knows than or has to return True anyway. It's an optimization. It's a very desirable feature most of the time, as it can save a lot of useless processing.

如果变量为True,则永远不会调用long_fonction_to_execute,因为Python知道或者必须返回True。这是一个优化。在大多数情况下,这是一个非常理想的功能,因为它可以节省大量无用的处理。

But it means you cannot make it a function:

但这意味着你不能使它成为一个功能:

E.G:

例如:

if bor(variable, long_fonction_to_execute()):
    # do stuff

In that case, long_fonction_to_execute is called even before being evaluated.

在这种情况下,甚至在评估之前就会调用long_fonction_to_execute。

Luckily, you rarely need something like that given the fact that you an use generators and list comprehensions.

幸运的是,鉴于您使用生成器和列表推导这一事实,您很少需要类似的东西。

#2


12  

The and and or operators don't have an equivalent in the operator module, because they can't be implemented as a function. This is because they are short-circuiting: they may not evaluate their second operand depending on the outcome of the first.

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

#3


3  

Extension of e-satis's answer:

延长电子满意答案:

lazyand = (lambda x,y: x() and y())
lazyor = (lambda x,y: x() or y())

The difference here is the conditions passed in are themselves thunks (functions of the form "() -> value") which are only evaluated as needed. (It could be argued that only y needs to be lazily evaluated, but I wrote it as such for consistency).

这里的区别在于传入的条件本身是thunks(形式为“( - - > value”)的函数),它们仅根据需要进行评估。 (可以说只有y需要被懒惰地评估,但我为了一致性而这样写了)。

That is, this preserves the "lazy" aspect of and (and or) at the expense of more verbose code and more objects/method invocations.

也就是说,这会以更冗长的代码和更多的对象/方法调用为代价来保留和(和/或)的“懒惰”方面。

andexpr = lazyand(lambda: false, lambda: never_executed())
andexpr() # false

I would be hard pressed to actually recommend using this approach though - it is important to note that these thunks must be explicit, as shown above. This might be one reason it was not included in the operator module. Some other languages allow pass-by-name or implicit "lifting".

我很难真正推荐使用这种方法 - 重要的是要注意这些thunk必须是明确的,如上所示。这可能是它未包含在运算符模块中的一个原因。其他一些语言允许按名称或隐式“提升”。

Happy coding.

快乐的编码。

#1


6  

These do not exist. The best you can do is to replace them with a lambda:

这些不存在。你能做的最好的事就是用lambda代替它们:

band = (lambda x,y: x and y)
bor = (lambda x,y: x or y)

The reason is you can not implement the complete behavior of and or or because they can short circuit.

原因是你不能实现和/或因为它们可以短路的完整行为。

E.G:

例如:

if variable or long_fonction_to_execute():
    # do stuff

If variable is True, the long_fonction_to_execute will never be called because Python knows than or has to return True anyway. It's an optimization. It's a very desirable feature most of the time, as it can save a lot of useless processing.

如果变量为True,则永远不会调用long_fonction_to_execute,因为Python知道或者必须返回True。这是一个优化。在大多数情况下,这是一个非常理想的功能,因为它可以节省大量无用的处理。

But it means you cannot make it a function:

但这意味着你不能使它成为一个功能:

E.G:

例如:

if bor(variable, long_fonction_to_execute()):
    # do stuff

In that case, long_fonction_to_execute is called even before being evaluated.

在这种情况下,甚至在评估之前就会调用long_fonction_to_execute。

Luckily, you rarely need something like that given the fact that you an use generators and list comprehensions.

幸运的是,鉴于您使用生成器和列表推导这一事实,您很少需要类似的东西。

#2


12  

The and and or operators don't have an equivalent in the operator module, because they can't be implemented as a function. This is because they are short-circuiting: they may not evaluate their second operand depending on the outcome of the first.

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

#3


3  

Extension of e-satis's answer:

延长电子满意答案:

lazyand = (lambda x,y: x() and y())
lazyor = (lambda x,y: x() or y())

The difference here is the conditions passed in are themselves thunks (functions of the form "() -> value") which are only evaluated as needed. (It could be argued that only y needs to be lazily evaluated, but I wrote it as such for consistency).

这里的区别在于传入的条件本身是thunks(形式为“( - - > value”)的函数),它们仅根据需要进行评估。 (可以说只有y需要被懒惰地评估,但我为了一致性而这样写了)。

That is, this preserves the "lazy" aspect of and (and or) at the expense of more verbose code and more objects/method invocations.

也就是说,这会以更冗长的代码和更多的对象/方法调用为代价来保留和(和/或)的“懒惰”方面。

andexpr = lazyand(lambda: false, lambda: never_executed())
andexpr() # false

I would be hard pressed to actually recommend using this approach though - it is important to note that these thunks must be explicit, as shown above. This might be one reason it was not included in the operator module. Some other languages allow pass-by-name or implicit "lifting".

我很难真正推荐使用这种方法 - 重要的是要注意这些thunk必须是明确的,如上所示。这可能是它未包含在运算符模块中的一个原因。其他一些语言允许按名称或隐式“提升”。

Happy coding.

快乐的编码。