Rug*_*rra 5 python operators redefine and-operator
为什么我不能重新定义__and__运算符?
class Cut(object):
def __init__(self, cut):
self.cut = cut
def __and__(self, other):
return Cut("(" + self.cut + ") && (" + other.cut + ")")
a = Cut("a>0")
b = Cut("b>0")
c = a and b
print c.cut()
Run Code Online (Sandbox Code Playgroud)
我想(a>0) && (b>0),但我得到了b,通常的行为and
Ned*_*der 11
__and__是二进制(按位)&运算符,而不是逻辑and运算符.
由于and操作员是短路操作员,因此无法将其作为一种功能实现.也就是说,如果第一个参数为false,则根本不评估第二个参数.如果您尝试将其作为函数实现,则必须在调用函数之前评估这两个参数.
| 归档时间: |
|
| 查看次数: |
1993 次 |
| 最近记录: |