我有一个类,其函数mul和div的实现如下:
def __mul__(self, other):
return Foo(self.a * other)
def __div__(self, other):
return Foo(self.a / other)
Run Code Online (Sandbox Code Playgroud)
乘法(例如 a * b,其中 a 是 Foo 类的实例,b 是整数)工作正常,但除法 (a / b) 会给出错误,指出不支持该运算符。我该如何解决这个问题?
TypeError: unsupported operand type(s) for /: 'Foo' and 'int'
Run Code Online (Sandbox Code Playgroud) python ×1