why doesnt the following code work?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

28th Jun 2018, 11:01 AM
Raghav Naswa
Raghav Naswa - avatar
3 Answers
28th Jun 2018, 11:12 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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.
28th Jun 2018, 11:11 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 3
got it, thnx
28th Jun 2018, 11:13 AM
Raghav Naswa
Raghav Naswa - avatar