# compare_test.py
    @total_ordering
    class StrictComparator(object):
    <...>

        def __lt__(self, other):
            if not isinstance(other, StrictComparator):
                return NotImplemented
            else:
                return (self.v > other.v) - (self.v < other.v)

It looks like __cmp__, not __lt__ method. __lt__ method should look like:

        def __lt__(self, other):
            if not isinstance(other, StrictComparator):
                return NotImplemented
            else:
                return self.v < other.v

However, with this replacement test also falls into recursion.

2010/1/26 Lennart Regebro <lrege...@jarn.com>

> On Tue, Jan 26, 2010 at 02:56, Nick Coghlan <ncogh...@gmail.com> wrote:
> > Lennart Regebro wrote:
> >> On Mon, Jan 25, 2010 at 15:30, Nick Coghlan <ncogh...@gmail.com> wrote:
> >>> Ah, you mean the case where both classes implement the recipe, but know
> >>> nothing about each other and hence both return NotImplemented from
> their
> >>> root comparison?
> >>
> >> Well, only one needs to return NotImplemented, actually.
> >
> > I'd like to see a test case that proved that. With two different types
> > and only one of them returning NotImplemented, the recursion should
> > terminate inside the one with the root comparison that can handle both
> > types.
>
> It never gets to that root comparison, as several of that types
> comparisons just refer back to the type who returns NotImplemented. To
> solve it you need to never refer to the other type in your
> comparisons. And as far as I see that makes it impossible to implement
> full comparisons with only one operator implemented.
>
> In short, the recipe assumes you never compare with other types, but
> it doesn't check for it.
>
> Test attached (assuming this list accepts attachements).
>
> --
> Lennart Regebro: Python, Zope, Plone, Grok
> http://regebro.wordpress.com/
> +33 661 58 14 64
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/july.tikh%40gmail.com
>
>
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to