【问题标题】:Python 3.1 inline division overridePython 3.1 内联除法覆盖
【发布时间】:2010-11-24 18:36:21
【问题描述】:

我不知道这是否是 3.1 中的错误,但如果我没记错的话,“内联”除法在 3k 之前的版本中是这样工作的:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...     def __init__(self, x):
...             self.x = x
...     def __idiv__(self, y):
...             self.x /= y
...
>>> a = A(5)
>>> a /= 2

但是,3.1 给了我这个:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /=: 'A' and 'int'

...还是我错过了什么?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    啊啊啊!找到 __floordiv____truediv__。对不起!

    如果您想告诉我为什么 2to3 不能将 __idiv__ 转换为带有 __floordiv__(self, y): self.__truediv__(y)__truediv__,请继续!

    【讨论】:

    • 您可能需要提交一份错误报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多