class Point(object):
''' A point on a grid at location x, y '''
def __init__(self, x, y):
self.X=x
self.Y=y
def __str__(self):
return "X=" + str(self.X), "Y=" + str(self.Y)
def __add__(self, other):
if not isinstance(other, Point):
raise TypeError("must be of type point")
x= self.X+ other.X
y= self.Y+ other.Y
return Point(x, y)
p1= Point(5, 8)
print p1 + [10, 12]
When trying to add list or tuple at RHS i.e. print p1 + [10, 12], I'm getting
attributeError: int object has no attribute
How can this problem be solved?
TypeError("must be of type point"). Because you're adding a type other than a point, to the point. That's exactly what you told your code to do, what's the issue?[10, 12]clearly does not equalPoint(10,12). You are adding a) list to point, b) point to list. Both operations are not supported by your code right now. first one may potentially be implemented (but really shouldn't), second may not.