I would like to write something like this in python:
a = (1, 2)
b = (3, 4)
c = a + b
d = 3 * b
I understand that you can overload statements to work with custom classes, but is there a way to overload statements to work with pairs?
Of course, solutions like
c = tuple([x+y for x, y in zip(a, b)])
really work, but, dropping the performance, they are not as good as operator overload +.
Of course, you can define functions addand mul, such as
def add((x1, y1), (x2, y2)):
return (x1 + x2, y1 + y2)
def mul(a, (x, y)):
return (a * x, a * y)
but still the opportunity to write q * b + rinstead add(times(q, b), r)will be more pleasant.
Ideas?
. , , + , , . - - =)