container.___le___ can use only <=?

Carl Banks pavlovevidence at gmail.com
Sat Dec 15 09:39:52 EST 2007


On Dec 15, 9:05 am, ceru... at tds.net wrote:
> On Dec 15, 8:33 am, Carl Banks <pavlovevide... at gmail.com> wrote:
>
>
>
> > On Dec 14, 4:15 pm, Neil Cerutti <horp... at yahoo.com> wrote:
>
> > > 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.
>
> > And yet, by the same reasoning, using > and <= for list and tuple is
> > also a "bad idea".
>
> My reasoning is (I hope) that the container ought to support every
> comparison operation supported by the contained objects. This can be
> ensured by being careful in the implementation.

I see what you're asking now.  Yep, probably a bad idea to use that
shortcut.


Carl Banks



More information about the Python-list mailing list