I have a class
class Vector():
def __init__(self, *args):
self.coords = list(args)
def __add__(self, other):
temp = []
for i in range(len(self)):
tmp.append(self[i] + other[i])
print(str(tmp))
return Vector(*tmp)
def __getitem__(self, key):
return self.coords[key]
The goal is to make Vector class objects behave like vectors. I want do the addition operations like this
list = [0, 1, 2, 3]
v1 = Vector(3, 2, 1, 0)
result1 = v1 + list
result2 = list + v1
and get this results:
result1 = Vector(3, 3, 3, 3)
result2 = Vector(3, 3, 3, 3)
When i defined the getitem method, Vector class started support indexing and result1
is correct. But result 2
returns TypeError: can only concatenate list (not "Vector") to list
. I ran out of ideas what should I looking for to resolve this.