On Jan 9, 2008 12:04 AM, Mark Summerfield <[EMAIL PROTECTED]> wrote: > I'm using this as a class decorator that fills in "missing" comparisons: > > def complete_comparisons(cls): > class CompleteComparisonsError(Exception): pass > > if hasattr(cls.__lt__, "__objclass__"): # i.e. < inherited from object > raise CompleteComparisonsError("{0} must define < and " > "ideally ==".format(cls.__name__)) > if hasattr(cls.__eq__, "__objclass__"): # i.e. == inherited from object > cls.__eq__ = lambda self, other: not ( > cls.__lt__(self, other) or cls.__lt__(other, self)) > cls.__ne__ = lambda self, other: not cls.__eq__(self, other) > cls.__gt__ = lambda self, other: cls.__lt__(other, self) > cls.__le__ = lambda self, other: (cls.__lt__(self, other) or > cls.__eq__(self, other)) > cls.__ge__ = lambda self, other: (cls.__lt__(other, self) or > cls.__eq__(self, other)) > return cls > > > The reason for the ugly hasattr() calls is that object itself defines > the comparison operators (in Python 3.0a2).
That's due to object's implementation of __eq__ and __ne__; this is done by having a tp_richcompare slot and then the wrapper generator adds wrappers for all six operators to the class dict. This is not easy to fix. -- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com