I have a class that has the functions mul and div implemented as follows:
def __mul__(self, other):
return Foo(self.a * other)
def __div__(self, other):
return Foo(self.a / other)
The multiplication (e.g. a * b, where a is an instance of the class Foo and b is an integer) works fine, but the division (a / b) gives an error saying the operator is not supported. How do I get around this?
TypeError: unsupported operand type(s) for /: 'Foo' and 'int'
You are using from __future__ import division. So you have to implement __truediv__ for /, and __floordiv__ for //.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With