Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-26 Thread Юлий Тихонов
# 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

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-26 Thread Jack Diederich
On Mon, Jan 25, 2010 at 6:59 AM, Lennart Regebro lrege...@jarn.com wrote: [snip] If class A returns NotImplemented when compared to class B, and class B implements the recipe above, then we get infinite recursion, because 1. A() B() will call A.__lt__(B) which will return NotImplemented. 2.

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-26 Thread Lennart Regebro
On Tue, Jan 26, 2010 at 09:15, Юлий Тихонов july.t...@gmail.com wrote: It looks like __cmp__, not __lt__ method. Oh, yeah, sorry. I copy/pasted code from my code where I use __cmp__, :) However, with this replacement test also falls into recursion. Told ya. :) -- Lennart Regebro: Python,

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-26 Thread Nick Coghlan
Lennart Regebro wrote: 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

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-26 Thread Michael Foord
On 26/01/2010 12:22, Nick Coghlan wrote: Lennart Regebro wrote: 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

[Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Lennart Regebro
If you look at the description of the rich comparison methods in the documentation: http://docs.python.org/reference/datamodel.html#object.__lt__ It refers to a recipe: http://code.activestate.com/recipes/576529/ However, that recipe will convert a __ge__(self, other) into a other self. So when

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Nick Coghlan
Lennart Regebro wrote: 1. A() B() will call A.__lt__(B) which will return NotImplemented. 2. which will mean that Python calls B.__ge__(A) 3. Which B implements by doing A B 4. Start over at 1. Have I missed something, or is this recipe incomplete by not handling the NotImplemented

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Lennart Regebro
On Mon, Jan 25, 2010 at 14:34, Nick Coghlan ncogh...@gmail.com wrote: However, returning NotImplemented generally implies that A and B are *different* classes Which is exactly the case here. so I think this is more of a theoretical problem than a practical one. How so? The whole point of

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Nick Coghlan
Lennart Regebro wrote: On Mon, Jan 25, 2010 at 14:34, Nick Coghlan ncogh...@gmail.com wrote: However, returning NotImplemented generally implies that A and B are *different* classes Which is exactly the case here. It wasn't in my tests though - I used the same class on both sides of the

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Lennart Regebro
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. OK,

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Nick Coghlan
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

Re: [Python-Dev] Rich Comparison recipe wrong?

2010-01-25 Thread Lennart Regebro
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