> According to the documentation I have, one overloads == and != by
> defining __cmp__ in a class definition. Unfortunately, the is the (to
> me) undesirable side effect of also overloading <, <=, >, and >=.
> There are (non-magnitude) classes for which one would like to override
> ==, and !=, but not <, <=, >, and >=. Is there a way to do this? Is
> something planned?
After some email discussion with Guido.van.Rossum@cwi.nl, I have
decided to propose a change to python:
When defining classes, the function __equal__(self,other) may be
defined instead of the function __cmp__(self,other). The __equal__
function only tests equality. If self and other are equal, then
__equal__ returns a non-zero value, otherwise it returns 0. When
__equal__ is defined rather than __cmp__, then the comparisons <, >,
<=, and => are illegal for the type being defined and cause
TypeError exceptions to be raised. The functions __equal__ and
__cmp__ may not both be defined in the same class. An __equal__
definition overrides any __cmp__ definitions would otherwise be
inherited from superclasses as well as the default __cmp__
definition.
Benefits:
This enhancement allows definition of classes for objects that
cannot be ordered, but for which testing of equality is meaningful.
It provides greater type safety because it causes meaningless
comparisons to raise TypeError exceptions.
Impact:
As I understand it, existing code generally does not expect
comparisons to raise exceptions. With this change, TypeErrors may
be raised in situations where they were not raised before. For
example, TypeError exceptions may be raised when sorting.
-- -- Jim Fulton jfulton@dis2qvarsa.er.usgs.gov (703) 648-5622 U.S. Geological Survey, Reston VA 22092 This message is being posted to obtain or provide technical information relating to my duties at the U.S. Geological Survey.