0

class question __init__

why can I result it ? 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 #result = first.__add__(second) result = Vector2D(5,7)+Vector2D.__add__(3,9) print(result.x) print(result.y)

12th Aug 2020, 9:27 AM
吳宗原
吳宗原 - avatar
2 Antworten
+ 4
This is illegal: Vector2D.__add__(3, 9) Because when you called Vector2D.__add__ you passed 2 integers 3 and 9 for parameters self and other. Then you tried self.x, which is illegal, because self is an integer and integers don't have attribute x. also other.x, self.y and other.y raised similar errors.
12th Aug 2020, 9:38 AM
Seb TheS
Seb TheS - avatar
0
why these codes below is not equal ? #first = Vector2D(5, 7) #second = Vector2D(3, 9) #result = first + second #result = first.__add__(second) result = Vector2D(5,7)+Vector2D.__add__(3,9)
12th Aug 2020, 9:28 AM
吳宗原
吳宗原 - avatar