object-oriented programming table comparison special methods special method usage description __lt__(selfotherx __le__(selfotherx < returns true if is less than or equal to __eq__(selfotherx = returns true if is equal to __ne__(selfotherx ! returns true if is not equal to __ge__(selfotherx > returns true if is greater than or equal to __gt__(selfotherx returns true if is less than returns true if is greater than would get an attributeerror exception (since ints don' have an attributeon the other handwe can compare point objects with other objects that coincidentally just happen to have an attribute (thanks to python' duck typing)but this may lead to surprising results if we want to avoid inappropriate comparisons there are few approaches we can take one is to use an assertionfor exampleassert isinstance(otherpointanother is to raise typeerror to indicate that comparisons between the two types are not supportedfor exampleif not isinstance(otherpoint)raise typeerror(the third way (which is also the most pythonically correctis to do thisif not isinstance(otherpoint)return notimplemented in this third caseif notimplemented is returnedpython will then try calling other __eq__(selfto see whether the other type supports the comparison with the point typeand if there is no such method or if that method also returns notimplementedpython will give up and raise typeerror exception (note that only reimplementations of the comparison special methods listed in table may return notimplemented the built-in isinstance(function takes an object and class (or tuple of classes)and returns true if the object is of the given class (or of one of the tuple of classes)or of one of the class' (or one of the tuple of classes'base classes def __repr__(self)return "point({ ! }{ ! })format(selfstr format( the built-in repr(function calls the __repr__(special method for the object it is given and returns the result the string returned is one of two kinds one kind is where the string returned can be evaluated using the built-in eval(function to produce an object equivalent to the one repr(was called on the other kind is used where this is not possiblewe will see an example later on here is how we can go from point object to string and back to point object