I am trying to override _add_ method. I am getting error :
Point3 = Point1 + Point2
TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
What am I missing? Please help. This was my first Python class.
from math import sqrt
class Point(object):
def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "%i,%i,%i"%(self.x, self.y, self.z)
def _add_(self, other):
TotalX = self.x + other.x
TotalY = self.y + other.y
TotalZ = self.z + other.z
return Point(TotalX, TotalY, TotalZ)
def Distance(self, other):
val =0
val = ((self.x - other.x)**2+ (self.y - other.y)**2 + (self.z - other.z)**2)
return val
print ("Just defined method")
Point1= Point(x=4, y=2, z=9)
Point2= Point(x=5, y=3, z=10)
Point3 = Point1 + Point2
Thanks, Shruti.
Distanceis not correct... Please Correct that by editing your question