小编Dav*_*ave的帖子

Python:rtruediv 没有按我的预期工作

使用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 时,是否有可以覆盖的方法?

我在查看有关过载的一些信息时发现了这种行为,并遇到了这种不一致的情况。

戴夫

python operator-overloading division

5
推荐指数
2
解决办法
2563
查看次数

标签 统计

division ×1

operator-overloading ×1

python ×1