小编use*_*426的帖子

Python类__div__问题

元组代表分数.我试图通过乘以收益来划分分数

class Test():
    def __init__(self):
        self._x=(1,2)
    def __div__(self,div_fraction):
        return (self._x[0]*div_fraction[1],self._x[1]*div_fraction[0])

y=Test()
z=y/(1,3)
print(z)
Run Code Online (Sandbox Code Playgroud)

给我:

Traceback (most recent call last):
  File "E:/test.py", line 8, in <module>
   z=y/(1,3)
TypeError: unsupported operand type(s) for /: 'Test' and 'tuple'
Run Code Online (Sandbox Code Playgroud)

然而,当我改变__div__to __mul__和use *而不是/它做它应该做的事情.

我如何修复我得到的异常?

python tuples class operators divide

17
推荐指数
2
解决办法
8107
查看次数

标签 统计

class ×1

divide ×1

operators ×1

python ×1

tuples ×1