我有一个类,其函数、mul、和div实现如下:
def __mul__(self, other):
return Foo(self.a * other)
def __div__(self, other):
return Foo(self.a / other)乘法(例如a* b,其中a是类Foo的实例,b是整数)工作得很好,但是除法(a / b)给出了一个错误,表示不支持操作符。我怎么才能避开这一切?
TypeError: unsupported operand type(s) for /: 'Foo' and 'int'发布于 2015-11-29 19:55:37
您正在使用from __future__ import division。因此,您必须为/实现__truediv__,为//实现__floordiv__。
发布于 2015-11-29 19:52:13
使用__rdiv__:带反向操作数的除法。rdiv
https://stackoverflow.com/questions/33987239
复制相似问题