0

Can you explain in this code how does self,other workd ?

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) p=vector2D(5,2) q=vector2D(7,9) r=vector2D(5,0) r=p+q+r # r=p.__add__(q) print("{}i + {}j".format(r.x,r.y))

7th May 2020, 11:45 AM
Narayanasamy Lorshan
Narayanasamy Lorshan - avatar
1 Answer
+ 2
self refers to the current object (left of the operator). other refers to another object (right of the operator). When doing p+q. p is this and q is other. What it returns is a vector2D(p.x+q.x, p.y+q.y)
7th May 2020, 11:56 AM
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ - avatar