布尔“与”、“或”的运算符方法是什么?在Python中?

发布于 2024-10-18 21:05:53 字数 526 浏览 6 评论 0原文

例如,这些是在 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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-10-25 21:05:53

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

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.

薄荷港 2024-10-25 21:05:53

这些都不存在。您能做的最好的事情就是用 lambda 替换它们:

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

原因是您无法实现 andor 的完整行为,因为它们可能会短路。

EG:

if variable or long_fonction_to_execute():
    # do stuff

如果variableTruelong_fonction_to_execute将永远不会被调用,因为Python知道or必须返回<无论如何,代码> True 。这是一个优化。大多数时候这是一个非常理想的功能,因为它可以节省大量无用的处理。

但这意味着您不能将其设为函数:

EG:

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

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

幸运的是,考虑到您使用生成器和列表推导式,您很少需要类似的东西。

These do not exist. The best you can do is to replace them with a 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.

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.

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

往事风中埋 2024-10-25 21:05:53

e-satis 答案的扩展:

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

这里的区别是传入的条件是它们本身的 thunk(“() -> value”形式的函数),它们仅被评估根据需要。 (可能有人会说,只有 y 需要延迟计算,但为了保持一致性,我这样写)。

也就是说,这保留了 and (和 or)的“惰性”方面,但代价是更冗长的代码和更多的对象/方法调用。

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

不过,我很难真正推荐使用这种方法 - 重要的是要注意这些重击必须显式,如上所示。这可能是它没有包含在 operator 模块中的原因之一。其他一些语言允许通过名称或隐式“提升”。

快乐编码。

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).

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".

Happy coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文