class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate object's x coordinate.# Getter methods are better practice than just accessing an attribute directlyreturn self.xdef getY(self):# Getter method for a Coordinate object's y coordinatereturn self.ydef __str__(self):return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'def __eq__(self, other):if self.getX == other.getX and self.getY == other.getY:return Trueelse:return False c = Coordinate(2, 3)
d = Coordinate(2, 3)c == d
Your __eq__
method should probably be like this:
It avoids comparing objects of different type and alerts of the error immediately, and avoids the use of getters intra class.
def __eq__(self, other):if self.__class__.__name__ == other.__class__.__name__:return self.x == other.x and self.y == other.yelse:raise NotImplementedError
or in python > 3.3:
def __eq__(self, other):if self.__class__.__qualname__ == other.__class__.__qualname__:return self.x == other.x and self.y == other.yelse:raise NotImplementedError