Python 3.1 inline division

I don’t know if this is a bug in 3.1, but if I remember correctly that the “built-in” unit worked like this in versions up to 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

However, 3.1 gives me the following:

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

... or am I missing something?

+3
source share
1 answer

Gaaah! Found __floordiv__and __truediv__. Sorry!

If you want to tell me why 2to3 does not translate __idiv__into __truediv__s __floordiv__(self, y): self.__truediv__(y), please continue!

+6
source

Source: https://habr.com/ru/post/1716632/


All Articles