使用python的V3,我创建:
class NewInt(int):
def __rtruediv__(self, num):
print('in here')
x = NewInt(5)
343 / x # called rtruediv as I expect, but
343.3 / x # does not call rtruediv
Run Code Online (Sandbox Code Playgroud)
不知道为什么这是因为:
x / 343
x / 343.3 #both will call truediv
Run Code Online (Sandbox Code Playgroud)
对我来说,这似乎不一致..
任何人都可以解释为什么会这样……当 Python 执行 343.3 / x 时,是否有可以覆盖的方法?
我在查看有关过载的一些信息时发现了这种行为,并遇到了这种不一致的情况。
戴夫