+ 1
Need help
class Vector2D: def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): return Vector2D(self.x + other.x, self.y + other.y) first = Vector2D(5, 7) second = Vector2D(3, 9) result = first + second print(result.x) print(result.y) what is other.x and other.y equals to here.
1 Antwort
+ 2
def __add__(self, other):
overloads the + operator for an object of the Vector2D class.
when you use first + second (both of which are Vector2D objects) it is basically the same as calling first.__add__(second)
so other.x and other.y are the x and y variables/values for the Vector2D object second. The 2 objects are added together to create a new object called result in which its x and y values are the sum of first and second's x and y values respectively.