[Python-Dev] a>b == b<a dangerous?

Tim Peters tim.one@home.com
Sun, 21 Jan 2001 20:32:30 -0500


[Greg Ewing]
> Suppose I have a class which checks whether it knows
> how to do a comparison, and if not, wants to pass it
> on to the other operand in case it knows:
>
>   class Foo:
>
>     def __lt__(self, other):
>       if I_know_about(other):
>         # do the comparison
>       else:
>         return other.__gt__(self)
>
> If the other operand has a __gt__ method which is
> doing similar tricks, infinite recursion could result.

Does this have something to do with comparisons?  That is, wouldn't the same
be true if you coded two methods named "spam" and "eggs" in this way?

whatever = 0

class Foo:
    def spam(self, other):
       if whatever:
           return 1
       else:
           return other.eggs(self)

class Bar:
    def eggs(self, other):
       if whatever:
           return 1
       else:
           return other.spam(self)

Foo().spam(Bar())  # RuntimeError: Maximum recursion depth exceeded

It that's all there is to it, you got what you asked for.