Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重定义_和_运算符_Python_Operators_Redefine_And Operator - Fatal编程技术网

Python 重定义_和_运算符

Python 重定义_和_运算符,python,operators,redefine,and-operator,Python,Operators,Redefine,And Operator,为什么我不能重新定义\u和操作符 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 prin

为什么我不能重新定义
\u和
操作符

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

我想要
(a>0)&(b>0)
,但我得到了b,
&
运算符,而不是逻辑
运算符


由于
运算符是短路运算符,因此不能将其作为函数实现。也就是说,如果第一个参数为false,则根本不计算第二个参数。如果您试图将其作为一个函数来实现,那么在调用函数之前必须对这两个参数进行求值。

因为您无法在Python中重新定义关键字(这就是
的含义)<代码>\uuuu添加\uuuu
用于执行其他操作:


b=cut(“b>0”)应该是cut(大写)注意,您可以通过提供更复杂的接口来允许带短路的
重载,正如Python()所建议的那样,尽管它不受支持。有趣的是,我没有看到PEP。根据关键字和运算符的这种奇怪区别,不能在中重新定义
。但是
\uuuuu contains()\uuuuu
是完全有效的。。。