Peter Otten写道: Peter Otten wrote: Chris写道: Chris wrote: >>我正在尝试重载__invert__运算符(〜),这样它可以采取第二个参数,而不是 self,以便我可以表达: x~y 使用: def __invert __(self,other):<做某事> 例如。这可能吗? >>I am trying to overload the __invert__ operator (~) such thatit can take a second argument, other thanself, so that I can express:x ~ yby using:def __invert__(self, other): <do something>for example. Is this possible? 不,在python甚至查找名称之前你会得到一个语法错误: No, you will get a syntax error before python even look up the names: >>>> x >>>>x Traceback(最近一次调用最后一次) : 文件"< stdin>",第1行,在? NameError:名称''x'未定义 Traceback (most recent call last): File "<stdin>", line 1, in ?NameError: name ''x'' is not defined >>>> x~x >>>>x ~ x 文件" ;< stdin>",第1行 x~x ^ 语法错误:语法无效 Peter File "<stdin>", line 1 x ~ x ^SyntaxError: invalid syntaxPeter 似乎是一个任意限制。考虑 - x 和 x - y 这与限制一元行动是不一致的。 詹姆斯 Seems an arbitrary limitation. Consider - x and x - y Which is inconsistent with limiting ~ to a unary operation. James 这篇关于重载波浪运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 16:52