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))
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)