0

what that code do?

class vec: def __init__(self,x,y): self.x=x self.y=y def __add__(self,other): return vec(self.x+other.x,self.y+other.y) v1=vec(3,4) v2=vec(1,2) r= v1+v2 print r.x*r.y

5th Apr 2018, 8:50 PM
AhmedTaha
AhmedTaha - avatar
2 Answers
+ 5
It does this: # Declare the class class vec: # Constructor to save parameters in the instance def __init__(self,x,y): self.x=x self.y=y # Add operator to create a new instance with sum # of x & y for the parameters def __add__(self,other): return vec(self.x+other.x,self.y+other.y) # Create v1 with v1.x=3 & v1.y=4 v1=vec(3,4) # Create v2 with v2.x=1 & v2.y=2 v2=vec(1,2) # Add v1 & v2 creating r with r.x=4 (3+1) & # r.y=6 (4+2) r= v1+v2 # Print 24 (4*6) print(r.x*r.y)
5th Apr 2018, 9:38 PM
John Wells
John Wells - avatar