0
why doesnt the following code work??
class Vector2D: def __init__(self, x, y): self.x = x self.y = y def __add__(self, other, another): return Vector2D(self.x + other.x + another.x, self.y + other.y + another.y) first = Vector2D(5, 7) second = Vector2D(3, 9) third = Vector2D (1, 1) result = first + second + third print(result.x) print(result.y)
3 Respuestas
+ 4
Here is the working code.
https://code.sololearn.com/cGHEUrzp9ywg/?ref=app/
+ 4
Because the __add__ magic method should have just two parameters, 'self' and 'other':
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
Even then the addition of the three vectors will work because,
adition is a binary operation.
+ 3
got it, thnx