小编noo*_*der的帖子

Python 运算符重载 __div__

我有一个类,其函数muldiv的实现如下:

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

2
推荐指数
1
解决办法
5046
查看次数

标签 统计

python ×1