布尔“与”、“或”的运算符方法是什么?在Python中?
例如,这些是在 operator module 中定义的,并且可以这样使用:
import operator
print operator.__add__ # alias add -> +
print operator.__sub__ # alias sub -> -
print operator.__and__ # alias and_ -> &
print operator.__or__ # alias or_ -> |
然后呢相当于and
和or
吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
and
和or
运算符在运算符模块中没有等效项,因为它们无法作为函数实现。这是因为它们是短路的:它们可能不会根据第一个操作数的结果来评估第二个操作数。The
and
andor
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.这些都不存在。您能做的最好的事情就是用 lambda 替换它们:
原因是您无法实现
and
或or
的完整行为,因为它们可能会短路。EG:
如果
variable
为True
,long_fonction_to_execute
将永远不会被调用,因为Python知道or
必须返回<无论如何,代码> True 。这是一个优化。大多数时候这是一个非常理想的功能,因为它可以节省大量无用的处理。但这意味着您不能将其设为函数:
EG:
在这种情况下,
long_fonction_to_execute
甚至在评估之前就被调用。幸运的是,考虑到您使用生成器和列表推导式,您很少需要类似的东西。
These do not exist. The best you can do is to replace them with a lambda:
The reason is you can not implement the complete behavior of
and
oror
because they can short circuit.E.G:
If
variable
isTrue
, thelong_fonction_to_execute
will never be called because Python knows thanor
has to returnTrue
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:
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.
e-satis 答案的扩展:
这里的区别是传入的条件是它们本身的 thunk(“() -> value”形式的函数),它们仅被评估根据需要。 (可能有人会说,只有
y
需要延迟计算,但为了保持一致性,我这样写)。也就是说,这保留了
and
(和or
)的“惰性”方面,但代价是更冗长的代码和更多的对象/方法调用。不过,我很难真正推荐使用这种方法 - 重要的是要注意这些重击必须显式,如上所示。这可能是它没有包含在
operator
模块中的原因之一。其他一些语言允许通过名称或隐式“提升”。快乐编码。
Extension of e-satis's answer:
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
(andor
) at the expense of more verbose code and more objects/method invocations.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.