How can I overload operators (+, -, *, /, ... , //)
on an object in python.
Example:
a = Vector(3, 5)
b = Vector(2, 7)
print(a + b) # Output: <Vector (5.000000, 12.000000)>
print(b - a) # Output: <Vector (-1.000000, 2.000000)>
print(b * 1.3) # Output: <Vector (2.600000, 9.100000)>
print(a // 17) # Output: <Vector (0.000000, 0.000000)>
print(a / 17) # Output: <Vector (0.176471, 0.294118)>
where Vector
is the following class:
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y