如何重载获取 self class 对象的 __eq__ 函数

How to overload an __eq__ function that gets an object of the self class

我正在尝试的是使用 singledispatch 重载 Posicion class 中的这个函数并尝试遵循 OOP:

def __eq__(self, other):
    if isinstance(other, Posicion):
        return other.get_posicion() == self.get_posicion()

    elif type(other) == tuple:
        assert len(other) == 2, "La tupla pasada por parámetro debe constar de dos elementos"
        self.verificar_formato(other[0], other[1])

        return (other[0].upper(), other[1]) == self.get_posicion()

我尝试从 functools 库中应用 singledispatch,但我 运行 遇到了与这个问题相同的错误:python3: singledispatch in class, how to dispatch self type。因为我正在尝试发送自我类型。所以我尝试了

class _Posicion:
    def __init__(self, y, x):
    pass


class Posicion(_Posicion):
    def __init__(self, y, x):
        super()
        self.x = x
        self.y = y.upper()

    def get_posicion(self):
        return self.y, self.x

    @singledispatch
    def __eq__(self, other):
        raise NotImplementedError("Everything bad")
            

    @__eq__.register(_Posicion)
    def _(self, other):
        return other.get_posicion() == self.get_posicion()
    
    @__eq__.register(tuple)
    def _(self, other):
        assert len(other) == 2, "La tupla pasada por parametro debe constar de dos elementos"
        self.verificar_formato(other[0], other[1])

        return (other[0].upper(), other[1]) == self.get_posicion()


if __name__ == "__main__":
    Posicion('a', 1) == ('a', 1)
    

但它总是进入 @__eq__.register(_Posicion),如果我删除它,它总是进入 def __eq__(self, other):

对于这个问题可能措辞不当,我再次表示歉意,并在此先感谢您的帮助。如果还有其他信息需要补充,请告诉我。

我会混合使用 duck-typing 和 single-dispatch。

@singledispatchmethod
def __eq__(self, other):
    try:
        f = other.get_posicion
    except AttributeError:
        return (self is other) or NotImplemented

    return self.get_posicion() == f()

@__eq__.register(tuple)
def _(self, other):
    assert len(other) == 2, "La tupla pasada por parámetro debe constar de dos elementos"
    self.verificar_formato(other[0], other[1])

    return (other[0].upper(), other[1]) == self.get_posicion()

这会稍微削弱您的尝试:我们不再坚持比较两个 Posicion 实例,而是允许将 Posicion 与实现可调用 get_posicion 属性的任何对象进行比较。如果失败,则根据对象标识进行比较,否则调用两个对象的方法并比较结果。

我们检查的唯一显式类型是 tuple,避免需要在 class 定义本身内部真正引用 Posicion。 (虽然如果你愿意,你可以安全地检查 isinstance(other, Posicion) 里面的 __eq__ 的定义;它只是作为 singledispatchmethod 的一个参数 Posicion 尚未定义。)