[Python-ideas] Automatic total ordering

Dillon Collins dillonco at comcast.net
Wed Oct 15 20:17:31 CEST 2008

On Wednesday 15 October 2008, Terry Reedy wrote:
> Since rich comparisons are defined on object and are inherited by all
> classes, it would be difficult to make them not defined.

Well, I believe that the suggestion comes down to having object's rich 
comparison operators try to use those of it's subclass rather than just 
throwing an error.

Something like:

class object():
	def __lt__(self, o): raise Error
	def __eq__(self, o): raise Error
	def __ne__(self, o):
		return not self.__eq__(self, o)
	def __le__(self, o):
		return self.__lt__(self, o) or self.__eq__(self, o)
	def __gt__(self, o):
		return not (self.__lt__(self, o) or self.__eq__(self, o))
	def __ge__(self, o):
		return not self.__lt__(self, o) and self.__eq__(self, o)

Of course, if it were to actually be implemented, it would make more sense if 
it could use any two non complement ops (rather than lt and eq), but that 
would also make some trouble, I think.

BTW, rather than the class decorator, you could just inherit (multiply?) the 
above class for total ordering as well.

More information about the Python-ideas mailing list