Python 3.1内联除法

Jon*_*röm 3 python python-3.x

我不知道这是否是3.1中的错误,但是如果我没记错,"inline"除法在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
Run Code Online (Sandbox Code Playgroud)

但是,3.1给了我这个:

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

......或者我错过了什么?

Jon*_*röm 6

Gaaah!发现__floordiv____truediv__.抱歉!

如果你想告诉我为什么2to3没有翻译__idiv____truediv__a __floordiv__(self, y): self.__truediv__(y),请继续!

  • 您可能想要提交错误报告. (2认同)