I try to use the built-in function sum()in the list of objects and as a result get the object.
Here is an excerpt of my code:
class vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return vector(self.x+other.x, self.y+other.y)
l = []
l.append(vector(3, 5))
l.append(vector(-2, 3))
l.append(vector(0,-4))
net_force = sum(l)
I get an error message:
TypeError: unsupported operand type(s) for +: 'int' and 'instance'
I assume that since it sum()initially sets the result to 0 and then iterates through the list, but I can only determine if things are added to vector, and not vice versa.
source
share