container.___le___ can use only <=?

Neil Cerutti horpner at yahoo.com
Fri Dec 14 16:15:44 EST 2007


When implementing the rich comparison operators for some sort of
container, it's tempting to save code by doing something like:

 class LarchTree:
   ...
   def __gt__(self, other):
     # A lot of code to traverse the tree
   def __le__(self):
     return not self > other

However, if I'm thinking correctly, this is a bad idea. The
reasoning being that > and <= are not required to be each others
opposite for arbitrary value types, and may not even both be
supplied by the contained objects.

If a LarchTree user stores objects that don't support __gt__,
will he have a legitimate complaint when using LarchTree.__le__
results in an attribute error?

-- 
Neil Cerutti



More information about the Python-list mailing list