I have "TypeError: unsupported operand type(s) for /: " for this code
class Foo(object):
def __add__(self, other):
return print("add")
def __div__(self, other):
return print("div")
Foo() + Foo()
add
** BUT for / **
Foo() / Foo()
Traceback (most recent call last):
File "<ipython-input-104-8efbe0dde481>", line 1, in <module>
Foo() / Foo()
TypeError: unsupported operand type(s) for /: 'Foo' and 'Foo'
turbulencetoo :
Python3 uses special division names: __truediv__ and __floordiv__ for the / and // operators, respectively.\n\nIn Python3, the / is a true division in that 5/2 will return the floating point number 2.5. Similarly 5//2 is a floor division or integer division because it will always return an int, in this case 2.\n\nIn Python2 the / operator worked the same way that the // operator works in Python3. Because of the way that the operators changed between versions, the __div__ name was removed to to avoid ambiguity. \n\nReference: http://www.diveintopython3.net/special-method-names.html#acts-like-number",
2016-11-23T17:23:05