I am new to OOP programming in Python. I did this tutorial on operator overloading from here (scroll down to operator overload). I could not understand this piece of code. Hope someone explains this in detail. To be precise, I did not understand how 2 objects are added here and what are the strings
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
def __add__(self,other):
return Vector(self.a + other.a, self.b + other.b)
here?
class Vector:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return 'Vector (%d, %d)' % (self.a, self.b)
def __add__(self,other):
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2
This generates an output vector (7.8). How are objects v1 and v2 added here?
source
share