[Python-ideas] Automatic total ordering

Terry Reedy tjreedy at udel.edu
Wed Oct 15 20:54:27 CEST 2008


Dillon Collins wrote:
> 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.

I do not understand this response.  In 3.0, the actual definitions are 
the C equivalent of

class object():
   def __eq__(self,other): return id(self) == id(other)
   def __ne__(self,other): return id(self) != id(other)
   def __lt__(self,other): return NotImplemented
   <etcetera>

tjr




More information about the Python-ideas mailing list